首页 > 解决方案 > 上传文件时,请求模块仅在 virtualenv 中显示错误

问题描述

我有一个非常简单的 python 脚本test.py

import requests
f = open('test.txt', 'rb')
r = requests.post('http://localhost:5000/uploadFile', files={'file': f})
print(r.text)

它应该将文件“test.txt”发送到我正在运行烧瓶应用程序的本地主机。这是代码flaskapp.py

app = Flask(__name__)
app.config['MAX_CONTENT_LENGTH'] = 3 * 1024 * 1024 * 1024

@app.route("/uploadFile", methods=['POST'])
def uploadFile():
    return "ok"

if __name__ == "__main__":
    port = int(os.environ.get("PORT", 5000))
    app.run(host='0.0.0.0', port=port, threaded=True)

它实际上只是返回“ok”。

当我test.py正常运行时,它会按预期打印“ok”。但是,当我激活 virtualenv 并运行相同的代码时,会出现此错误消息

Traceback (most recent call last):
  File "G:\Felipe\Projetos\host-python\venv-teste\lib\site-packages\urllib3\response.py", line 437, in _error_catcher
    yield
  File "G:\Felipe\Projetos\host-python\venv-teste\lib\site-packages\urllib3\response.py", line 519, in read
    data = self._fp.read(amt) if not fp_closed else b""
  File "c:\users\fsvic\appdata\local\programs\python\python37\Lib\http\client.py", line 457, in read
    n = self.readinto(b)
  File "c:\users\fsvic\appdata\local\programs\python\python37\Lib\http\client.py", line 501, in readinto
    n = self.fp.readinto(b)
  File "c:\users\fsvic\appdata\local\programs\python\python37\Lib\socket.py", line 589, in readinto
    return self._sock.recv_into(b)
ConnectionAbortedError: [WinError 10053] An established connection was aborted by the software in your host machine

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "G:\Felipe\Projetos\host-python\venv-teste\lib\site-packages\requests\models.py", line 751, in generate
    for chunk in self.raw.stream(chunk_size, decode_content=True):
  File "G:\Felipe\Projetos\host-python\venv-teste\lib\site-packages\urllib3\response.py", line 576, in stream
    data = self.read(amt=amt, decode_content=decode_content)
  File "G:\Felipe\Projetos\host-python\venv-teste\lib\site-packages\urllib3\response.py", line 541, in read
    raise IncompleteRead(self._fp_bytes_read, self.length_remaining)
  File "c:\users\fsvic\appdata\local\programs\python\python37\Lib\contextlib.py", line 130, in __exit__
    self.gen.throw(type, value, traceback)
  File "G:\Felipe\Projetos\host-python\venv-teste\lib\site-packages\urllib3\response.py", line 455, in _error_catcher
    raise ProtocolError("Connection broken: %r" % e, e)
urllib3.exceptions.ProtocolError: ("Connection broken: ConnectionAbortedError(10053, 'An established connection was aborted by the software in your host machine', None, 10053, None)", ConnectionAbortedError(10053, 'An established connection was aborted by the software in your host machine', None, 10053, None))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "teste.py", line 13, in <module>
    r = requests.post('http://localhost:5000/uploadFile', files={'file': f})
  File "G:\Felipe\Projetos\host-python\venv-teste\lib\site-packages\requests\api.py", line 119, in post
    return request('post', url, data=data, json=json, **kwargs)
  File "G:\Felipe\Projetos\host-python\venv-teste\lib\site-packages\requests\api.py", line 61, in request
    return session.request(method=method, url=url, **kwargs)
  File "G:\Felipe\Projetos\host-python\venv-teste\lib\site-packages\requests\sessions.py", line 530, in request
    resp = self.send(prep, **send_kwargs)
  File "G:\Felipe\Projetos\host-python\venv-teste\lib\site-packages\requests\sessions.py", line 683, in send
    r.content
  File "G:\Felipe\Projetos\host-python\venv-teste\lib\site-packages\requests\models.py", line 829, in content
    self._content = b''.join(self.iter_content(CONTENT_CHUNK_SIZE)) or b''
  File "G:\Felipe\Projetos\host-python\venv-teste\lib\site-packages\requests\models.py", line 754, in generate
    raise ChunkedEncodingError(e)
requests.exceptions.ChunkedEncodingError: ("Connection broken: ConnectionAbortedError(10053, 'An established connection was aborted by the software in your host machine', None, 10053, None)", ConnectionAbortedError(10053, 'An established connection was aborted by the software in your host machine', None, 10053, None))

在 virtualenv 中执行pip list检索以下模块

Package    Version
---------- ----------
certifi    2020.4.5.1
chardet    3.0.4
idna       2.9
pip        20.1.1
requests   2.23.0
setuptools 47.1.1
urllib3    1.25.9
wheel      0.34.2

出于某种原因,如果test.txt小于 750 KB,则 virtualenv 可以完美运行。

为什么会这样?

标签: pythonfileflaskpython-requestsvirtualenv

解决方案


推荐阅读