Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

python-3.x

Cannot Upload from a Windows 7 32 bit OS. It works fine on Windows 7 64 bit OS with 32/64 bit Python. I am using Python 3.4.3 with latest requests API.

The error I get is:

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

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File
"C:\Python34\lib\site-packages\requests\packages\urllib3\connectionpool.py", line 559, in urlopen
body=body, headers=headers) File "C:\Python34\lib\site-packages\requests\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

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File "C:\Python34\lib\site-packages\requests\adapters.py", line 370, in send
timeout=timeout File "C:\Python34\lib\site-packages\requests\packages\urllib3\connectionpool.py", line 609, in urlopen
_stacktrace=sys.exc_info()[2]) File "C:\Python34\lib\site-packages\requests\packages\urllib3\util\retry.py", line 245, in increment
raise six.reraise(type(error), error, _stacktrace) File "C:\Python34\lib\site-packages\requests\packages\urllib3\packages\six.py", line 309, in reraise
raise value.with_traceback(tb) File "C:\Python34\lib\site-packages\requests\packages\urllib3\connectionpool.py", line 559, in urlopen
body=body, headers=headers) File "C:\Python34\lib\site-packages\requests\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))

During handling of the above exception, another exception occurred:

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\requests\api.py", line 109, in post
return request('post', url, data=data, json=json, *kwargs) File "C:\Python34\lib\site-packages\requests\api.py", line 50, in request
response = session.request(method=method, url=url, *kwargs) File "C:\Python34\lib\site-packages\requests\sessions.py", line 468, in request
resp = self.send(prep, *send_kwargs) File "C:\Python34\lib\site-packages\requests\sessions.py", line 576, in send
r = adapter.send(request, *kwargs) File "C:\Python34\lib\site-packages\requests\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))

The code is

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)

This code works fine on a Windows7 64 bit OS, but returns the error as described in Windows7 32 bit OS.

Also, I can upload small packages using the provided code on a 32 bit Windows 7 OS. The only problem is with uploading large packages.

like image 452
srujan Avatar asked Nov 18 '15 23:11

srujan


2 Answers

Ignore the first ("buffering=True") exception. That's an internal backward compatibility artefact. The real errors are the ones that follow.

like image 144
fche Avatar answered Oct 16 '22 21:10

fche


Here's a little more context for @fche's correct answer.

This comment from a Requests maintainer sums up what is going on here.

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.

like image 41
Christian Long Avatar answered Oct 16 '22 20:10

Christian Long