我想检测,如果该请求来自来到localhost:5000
或foo.herokuapp.com
被请求的主机和路径是什么。 我如何获得有关瓶的请求这些信息?
Answer 1:
您可以检查通过几个URL Request
的字段 :
用户请求以下网址:
http://www.example.com/myapplication/page.html?x=y
在这种情况下,上面提到的属性的值将是以下内容:
path /page.html script_root /myapplication base_url http://www.example.com/myapplication/page.html url http://www.example.com/myapplication/page.html?x=y url_root http://www.example.com/myapplication/
您可以轻松地提取与适当的拆分主机部分。
Answer 2:
另一个例子:
请求:
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
Answer 3:
你应该试试:
request.url
它假设总是工作,即使在本地主机上(只是做了它)。
文章来源: How do I get the different parts of a Flask request's url?