【问题标题】:How do I get the different parts of a Flask request's url?如何获取 Flask 请求的 url 的不同部分?
【发布时间】:2013-04-05 04:18:51
【问题描述】:

我想检测请求是否来自localhost:5000foo.herokuapp.com 主机以及请求的路径。如何获取有关 Flask 请求的信息?

【问题讨论】:

    标签: python url flask


    【解决方案1】:

    您可以通过多个Request 字段检查网址:

    假设您的应用程序正在侦听以下应用程序根目录:

    http://www.example.com/myapplication
    

    并且用户请求以下 URI:

    http://www.example.com/myapplication/foo/page.html?x=y
    

    在这种情况下,上述属性的值如下:

        path             /foo/page.html
        full_path        /foo/page.html?x=y
        script_root      /myapplication
        base_url         http://www.example.com/myapplication/foo/page.html
        url              http://www.example.com/myapplication/foo/page.html?x=y
        url_root         http://www.example.com/myapplication/
    

    您可以通过适当的拆分轻松提取主机部分。

    【讨论】:

    • 我想得到Request.root_url,作为回报,我只得到<werkzeug.utils.cached_property object>,而不是格式很好的http://www.example.com/myapplication/。或者这个功能在 localhost 上不起作用?
    • @Vadim 你应该使用 request.root_url,而不是 Request.root_url。
    • Flask 新手,我不知道请求对象来自哪里以及它是如何工作的,这里是:flask.pocoo.org/docs/0.12/reqcontext
    • request.url_root 对我有用,而 request.root_url 和 Request.root_url 失败。因此,请注意 cap 'R' 和 url_root 与 root_url
    • url_root 返回http://www.example.com/ 而不是http://www.example.com/myapplication/ base_url 返回http://www.example.com/myapplication/
    【解决方案2】:

    如果您使用的是 Python,我建议您探索请求对象:

    dir(request)

    由于对象支持dict方法:

    request.__dict__

    可以打印或保存。我用它在 Flask 中记录 404 代码:

    @app.errorhandler(404)
    def not_found(e):
        with open("./404.csv", "a") as f:
            f.write(f'{datetime.datetime.now()},{request.__dict__}\n')
        return send_file('static/images/Darknet-404-Page-Concept.png', mimetype='image/png')
    

    【讨论】:

      【解决方案3】:

      另一个例子:

      请求:

      curl -XGET http://127.0.0.1:5000/alert/dingding/test?x=y
      

      然后:

      request.method:              GET
      request.url:                 http://127.0.0.1:5000/alert/dingding/test?x=y
      request.base_url:            http://127.0.0.1:5000/alert/dingding/test
      request.url_charset:         utf-8
      request.url_root:            http://127.0.0.1:5000/
      str(request.url_rule):       /alert/dingding/test
      request.host_url:            http://127.0.0.1:5000/
      request.host:                127.0.0.1:5000
      request.script_root:
      request.path:                /alert/dingding/test
      request.full_path:           /alert/dingding/test?x=y
      
      request.args:                ImmutableMultiDict([('x', 'y')])
      request.args.get('x'):       y
      

      【讨论】:

      • 这应该是公认的答案,因为它有更多的细节。
      • 这是一个很好的答案。非常有用且完整。
      • 想使用request.full_path的朋友,建议改用request.environ['RAW_URI']。因为,当实际的完整查询路径为/alert/dingding/test时,request.full_path返回/alert/dingding/test?,结果会多加一个问号,这可能是不可取的。
      • 谢谢!这些详细的信息帮助很大。真的很感激。
      • 这是一个很棒且非常详细的答案
      【解决方案4】:

      你应该试试:

      request.url 
      

      它应该一直工作,即使在本地主机上(刚刚做到了)。

      【讨论】:

        猜你喜欢
        • 2015-04-20
        • 1970-01-01
        • 2021-07-18
        • 1970-01-01
        • 2013-05-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-03-22
        相关资源
        最近更新 更多