2022-10-12- urllib3的bug

2022-10-12  本文已影响0人  破阵子沙场秋点兵

本本来自 https://www.jianshu.com/p/8cc54b849d1c,侵权请联系删除

python requests请求上传大文件报BrokenPipeError(32, 'Broken pipe')

[图片上传失败...(image-f187d5-1665571293697)]

Ivanlfli关注

<time datetime="2022-05-18T02:15:46.000Z" style="box-sizing: border-box; margin-right: 10px;">2022.05.18 10:15:46</time>字数 61阅读 325

<article class="_2rhmJa" style="box-sizing: border-box; display: block; font-weight: 400; line-height: 1.8; margin-bottom: 20px; word-break: break-word; position: relative; color: rgb(64, 64, 64); font-family: -apple-system, "system-ui", "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol", "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">

问题:

请求上传大文件报BrokenPipeError(32, 'Broken pipe')

Traceback (most recent call last):
  File "/Users/ivanli/git/own/ci-test/lib/python3.8/site-packages/urllib3/connectionpool.py", line 670, in urlopen
    httplib_response = self._make_request(
  File "/Users/ivanli/git/own/ci-test/lib/python3.8/site-packages/urllib3/connectionpool.py", line 392, in _make_request
    conn.request(method, url, **httplib_request_kw)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/http/client.py", line 1252, in request
    self._send_request(method, url, body, headers, encode_chunked)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/http/client.py", line 1298, in _send_request
    self.endheaders(body, encode_chunked=encode_chunked)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/http/client.py", line 1247, in endheaders
    self._send_output(message_body, encode_chunked=encode_chunked)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/http/client.py", line 1046, in _send_output
    self.send(chunk)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/http/client.py", line 968, in send
    self.sock.sendall(data)
BrokenPipeError: [Errno 32] Broken pipe

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/ivanli/git/own/ci-test/lib/python3.8/site-packages/requests/adapters.py", line 440, in send
    resp = conn.urlopen(
  File "/Users/ivanli/git/own/ci-test/lib/python3.8/site-packages/urllib3/connectionpool.py", line 726, in urlopen
    retries = retries.increment(
  File "/Users/ivanli/git/own/ci-test/lib/python3.8/site-packages/urllib3/util/retry.py", line 410, in increment
    raise six.reraise(type(error), error, _stacktrace)
  File "/Users/ivanli/git/own/ci-test/lib/python3.8/site-packages/urllib3/packages/six.py", line 734, in reraise
    raise value.with_traceback(tb)
  File "/Users/ivanli/git/own/ci-test/lib/python3.8/site-packages/urllib3/connectionpool.py", line 670, in urlopen
    httplib_response = self._make_request(
  File "/Users/ivanli/git/own/ci-test/lib/python3.8/site-packages/urllib3/connectionpool.py", line 392, in _make_request
    conn.request(method, url, **httplib_request_kw)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/http/client.py", line 1252, in request
    self._send_request(method, url, body, headers, encode_chunked)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/http/client.py", line 1298, in _send_request
    self.endheaders(body, encode_chunked=encode_chunked)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/http/client.py", line 1247, in endheaders
    self._send_output(message_body, encode_chunked=encode_chunked)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/http/client.py", line 1046, in _send_output
    self.send(chunk)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/http/client.py", line 968, in send
    self.sock.sendall(data)
urllib3.exceptions.ProtocolError: ('Connection aborted.', BrokenPipeError(32, 'Broken pipe'))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/ivanli/git/own/ci-test/tencent/pic.py", line 216, in <module>
    run()
  File "/Users/ivanli/git/own/ci-test/tencent/pic.py", line 211, in run
    res = CIApi().req(uri=f"/{fname}", method="put", body=res_body, headers=headers)
  File "/Users/ivanli/git/own/ci-test/tencent/pic.py", line 182, in req
    rsp = HttpReq(method=method, url=url, data=body, headers=headers).req()
  File "/Users/ivanli/git/own/ci-test/tencent/pic.py", line 48, in req
    return s.send(s.prepare_request(self))
  File "/Users/ivanli/git/own/ci-test/lib/python3.8/site-packages/requests/sessions.py", line 645, in send
    r = adapter.send(request, **kwargs)
  File "/Users/ivanli/git/own/ci-test/lib/python3.8/site-packages/requests/adapters.py", line 501, in send
    raise ConnectionError(err, request=request)
requests.exceptions.ConnectionError: ('Connection aborted.', BrokenPipeError(32, 'Broken pipe'))

Process finished with exit code 1

解决:

原因是用的urllib3库版本是1.25.x,这个版本有bug,需要升到1.26.x
https://github.com/urllib3/urllib3/pull/1524

image

</article>

上一篇下一篇

猜你喜欢

热点阅读