gpt4 book ai didi

python - 如何获取 Flask 请求的 url 的不同部分?

转载 作者:IT老高 更新时间:2023-10-28 21:06:30 25 4
gpt4 key购买 nike

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

最佳答案

您可以通过几个Request 来查看网址。字段:

Imagine your application is listening on the following application root:

http://www.example.com/myapplication

And a user requests the following URI:

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

In this case the values of the above mentioned attributes would be the following:

    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/

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

一个使用这个的例子:

from flask import request

@app.route('/')
def index():
return request.base_url

关于python - 如何获取 Flask 请求的 url 的不同部分?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15974730/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com