Python 3.3 requests issue

I'm getting a strange error when using Requests in Python 3.3 (other flavors of Python  3 do not get this error):

```
Traceback (most recent call last):
  File "C:\Python33\lib\site-packages\requests\packages\urllib3\connectionpool.py", line 421, in urlopen
    body=body, headers=headers)
  File "C:\Python33\lib\site-packages\requests\packages\urllib3\connectionpool.py", line 273, in _make_request
    conn.request(method, url, **httplib_request_kw)
  File "C:\Python33\lib\http\client.py", line 1049, in request
    self._send_request(method, url, body, headers)
  File "C:\Python33\lib\http\client.py", line 1087, in _send_request
    self.endheaders(body)
  File "C:\Python33\lib\http\client.py", line 1045, in endheaders
    self._send_output(message_body)
  File "C:\Python33\lib\http\client.py", line 890, in _send_output
    self.send(msg)
  File "C:\Python33\lib\http\client.py", line 828, in send
    self.connect()
  File "C:\Python33\lib\site-packages\requests\packages\urllib3\connectionpool.py", line 104, in connect
    ssl_version=resolved_ssl_version)
  File "C:\Python33\lib\site-packages\requests\packages\urllib3\util.py", line 329, in ssl_wrap_socket
    return context.wrap_socket(sock, server_hostname=server_hostname)
  File "C:\Python33\lib\ssl.py", line 210, in wrap_socket
    _context=self)
  File "C:\Python33\lib\ssl.py", line 310, in __init__
    raise x
  File "C:\Python33\lib\ssl.py", line 306, in __init__
    self.do_handshake()
  File "C:\Python33\lib\ssl.py", line 513, in do_handshake
    self._sslobj.do_handshake()
ConnectionResetError: [WinError 10054] An existing connection was forcibly closed by the remote host

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Python33\lib\site-packages\requests\adapters.py", line 211, in send
    timeout=timeout
  File "C:\Python33\lib\site-packages\requests\packages\urllib3\connectionpool.py", line 465, in urlopen
    raise MaxRetryError(self, url, e)
requests.packages.urllib3.exceptions.MaxRetryError: HTTPSConnectionPool(host='analysiscenter.veracode.com', port=443): Max retries exceeded with url: /api/2.0/getappbuilds.do (Caused by <class 'ConnectionResetError'>: [WinError 10054] An existing connection was forcibly closed by the remote host)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\tfs\Vertafore_TFSDev\CQ\python\veracode\pythontestscriptonation.py", line 33, in <module>
    print(v.get_app_builds())
  File "C:\tfs\Vertafore_TFSDev\CQ\python\veracode\apiwrapper.py", line 184, in get_app_builds
    {}
  File "C:\tfs\Vertafore_TFSDev\CQ\python\veracode\apiwrapper.py", line 57, in request
    r = requests.get(URL, params=data, auth=username_password)
  File "C:\Python33\lib\site-packages\requests\api.py", line 55, in get
    return request('get', url, **kwargs)
  File "C:\Python33\lib\site-packages\requests\api.py", line 44, in request
    return session.request(method=method, url=url, **kwargs)
  File "C:\Python33\lib\site-packages\requests\sessions.py", line 354, in request
    resp = self.send(prep, **send_kwargs)
  File "C:\Python33\lib\site-packages\requests\sessions.py", line 460, in send
    r = adapter.send(request, **kwargs)
  File "C:\Python33\lib\site-packages\requests\adapters.py", line 246, in send
    raise ConnectionError(e)
requests.exceptions.ConnectionError: HTTPSConnectionPool(host='analysiscenter.veracode.com', port=443): Max retries exceeded with url: /api/2.0/getappbuilds.do (Caused by <class 'ConnectionResetError'>: [WinError 10054] An existing connection was forcibly closed by the remote host)
[Finished in 61.0s with exit code 1]
```

This automation has run for months on Python 3.2 and these errors have never occurred. I don't really know enough about requests to investigate this issue, but I'd be happy to help recreate the issue or debug if someone else can. Perhaps there's a bug in how Requests is handling HTTPS requests with Python 3.3? (Did 3.3 change how urllib works?...I don't know offhand...)

Again, I'm not getting any of these issues in Python 3.2 or Python 3.1. Please help! :)


Please reply with the following: `python -c 'import requests; print(requests.__version__)'`


Requests version 1.1.0.


@t-8ch can explain this in detail but upgrading to 1.2.0 should fix all of your problems.


Excellent! I'll test it out. Thank you for your quick response. :)


If it doesn't fix it for you, feel free to re-open the issue or ask me to do it for you.


I'm still having the same failure, no matter where I run it.

```
C:\Program Files (x86)\Jenkins\jobs\python-test1\workspace>python -V 
Python 3.3.0
```

```
C:\Program Files (x86)\Jenkins\jobs\python-test1\workspace>exit 0 
[workspace] $ python C:\Windows\TEMP\hudson581448604896317111.py
Requests Version: 1.2.0
Traceback (most recent call last):
  File "C:\Python33\lib\site-packages\requests\packages\urllib3\connectionpool.py", line 421, in urlopen
    body=body, headers=headers)
  File "C:\Python33\lib\site-packages\requests\packages\urllib3\connectionpool.py", line 273, in _make_request
    conn.request(method, url, **httplib_request_kw)
  File "C:\Python33\lib\http\client.py", line 1049, in request
    self._send_request(method, url, body, headers)
  File "C:\Python33\lib\http\client.py", line 1087, in _send_request
    self.endheaders(body)
  File "C:\Python33\lib\http\client.py", line 1045, in endheaders
    self._send_output(message_body)
  File "C:\Python33\lib\http\client.py", line 890, in _send_output
    self.send(msg)
  File "C:\Python33\lib\http\client.py", line 828, in send
    self.connect()
  File "C:\Python33\lib\site-packages\requests\packages\urllib3\connectionpool.py", line 104, in connect
    ssl_version=resolved_ssl_version)
  File "C:\Python33\lib\site-packages\requests\packages\urllib3\util.py", line 329, in ssl_wrap_socket
    return context.wrap_socket(sock, server_hostname=server_hostname)
  File "C:\Python33\lib\ssl.py", line 210, in wrap_socket
    _context=self)
  File "C:\Python33\lib\ssl.py", line 310, in __init__
    raise x
  File "C:\Python33\lib\ssl.py", line 306, in __init__
    self.do_handshake()
  File "C:\Python33\lib\ssl.py", line 513, in do_handshake
    self._sslobj.do_handshake()
ConnectionResetError: [WinError 10054] An existing connection was forcibly closed by the remote host

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Python33\lib\site-packages\requests\adapters.py", line 211, in send
    timeout=timeout
  File "C:\Python33\lib\site-packages\requests\packages\urllib3\connectionpool.py", line 465, in urlopen
    raise MaxRetryError(self, url, e)
requests.packages.urllib3.exceptions.MaxRetryError: HTTPSConnectionPool(host='analysiscenter.veracode.com', port=443): Max retries exceeded with url: /api/4.0/getapplist.do (Caused by <class 'ConnectionResetError'>: [WinError 10054] An existing connection was forcibly closed by the remote host)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Windows\TEMP\hudson581448604896317111.py", line 11, in <module>
    print(v.get_app_list())
  File "c:/tfs/Vertafore_TFSDev/CQ/python/veracode\apiwrapper.py", line 206, in get_app_list
    {}
  File "c:/tfs/Vertafore_TFSDev/CQ/python/veracode\apiwrapper.py", line 57, in request
    r = requests.get(URL, params=data, auth=username_password)
  File "C:\Python33\lib\site-packages\requests\api.py", line 55, in get
    return request('get', url, **kwargs)
  File "C:\Python33\lib\site-packages\requests\api.py", line 44, in request
    return session.request(method=method, url=url, **kwargs)
  File "C:\Python33\lib\site-packages\requests\sessions.py", line 354, in request
    resp = self.send(prep, **send_kwargs)
  File "C:\Python33\lib\site-packages\requests\sessions.py", line 460, in send
    r = adapter.send(request, **kwargs)
  File "C:\Python33\lib\site-packages\requests\adapters.py", line 246, in send
    raise ConnectionError(e)
requests.exceptions.ConnectionError: HTTPSConnectionPool(host='analysiscenter.veracode.com', port=443): Max retries exceeded with url: /api/4.0/getapplist.do (Caused by <class 'ConnectionResetError'>: [WinError 10054] An existing connection was forcibly closed by the remote host)
Build step 'Execute Python script' marked build as failure
Finished: FAILURE
```


Is the URL meant to be publicly accessible?
I get timeouts in every way I try to access it,
so I assume it that the timeout is expected by your testsuite.

On python3.3 the ssl module raises a `ConnectionResetError`, while on python3.2 it's a `socket.error`.
Both are wrapped in `requests.exceptions.ConnectionError`.

Could it be, that your testsuite is somewhere checking the message of the
exception for "socket.error"?


I _was_ checking for socket.error, however, this isn't the issue (I tested with and without it and it still causes the same failure.) Theoretically, according to PEP 3151, it shouldn't be an issue either, because socket.error is an alias for the new OSError. (right?) http://docs.python.org/dev/whatsnew/3.3.html

Unfortunately this isn't a public API. :(

Based on the error I'm seeing, I'm certain this has to be my code... :|

Thank you so much for your kind help. You guys are really awesome. :)

I hope to have this code open sourced before too long here, so I'll update this thread when that happens. Maybe we can get to the bottom of this...


I'm hitting the same problem with Requests 1.2.3 running on Python3.3.


@AvivLander Have you tried the solution proposed in #1418?


@Lukasa Thanks for the suggested solution. Although, I'm not using SSL in my requests. 


Really? You're seeing this error without using SSL? I'm not entirely sure that's possible. Can you copy your traceback?


@Lukasa, It very well could be an error with the script and not the library. The odd thing here is that the address in question is completely accessible.

Here's a traceback:

```
Traceback (most recent call last):
  File "test.py", line 212, in <module>
    HTTPtests = requests.get(url + '&skip=' + str(skip) + '&limit=' + str(baseBatch), auth=(username, password), timeout=None)
  File "/usr/local/lib/python3.3/dist-packages/requests-1.2.3-py3.3.egg/requests/api.py", line 55, in get
    return request('get', url, **kwargs)
  File "/usr/local/lib/python3.3/dist-packages/requests-1.2.3-py3.3.egg/requests/api.py", line 44, in request
    return session.request(method=method, url=url, **kwargs)
  File "/usr/local/lib/python3.3/dist-packages/requests-1.2.3-py3.3.egg/requests/sessions.py", line 335, in request
    resp = self.send(prep, **send_kwargs)
  File "/usr/local/lib/python3.3/dist-packages/requests-1.2.3-py3.3.egg/requests/sessions.py", line 438, in send
    r = adapter.send(request, **kwargs)
  File "/usr/local/lib/python3.3/dist-packages/requests-1.2.3-py3.3.egg/requests/adapters.py", line 327, in send
    raise ConnectionError(e)
requests.exceptions.ConnectionError: HTTPConnectionPool(host='internal-VPC-blablabla.com', port=12345): Max retries exceeded with url: /test/_design/test/_view/test?group_level=2&startkey=[%221%22]&endkey=[%222%22]&skip=5000&limit=500 (Caused by <class 'http.client.BadStatusLine'>: '')

```


So the problem here is right at the end of the exception text: the original exception is [`http.client.BadStatusLine`](http://docs.python.org/3.0/library/http.client.html#http.client.BadStatusLine). Can you either a) make the request from your web browser dev tools and check the status line in the response; or b) do so using Wireshark?


So there's actually an error before:

```
File "/usr/local/lib/python3.3/dist-packages/requests-1.2.3-py3.3.egg
/requests/packages/urllib3/connectionpool.py", line 288, in _make_request
    httplib_response = conn.getresponse(buffering=True)
TypeError: getresponse() got an unexpected keyword argument 'buffering'
```


It should not be possible to hit that error. urllib3 has had a try..except block catching that exception since Requests v0.13.2. Try reinstalling Requests?


The issue was related to urllib3 and AWS ELB. It was resolved when routing the request without the ELB. Hardly a desirable option... Regardless, thanks for your help @Lukasa .

"First, ELB has internal "feature" of closing all incoming HTTP connections that do not get response within 60 seconds. I.e. if your client executes GET ... and waits for more then 60 seconds - ELB will close the connection. This timeout is currently unconfigurable through amazon API and even not advertised in Amazon docs. "
http://tech.zarmory.com/2013/02/handling-tmeouts-with-amazon-elastic.html


@AvivLander Glad you managed to get to the bottom of this.


I still get the <b>TypeError: getresponse() got an unexpected keyword argument 'buffering'</b> for some reason, even though I have Requests 1.2.3 and Python 3.3.1 

I have the following stack trace:

```
Traceback (most recent call last):
  File "/usr/local/lib/python3.3/dist-packages/requests/packages/urllib3/connectionpool.py", line 288, 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 "/usr/local/lib/python3.3/dist-packages/requests/packages/urllib3/connectionpool.py", line 428, in urlopen
    body=body, headers=headers)
  File "/usr/local/lib/python3.3/dist-packages/requests/packages/urllib3/connectionpool.py", line 290, in _make_request
    httplib_response = conn.getresponse()
  File "/usr/lib/python3.3/http/client.py", line 1143, in getresponse
    response.begin()
  File "/usr/lib/python3.3/http/client.py", line 354, in begin
    version, status, reason = self._read_status()
  File "/usr/lib/python3.3/http/client.py", line 316, in _read_status
    line = str(self.fp.readline(_MAXLINE + 1), "iso-8859-1")
  File "/usr/lib/python3.3/socket.py", line 297, in readinto
    return self._sock.recv_into(b)
ConnectionResetError: [Errno 104] Connection reset by peer

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/local/lib/python3.3/dist-packages/requests/adapters.py", line 292, in send
    timeout=timeout
  File "/usr/local/lib/python3.3/dist-packages/requests/packages/urllib3/connectionpool.py", line 474, in urlopen
    raise MaxRetryError(self, url, e)
requests.packages.urllib3.exceptions.MaxRetryError: HTTPConnectionPool(host='la.wikipedia.org', port=80): Max retries exceeded with url: /w/api.php?bltitle=Britannia+Minor&list=backlinks&action=query&format=json&maxlag=60&blnamespace=0&blredirect=true&bllimit=500 (Caused by <class 'ConnectionResetError'>: [Errno 104] Connection reset by peer)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "remote_coords_finder_5.py", line 185, in <module>
    {'list': 'backlinks', 'bltitle': output['title'], 'blnamespace': 0, 'bllimit': 500, 'blredirect': 'true'}, 'backlinks', 'blcontinue'):
  File "remote_coords_finder_5.py", line 55, in query
    response = requests.get('http://' + language + '.wikipedia.org/w/api.php', params=req, headers=headers, timeout=60)
  File "/usr/local/lib/python3.3/dist-packages/requests/api.py", line 55, in get
    return request('get', url, **kwargs)
  File "/usr/local/lib/python3.3/dist-packages/requests/api.py", line 44, in request
    return session.request(method=method, url=url, **kwargs)
  File "/usr/local/lib/python3.3/dist-packages/requests/sessions.py", line 335, in request
    resp = self.send(prep, **send_kwargs)
  File "/usr/local/lib/python3.3/dist-packages/requests/sessions.py", line 438, in send
    r = adapter.send(request, **kwargs)
  File "/usr/local/lib/python3.3/dist-packages/requests/adapters.py", line 327, in send
    raise ConnectionError(e)
requests.exceptions.ConnectionError: HTTPConnectionPool(host='la.wikipedia.org', port=80): Max retries exceeded with url: /w/api.php?bltitle=Britannia+Minor&list=backlinks&action=query&format=json&maxlag=60&blnamespace=0&blredirect=true&bllimit=500 (Caused by <class 'ConnectionResetError'>: [Errno 104] Connection reset by peer)
```


@9emE0iL18gxCqLT Are you using AWS ELB?


@Lukasa No. Just a locally run Python script that does simple HTTP requests. I'm on a Kubuntu GNU/Linux x86_64 desktop.


That's genuinely bizarre. Can you try reinstalling your copy of Requests? (You'll have to delete it first). There's just no way you can hit that exception, because it's inside a try/except block for exactly that purpose.


@Lukasa I've reinstalled requests after I deleted it first, and installed it with <b>sudo pip install git+git://github.com/kennethreitz/requests.git</b> . I get exactly the same TypeError, that is: 

```
Traceback (most recent call last):
  File "/usr/local/lib/python3.3/dist-packages/requests/packages/urllib3/connectionpool.py", line 291, in _make_request
    httplib_response = conn.getresponse(buffering=True)
TypeError: getresponse() got an unexpected keyword argument 'buffering'
etc...
```


@Lukasa httplib.HTTPConnection.getresponse() doesn't accept any keyword arguments in Python 3.3 

See:
1) http://docs.python.org/3.3/library/http.client.html?highlight=httpconnection#http.client.HTTPConnection.getresponse
2) http://hg.python.org/cpython/file/3.3/Lib/http/client.py#l1101

The comments are incorrect in /usr/local/lib/python3.3/dist-packages/requests/packages/urllib3/connectionpool.py line 290:

``` python
   try: # Python 2.7+, use buffering of HTTP responses
            httplib_response = conn.getresponse(buffering=True)
        except TypeError: # Python 2.6 and older
            httplib_response = conn.getresponse()
```

I'll try to send a fix to urllib3.


I agree that the comments are incorrect, but the except block should still catch the TypeError, which is why I'm confused. =)


> but the except block should still catch the TypeError

Well, it is caught, however another exception occurs. For @9emE0iL18gxCqLT it's `ConnectionResetError`
In my case it's `BadStatusLine`

```
Traceback (most recent call last):
  File "/Users/prophet/.envs/djangodash2013/lib/python3.3/site-packages/requests/packages/urllib3/connectionpool.py", line 428, in urlopen
    body=body, headers=headers)
  File "/Users/prophet/.envs/djangodash2013/lib/python3.3/site-packages/requests/packages/urllib3/connectionpool.py", line 290, in _make_request
    httplib_response = conn.getresponse()
  File "/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/http/client.py", line 1131, in getresponse
    response.begin()
  File "/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/http/client.py", line 354, in begin
    version, status, reason = self._read_status()
  File "/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/http/client.py", line 324, in _read_status
    raise BadStatusLine(line)
http.client.BadStatusLine: ''
```


Does anyone have a URL that they can consistently reproduce this with? I've tried about 10 different websites and cannot get either of those exceptions to fire.


I've never had any luck reproducing this.


Hi guys,

I have the same issue here, in the connectionpool.py, line 311, when using Requests 2.1.0:

```
    try:
        try: # Python 2.7+, use buffering of HTTP responses
            httplib_response = conn.getresponse(buffering=True)
        except TypeError: # Python 2.6 and older
            httplib_response = conn.getresponse()
    except SocketTimeout:
        raise ReadTimeoutError(
            self, url, "Read timed out. (read timeout=%s)" % read_timeout)
```

I always get this error, apparently when the connection is not working (I'm currently connecting to an internal ip address, but there's nothing listening on the ip because of some error in the Philips Hue device). If I type in a wrong ip, I don't get this error, then my program just waits a very long time. 

If I can help debugging something, let me know. It's hard for me to reproduce, since the error is not always there, but I can.


> I always get this error

Which error? Can you post a stack trace? There have been a couple different ones posted here that are not exactly the same.

For what it's worth, I still have no way to reproduce this. :(