【问题标题】:data passing app in falcon python猎鹰python中的数据传递应用程序
【发布时间】:2016-04-09 16:29:25
【问题描述】:

在问这个问题之前,我想提一下,我知道我可以使用 django 来制作应用程序,但我需要使用 falcon 而不是别的。

我只是在寻找一种方法

让我们看一个非常简单的场景,以便我了解数据如何在应用程序的各个部分之间流动。

我有一个使用 html 的简单登录页面:

<!DOCTYPE html>
<html>
<body>

<form action="***what-do-i-put-here***">
  <fieldset>
    <legend>Personal information:</legend>
    First name:<br>
    <input type="text" name="firstname" value="Mickey">
    <br>
    Last name:<br>
    <input type="text" name="lastname" value="Mouse">
    <br><br>
    <input type="submit" value="Submit">
  </fieldset>
</form>

</body>
</html>

我使用 python 默认存在的 simpleHTTpServer 运行它。

现在我创建了一个非常基本的猎鹰应用程序,只有一个响应者“on_post()”,它只回复从表单中收到的数据,

我在本地服务器上使用 uWsgi 来托管我的 falcon 应用程序。我如何使这两个不同的代码相互交互我的意思是在 html 表单中,如果是 Php,我们所做的是我们在“actions”标签下定义 php 文件的名称。我们如何做到这一点猎鹰。

非常感谢一个非常简单和小型的工作示例

【问题讨论】:

    标签: python html api falcon


    【解决方案1】:

    这是一个工作示例!

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
    </head>
    <body>
        <form action="http://127.0.0.1:8000" method="post">
            <input type="text" name="name">
            <button type="submit" name="btn">Submit</button>
        </form>
    </body>
    </html>
    

    猎鹰代码:

    import falcon
    from wsgiref import simple_server
    
    class Resource(object):
        def on_post(self, req, resp):
            resp.status = falcon.HTTP_200
            resp.body = req.params['name']
    
    app = api = falcon.API()
    app.req_options.auto_parse_form_urlencoded = True
    api.add_route('/', Resource())
    
    if __name__ == '__main__':
        http = simple_server.make_server('127.0.0.1', 8000, app)
        http.serve_forever()
    

    【讨论】:

    • 哇,我花了很长时间才找到api.req_options.auto_parse_form_urlencoded = True。谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-12-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多