解决gunicorn+flask不能接收chunked的数据

经测试单独使用flask(1.0.2版本)的时候,使用request.stream.read()能够获取到Transfer-Encoding为chunked的数据。

当使用gunicorn后,request.data值为空,不能获取到chunked数据。

解决方案:

在flask app对象创建后添加如下代码:

@app.before_request
def handle_chunking():
    """
    Sets the "wsgi.input_terminated" environment flag, thus enabling
    Werkzeug to pass chunked requests as streams; this makes the API
    compliant with the HTTP/1.1 standard.  The gunicorn server should set
    the flag, but this feature has not been implemented.
    """
    transfer_encoding = request.headers.get("Transfer-Encoding", None)
    if transfer_encoding == "chunked":
        request.environ["wsgi.input_terminated"] = True

 

下面是我初始化文件完整内容:

解决gunicorn+flask不能接收chunked的数据_第1张图片

你可能感兴趣的:(python,gunicorn,flask,gunicorn,flask,chunked)