关于python 3.x:TypeError:getresponse()获得了意外的关键字参数’buffering’

TypeError: getresponse() got an unexpected keyword argument 'buffering'

无法从Windows 7 32位操作系统上载。 在具有32/64位Python的Windows 7 64位OS上,它可以正常工作。 我正在将Python 3.4.3与最新的请求API配合使用。

我得到的错误是:

1
2
3
4
5
Traceback (most recent call last): File
"C:\Python34\lib\site-packages
equests\packages\urllib3\connectionpool.py", line 376, in _make_request
httplib_response = conn.getresponse(buffering=True)
TypeError: getresponse() got an unexpected keyword argument 'buffering'

在处理上述异常期间,发生了另一个异常:

1
2
3
4
5
6
7
8
9
10
11
Traceback (most recent call last): File
"C:\Python34\lib\site-packages
equests\packages\urllib3\connectionpool.py", line 559, in urlopen
body=body, headers=headers) File"C:\Python34\lib\site-packages
equests\packages\urllib3\connectionpool.py", line 378, in _make_request
httplib_response = conn.getresponse() File"C:\Python34\lib\http\client.py", line 1171, in getresponse
response.begin() File"C:\Python34\lib\http\client.py", line 351, in begin
version, status, reason = self._read_status() File"C:\Python34\lib\http\client.py", line 313, in _read_status
line = str(self.fp.readline(_MAXLINE + 1),"iso-8859-1") File"C:\Python34\lib\socket.py", line 374, in readinto
return self._sock.recv_into(b)
ConnectionResetError: WinError 10054] An existing connection was forcibly close d by the remote host

在处理上述异常期间,发生了另一个异常:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
Traceback (most recent call last): File"C:\Python34\lib\site-packages
equests\adapters.py", line 370, in send
timeout=timeout File"C:\Python34\lib\site-packages
equests\packages\urllib3\connectionpool.py", line 609, in urlopen
_stacktrace=sys.exc_info()[2]) File"C:\Python34\lib\site-packages
equests\packages\urllib3\util
etry.py", line 245, in increment
raise six.reraise(type(error), error, _stacktrace) File"C:\Python34\lib\site-packages
equests\packages\urllib3\packages\six.py", line 309, in reraise
raise value.with_traceback(tb) File"C:\Python34\lib\site-packages
equests\packages\urllib3\connectionpool.py", line 559, in urlopen
body=body, headers=headers) File"C:\Python34\lib\site-packages
equests\packages\urllib3\connectionpool.py", line 378, in _make_request
httplib_response = conn.getresponse() File"C:\Python34\lib\http\client.py", line 1171, in getresponse
response.begin() File"C:\Python34\lib\http\client.py", line 351, in begin
version, status, reason = self._read_status() File"C:\Python34\lib\http\client.py", line 313, in _read_status
line = str(self.fp.readline(_MAXLINE + 1),"iso-8859-1") File"C:\Python34\lib\socket.py", line 374, in readinto
return self._sock.recv_into(b)
requests.packages.urllib3.exceptions.ProtocolError: ('Connection aborted.', ConnectionResetError(10054, 'An existing connection was forcibly closed by the remote host', None, 10054, None))

在处理上述异常期间,发生了另一个异常:

1
2
3
4
5
6
7
8
9
10
11
12
13
Traceback (most recent call last): File"Upgrader.py", line 12, in
rdst = requests.post(urldst, files={'1_19_0_developer.exe': resp.content}) File"C:\Python34\lib\site-packages
equests\api.py", line 109, in post
return request('post', url, data=data, json=json, *kwargs) File"C:\Python34\lib\site-packages
equests\api.py", line 50, in request
response = session.request(method=method, url=url, *kwargs) File"C:\Python34\lib\site-packages
equests\sessions.py", line 468, in request
resp = self.send(prep, *send_kwargs) File"C:\Python34\lib\site-packages
equests\sessions.py", line 576, in send
r = adapter.send(request, *kwargs) File"C:\Python34\lib\site-packages
equests\adapters.py", line 412, in send
raise ConnectionError(err, request=request)
requests.exceptions.ConnectionError: ('Connection aborted.', ConnectionResetErro r(10054, 'An existing connection was forcibly closed by the remote host', None, 10054, None))

该代码是

1
2
3
4
5
6
7
8
9
import requests
from requests_file import FileAdapter

s = requests.Session()
s.mount('file://', FileAdapter())
resp = s.get('file:///local_package_name')
urldst = 'Upload URL'
rdst = requests.post(urldst, files={'filename': resp.content})
print(rdst)

该代码在Windows7 64位操作系统上可以正常工作,但返回Windows7 32位OS中所述的错误。

另外,我可以使用提供的代码在32位Windows 7操作系统上上传小型软件包。 唯一的问题是上传大软件包。


这是@fche正确答案的更多上下文。

来自请求维护者的评论总结了这里的情况。

This is an unforeseen problem to do with how exception tracebacks are being reported in Python 3. PEP 3134 introduced this 'chaining exceptions' reporting [...]. The purpose of this error reporting is to highlight that some exceptions occur in except blocks, and to work out what chain of exceptions was hit. This is potentially very useful: for instance, you can hit an exception after destroying a resource and then attempt to use that resource in the except block, which hits another exception. It's helpful to be able to see both exceptions at once.

The key is that the TypeError raised as the first exception is unrelated to the subsequent ones. In fact, that's the standard control flow in urllib3. This means that the real exception that's being raised here is the request.exceptions.ConnectionError exception that wraps the urllib3.exceptions.MaxRetryError exception being raised in urllib3.

This is not a Requests bug, it's just an ugly traceback introduced by Python 3. We can try to reduce the nastiness of it somewhat by refactoring the method in urllib3 [...], but that'll only remove the TypeError from the chain: the rest will stay in place.


忽略第一个(" buffering = True")异常。 那是内部向后兼容的产物。 真正的错误是随之而来的。