Answers:
您可以通过以下几个Request
字段检查网址:
用户请求以下URL:
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/
您可以通过适当的拆分轻松提取主体部分。
http://www.example.com/
而不是http://www.example.com/myapplication/
base_url返回http://www.example.com/myapplication/
另一个例子:
请求:
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
如果您使用的是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')
Request.root_url
,作为回报,我只能得到<werkzeug.utils.cached_property object>
而不是很好的格式化http://www.example.com/myapplication/
。还是此功能在localhost上不起作用?