确定确切的路径,包括Flask + gevent.pywsgi WSGIServer服务器上是否存在尾随问号

时间:2019-08-11 07:21:04

标签: python http flask wsgi gevent

是否可以确定请求到服务器的路径,包括是否包含问号?应用程序

from gevent import monkey
monkey.patch_all()

import gevent
from gevent.pywsgi import WSGIServer
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)

server = WSGIServer(('0.0.0.0', 8081), app)
server.serve_forever()

总是导致

full_path:/? path:/ query_string:b'' url:http://localhost:8081/

如果要求其中一个

http://localhost:8081/?

http://localhost:8081/

在许多情况下,这似乎并不重要,但我正在进行具有多个重定向的身份验证流程,其中用户应以与开始时完全相同的URL结束。目前,我看不到任何方法可以确保Flask + gevent WSGIServer能够做到这一点。


这与Flask request: determine exact path, including if there is a question mark类似,但使用WSGIServer中的gevent.pywsgithe answer似乎不适用,因为request.environ没有键RAW_URIREQUEST_URI

1 个答案:

答案 0 :(得分:0)

有一种方法可以定义自定义handler_class / WSGIHandler并将self.path添加到request.environ

from gevent import monkey
monkey.patch_all()

import gevent
from gevent.pywsgi import WSGIHandler, WSGIServer
from flask import Flask, Response, request

def root():
    return Response(
        f'request_line_path:{request.environ["REQUEST_LINE_PATH"]}'
    )

class RequestLinePathHandler(WSGIHandler):
    def get_environ(self):
        return {
            **super().get_environ(),
            'REQUEST_LINE_PATH': self.path,
        }

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

server = WSGIServer(('0.0.0.0', 8081), app, handler_class=RequestLinePathHandler)
server.serve_forever()

所以对http://localhost:8081/的请求输出

request_line_path:/

和对http://localhost:8081/?输出的请求

request_line_path:/?
相关问题