6

我正在使用Flask-uploads将文件上传到我的 Flask 服务器。允许的最大大小是通过使用设置的flaskext.uploads.patch_request_class(app, 16 * 1024 * 1024)

我的客户端应用程序(单元测试)使用请求来发布一个很大的文件。

我可以看到我的服务器返回一个带有 status 的 HTTP 响应413: Request Entity Too Large但是客户端在请求代码中引发了异常

ConnectionError: HTTPConnectionPool(host='api.example.se', port=80): Max retries exceeded with url: /images (Caused by <class 'socket.error'>: [Errno 32] Broken pipe)

我的猜测是服务器断开接收套接字并将响应发送回客户端。但是当客户端收到一个损坏的发送套接字时,它会引发异常并跳过响应。

问题:

  • 我对 Flask-Uploads 和 requests 的猜测是否正确?
  • Flask-Uploads 和 request 处理 413 错误是否正确?
  • 当帖子很大时,我是否应该期望我的客户端代码返回一些 html?

更新

这是一个重现我的问题的简单示例。

服务器.py

from flask import Flask, request
app = Flask(__name__)
app.config['MAX_CONTENT_LENGTH'] = 1024

@app.route('/post', methods=('POST',))
def view_post():
    return request.data

app.run(debug=True)

客户端.py

from tempfile import NamedTemporaryFile
import requests

def post(size):
    print "Post with size %s" % size,
    f = NamedTemporaryFile(delete=False, suffix=".jpg")
    for i in range(0, size):
        f.write("CoDe")
    f.close()

    # Post
    files = {'file': ("tempfile.jpg", open(f.name, 'rb'))}
    r = requests.post("http://127.0.0.1:5000/post", files=files)
    print "gives status code = %s" % r.status_code

post(16)
post(40845)
post(40846)

客户的结果

Post with size 16 gives status code = 200
Post with size 40845 gives status code = 413
Post with size 40846
Traceback (most recent call last):
  File "client.py", line 18, in <module>
    post(40846)
  File "client.py", line 13, in post
    r = requests.post("http://127.0.0.1:5000/post", files=files)
  File "/opt/python_env/renter/lib/python2.7/site-packages/requests/api.py", line 88, in post
    return request('post', url, data=data, **kwargs)
  File "/opt/python_env/renter/lib/python2.7/site-packages/requests/api.py", line 44, in request
    return session.request(method=method, url=url, **kwargs)
  File "/opt/python_env/renter/lib/python2.7/site-packages/requests/sessions.py", line 357, in request
    resp = self.send(prep, **send_kwargs)
  File "/opt/python_env/renter/lib/python2.7/site-packages/requests/sessions.py", line 460, in send
    r = adapter.send(request, **kwargs)
  File "/opt/python_env/renter/lib/python2.7/site-packages/requests/adapters.py", line 354, in send
    raise ConnectionError(e)
requests.exceptions.ConnectionError: HTTPConnectionPool(host='127.0.0.1', port=5000): Max retries exceeded with url: /post (Caused by <class 'socket.error'>: [Errno 32] Broken pipe)

我的版本

$ pip freeze
Flask==0.10.1
Flask-Mail==0.9.0
Flask-SQLAlchemy==1.0
Flask-Uploads==0.1.3
Jinja2==2.7.1
MarkupSafe==0.18
MySQL-python==1.2.4
Pillow==2.1.0
SQLAlchemy==0.8.2
Werkzeug==0.9.4
blinker==1.3
itsdangerous==0.23
passlib==1.6.1
python-dateutil==2.1
requests==2.0.0
simplejson==3.3.0
six==1.4.1
virtualenv==1.10.1
voluptuous==0.8.1
wsgiref==0.1.2
4

3 回答 3

7

Flask 正在关闭连接,您可以为 413 错误设置错误处理程序:

@app.errorhandler(413)
def request_entity_too_large(error):
    return 'File Too Large', 413

现在客户端应该得到一个 413 错误,注意我没有测试这段代码。

更新:

我尝试重新创建 413 错误,但没有收到 ConnectionError 异常。

这是一个简单的例子:

from flask import Flask, request

app = Flask(__name__)

app.config['MAX_CONTENT_LENGTH'] = 1024


@app.route('/post', methods=('POST',))
def view_post():
    return request.data


app.run(debug=True)

运行文件后,我使用终端测试请求和发送大数据:

>>> import requests
>>> r = requests.post('http://127.0.0.1:5000/post', data={'foo': 'a'})
>>> r
<Response [200]>
>>> r = requests.post('http://127.0.0.1:5000/post', data={'foo': 'a'*10000})
>>> r
<Response [413]>
>>> r.status_code
413
>>> r.content
'<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">\n<title>413 Request Entity Too Large</title
>\n<h1>Request Entity Too Large</h1>\n<p>The data value transmitted exceeds the capacity limit.</p>\n'

如您所见,我们收到了来自烧瓶 413 错误的响应,并且请求没有引发异常。

顺便说一句,我正在使用:

  • 烧瓶:0.10.1
  • 请求:2.0.0
于 2013-10-19T15:29:03.543 回答
4

HTTP 1.1 规范RFC 2616说:

10.4.14 413 请求实体太大

服务器拒绝处理请求,因为请求
实体大于服务器愿意或能够处理的大小。服务器可以关闭连接以
阻止客户端继续请求。

如果条件是临时的,服务器应该包含一个 Retry-
After 头域来指示它是临时的,并且在什么
时间之后客户端可以重试。

这就是这里发生的事情:flask 正在关闭连接以防止客户端继续上传,这会给您带来Broken pipe错误。

于 2013-10-18T21:24:54.570 回答
0

基于这个 github 问题的答案(https://github.com/benoitc/gunicorn/issues/1733#issuecomment-377000612

@app.before_request
def handle_chunking():
    """
    Sets the "wsgi.input_terminated" environment flag, thus enabling
    Werkzeug to pass chunked requests as streams.  The gunicorn server
    should set this, but it's not yet been implemented.
    """

    transfer_encoding = request.headers.get("Transfer-Encoding", None)
    if transfer_encoding == u"chunked":
        request.environ["wsgi.input_terminated"] = True
于 2019-12-16T07:28:06.627 回答