python - Flask 请求 : determine exact path, 包括是否有问号

标签 python http flask oauth query-string

有没有办法确定向服务器请求的路径是什么,包括它是否包含问号?应用

from flask import Flask, Response, request

def root():
    return Response(
        f'full_path:{request.full_path} '
        f'path:{request.path} '
        f'query_string:{request.query_string} '
        f'url:{request.url}'
    )

app = Flask('app')
app.add_url_rule('/', view_func=root)

app.run(host='0.0.0.0', port=8081, debug=False)

总是导致

完整路径:/? path:/query_string:b'' url:http://localhost:8081/

如果有要求

http://localhost:8081/?

http://localhost:8081/

这在很多情况下似乎并不重要,但我正在使用多个重定向进行身份验证流程,其中用户最终会到达与他们开始时完全相同的 URL。目前,我看不出有什么方法可以确保 Flask 发生这种情况。

最佳答案

除了您已经提到的字段外,“环境”字段中的值可能对您的情况有用:

def root():
    return Response(
        f'raw_uri:{request.environ["RAW_URI"]} '
        f'request_uri:{request.environ["REQUEST_URI"]}'
    )

输入:

http://localhost:8081/?

输出:

raw_uri:/?
request_uri:/?

输入:

http://localhost:8081/

输出:

raw_uri:/
request_uri:/

关于python - Flask 请求 : determine exact path, 包括是否有问号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57440598/

相关文章:

performance - http.Handle wrapper pattern -> 堆栈会膨胀吗?

python - IndexError : too many indices. 1 行 2 列的 Numpy 数组

python - Python中对象的 boolean 值

python - 使用 SQL Alchemy 列出现有架构中表中的列

python - 如何为 PyPI 组织 Python 模块以支持 2.x 和 3.x

c - C中的HTTP/1.0服务器,如何向客户端发送大文件?

python - 如何使用 HTTP 服务器从客户端获取输入并对其进行操作

python - 使用 flask-wtforms 输出类型 ="number"

javascript - 无法使用 backbone.js 向 flask-restful API 发出请求

node.js - 在 nodejs child_process 中将 flask 应用程序作为可执行文件执行