error 54, 'Connection reset by peer'

I'm getting the dreaded Error 54, Connection reset by peer when trying to do a POST to a URL with a payment provider I'm using.  Here is the basic code I'm running:

``` python
import requests, base64
url = "https://sandbox.api.mxmerchant.com/checkout/v3/auth/token/282059461"
headers = {
    'Authorization': 'Basic ' + base64.b64encode('myusername:mypassword')   ,
}
r = requests.post(url, headers=headers)
print r.text
```

If I run this exact code on an Ubuntu server it works fine (meaning it returns a json message stating that the username and password are incorrect).  

If I run it on OSX 10.11.4, I get the connection reset error.   

After reading a ton of issues on here and various postings on StackOverflow, everyone seems to think it's related to SNI.    Unfortunately, none of the recommendations that I've come across have fixed the issue.

My Ubuntu server has Python 2.7.6, OpenSSL 1.0.1f and requests 2.8.1.

My OSX  has Python 2.7.10, OpenSSL 1.0.2g and requests 2.10.0.  I also have ndg-httpsclient and pyopenssl both installed per #1347 

Any idea what it is that I'm missing?


Can you confirm for me what the output of these two commands is on your OS X machine?

```
python -c "import ssl; print ssl.OPENSSL_VERSION"
python -c "from OpenSSL.SSL import SSLeay_version, SSLEAY_VERSION; print SSLeay_version(SSLEAY_VERSION)"
```


```
>> python -c "import ssl; print ssl.OPENSSL_VERSION"
---
OpenSSL 0.9.8zh 14 Jan 2016
```

So...  obviously that's an issue there.  But this looks even worse...

```
>> python -c "from OpenSSL.SSL import SSLeay_version, SSLEAY_VERSION; print SSLeay_version(SSLEAY_VERSION)"
---
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/Users/degroat/.virtualenvs/cb/lib/python2.7/site-packages/OpenSSL/__init__.py", line 8, in <module>
    from OpenSSL import rand, crypto, SSL
  File "/Users/degroat/.virtualenvs/cb/lib/python2.7/site-packages/OpenSSL/rand.py", line 12, in <module>
    from OpenSSL._util import (
  File "/Users/degroat/.virtualenvs/cb/lib/python2.7/site-packages/OpenSSL/_util.py", line 6, in <module>
    from cryptography.hazmat.bindings.openssl.binding import Binding
  File "/Users/degroat/.virtualenvs/cb/lib/python2.7/site-packages/cryptography/hazmat/bindings/openssl/binding.py", line 15, in <module>
    from cryptography.hazmat.bindings._openssl import ffi, lib
ImportError: dlopen(/Users/degroat/.virtualenvs/cb/lib/python2.7/site-packages/cryptography/hazmat/bindings/_openssl.so, 2): Symbol not found: _BIO_new_CMS
  Referenced from: /Users/degroat/.virtualenvs/cb/lib/python2.7/site-packages/cryptography/hazmat/bindings/_openssl.so
  Expected in: flat namespace
 in /Users/degroat/.virtualenvs/cb/lib/python2.7/site-packages/cryptography/hazmat/bindings/_openssl.so
```


Alright! Good, we're getting somewhere.

So, the `ImportError` from `PyOpenSSL` is causing requests to fallback to the stdlib, and that ancient OpenSSL is almost certainly not compatible with your website. How did you install Python 2.7.10, or did it ship with the OS?


I'm 99% sure I went with the OSX baked in python last time I rebuilt my dev environment.  When I'm not in a virtualenv, which python points to '/usr/bin/python' and that file is not a symlink to the Cellar directory.


Ok, so that there is going to be the issue. The system Python uses the ancient OpenSSL that OS X ships.

I'd like to tag @reaperhulk in here to try to understand why PyOpenSSL isn't correctly installed, because that _should_ have resolved the problem.


I went ahead and installed a new version of python using homebrew (using brewed OpenSSL) and that fixed it for me.


@degroat Yup, that'll do it too. =)


I met the same issue. Is there a solution to solve this without upgrade python?


@goalong It's possible, but it depends on the specifics of your situation. In this instance, all we know is that the server you're connecting to doesn't like your TLS handshake. That can happen for lots of reasons, but without specifics it's hard to know what would fix the problem. 


For the sake of others like me coming to this issue by googling this exception:

I resolved this issue for myself by updating my system `virtualenvwrapper` package to latest (`sudo pip install -U virtualenvwrapper`), which lifted my `virtualenv` version from 13.1.2 to 15.1.0. I then recreated the virtualenv for my project. I then executed the command that was giving me the error in the first place, and it completed successfully.


while researching online another solution was `pip install pyopenssl ndg-httpsclient pyasn1` as per http://stackoverflow.com/a/38854398/1349938

@Bashar That specific list is now out of date. You should make sure you use `pip install requests[security]` instead: that will always install the correct dependencies.

Hey guys, I've tried the solution given above but still I'm facing the same issue. Could you please help. TIA
VJ:audit vijay.dugini$ python -c "import ssl; print ssl.OPENSSL_VERSION"
OpenSSL 1.0.2l  25 May 2017
VJ:audit vijay.dugini$ python -c "from OpenSSL.SSL import SSLeay_version, SSLEAY_VERSION; print SSLeay_version(SSLEAY_VERSION)"
OpenSSL 1.1.0f  25 May 2017

Tried pip install pyopenssl ndg-httpsclient pyasn1 and also installed requests but still no result

Ignore my post mates. I had multiple version of pythons in my machine due to which it wasn't able to pick the right one and was throwing error. Posting this thinking it may be helpful for someone.

Was facing the same issue: python ConnectionError: ('Connection aborted.', error(54, 'Connection reset by peer'))

`pip install requests[security]` resolved it.