经测试单独使用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
下面是我初始化文件完整内容:


测试发现,单独使用Flask 1.0.2版本时,用request.stream.read()可获取Transfer-Encoding为chunked的数据。但使用Gunicorn后,request.data值为空,无法获取chunked数据。文中给出了在Flask app对象创建后添加代码的解决方案。
4319

被折叠的 条评论
为什么被折叠?



