Connection broken: IncompleteRead(1394 bytes read)', IncompleteRead(1394 bytes read)
原因:网上说此问题出现的原因是服务器的请求只支持HTTP/1.0,urllib3中使用了chunk,但是chunk在HTTP/1.1中才会有,传输还没完成连接就中断了,所以报错
解决:
# 引入 http.client
import http.client
# 配置如下
http.client.HTTPConnection._http_vsn = 10
http.client.HTTPConnection._http_vsn_str = 'HTTP/1.0'
# 其他那些改代码什么都是不切实际的
这里注意的是python3以前http.client叫httplib
完整错误如下:
Traceback (most recent call last):
File "/usr/python/lib/python3.7/http/client.py", line 544, in _get_chunk_left
chunk_left = self._read_next_chunk_size()
File "/usr/python/lib/python3.7/http/client.py", line 511, in _read_next_chunk_size
return int(line, 16)
ValueError: invalid literal for int() with base 16: b''
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/usr/python/lib/python3.7/http/client.py", line 561, in _readall_chunked
chunk_left = self._get_chunk_left()
File "/usr/python/lib/python3.7/http/client.py", line 546, in _get_chunk_left
raise IncompleteRead(b'')
http.client.IncompleteRead: IncompleteRead(0 bytes read)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/home/luopan/PycharmProjects/AlibabaTmkApi/venv/lib/python3.7/site-packages/urllib3/response.py", line 425, in _error_catcher
yield
File "/home/luopan/PycharmProjects/AlibabaTmkApi/venv/lib/python3.7/site-packages/urllib3/response.py", line 503, in read
data = self._fp.read() if not fp_closed else b""
File "/usr/python/lib/python3.7/http/client.py", line 454, in read
return self._readall_chunked()
File "/usr/python/lib/python3.7/http/client.py", line 568, in _readall_chunked
raise IncompleteRead(b''.join(value))
http.client.IncompleteRead: IncompleteRead(1394 bytes read)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/home/luopan/PycharmProjects/AlibabaTmkApi/src/tmklogin.py", line 92, in <module>
do_upload()
File "/home/luopan/PycharmProjects/AlibabaTmkApi/src/tmklogin.py", line 64, in do_upload
respond = https.request(method='POST', url=url, headers=headers, multipart_boundary=boundary, fields=field)
File "/home/luopan/PycharmProjects/AlibabaTmkApi/venv/lib/python3.7/site-packages/urllib3/request.py", line 80, in request
method, url, fields=fields, headers=headers, **urlopen_kw
File "/home/luopan/PycharmProjects/AlibabaTmkApi/venv/lib/python3.7/site-packages/urllib3/request.py", line 171, in request_encode_body
return self.urlopen(method, url, **extra_kw)
File "/home/luopan/PycharmProjects/AlibabaTmkApi/venv/lib/python3.7/site-packages/urllib3/poolmanager.py", line 330, in urlopen
response = conn.urlopen(method, u.request_uri, **kw)
File "/home/luopan/PycharmProjects/AlibabaTmkApi/venv/lib/python3.7/site-packages/urllib3/connectionpool.py", line 720, in urlopen
method, url, error=e, _pool=self, _stacktrace=sys.exc_info()[2]
File "/home/luopan/PycharmProjects/AlibabaTmkApi/venv/lib/python3.7/site-packages/urllib3/util/retry.py", line 400, in increment
raise six.reraise(type(error), error, _stacktrace)
File "/home/luopan/PycharmProjects/AlibabaTmkApi/venv/lib/python3.7/site-packages/urllib3/packages/six.py", line 735, in reraise
raise value
File "/home/luopan/PycharmProjects/AlibabaTmkApi/venv/lib/python3.7/site-packages/urllib3/connectionpool.py", line 690, in urlopen
**response_kw
File "/home/luopan/PycharmProjects/AlibabaTmkApi/venv/lib/python3.7/site-packages/urllib3/response.py", line 597, in from_httplib
**response_kw
File "/home/luopan/PycharmProjects/AlibabaTmkApi/venv/lib/python3.7/site-packages/urllib3/response.py", line 258, in __init__
self._body = self.read(decode_content=decode_content)
File "/home/luopan/PycharmProjects/AlibabaTmkApi/venv/lib/python3.7/site-packages/urllib3/response.py", line 529, in read
raise IncompleteRead(self._fp_bytes_read, self.length_remaining)
File "/usr/python/lib/python3.7/contextlib.py", line 130, in __exit__
self.gen.throw(type, value, traceback)
File "/home/luopan/PycharmProjects/AlibabaTmkApi/venv/lib/python3.7/site-packages/urllib3/response.py", line 443, in _error_catcher
raise ProtocolError("Connection broken: %r" % e, e)
urllib3.exceptions.ProtocolError: ('Connection broken: IncompleteRead(1394 bytes read)', IncompleteRead(1394 bytes read))