New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

error 54, 'Connection reset by peer' #3189

Closed
degroat opened this Issue May 12, 2016 · 16 comments

Comments

Projects
None yet
7 participants
@degroat

degroat commented May 12, 2016

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:

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?

@Lukasa

This comment has been minimized.

Show comment
Hide comment
@Lukasa

Lukasa May 12, 2016

Member

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)"
Member

Lukasa commented May 12, 2016

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)"
@degroat

This comment has been minimized.

Show comment
Hide comment
@degroat

degroat May 12, 2016

>> 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

degroat commented May 12, 2016

>> 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
@Lukasa

This comment has been minimized.

Show comment
Hide comment
@Lukasa

Lukasa May 12, 2016

Member

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?

Member

Lukasa commented May 12, 2016

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?

@degroat

This comment has been minimized.

Show comment
Hide comment
@degroat

degroat May 12, 2016

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.

degroat commented May 12, 2016

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.

@Lukasa

This comment has been minimized.

Show comment
Hide comment
@Lukasa

Lukasa May 12, 2016

Member

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.

Member

Lukasa commented May 12, 2016

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.

@degroat

This comment has been minimized.

Show comment
Hide comment
@degroat

degroat May 12, 2016

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

degroat commented May 12, 2016

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

@Lukasa

This comment has been minimized.

Show comment
Hide comment
@Lukasa

Lukasa May 12, 2016

Member

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

Member

Lukasa commented May 12, 2016

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

@Lukasa Lukasa closed this May 12, 2016

@goalong

This comment has been minimized.

Show comment
Hide comment
@goalong

goalong May 30, 2016

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

goalong commented May 30, 2016

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

@Lukasa

This comment has been minimized.

Show comment
Hide comment
@Lukasa

Lukasa May 30, 2016

Member

@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.

Member

Lukasa commented May 30, 2016

@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.

@mattdeboard

This comment has been minimized.

Show comment
Hide comment
@mattdeboard

mattdeboard Nov 20, 2016

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.

mattdeboard commented Nov 20, 2016

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.

@Bashar

This comment has been minimized.

Show comment
Hide comment
@Bashar

Bashar Jan 19, 2017

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

Bashar commented Jan 19, 2017

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

@Lukasa

This comment has been minimized.

Show comment
Hide comment
@Lukasa

Lukasa Jan 19, 2017

Member

@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.

Member

Lukasa commented Jan 19, 2017

@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.

@duginivijay

This comment has been minimized.

Show comment
Hide comment
@duginivijay

duginivijay Sep 18, 2017

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

duginivijay commented Sep 18, 2017

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

@duginivijay

This comment has been minimized.

Show comment
Hide comment
@duginivijay

duginivijay Sep 18, 2017

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

duginivijay commented Sep 18, 2017

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

@duginivijay

This comment has been minimized.

Show comment
Hide comment
@duginivijay

duginivijay Sep 18, 2017

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.

duginivijay commented Sep 18, 2017

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.

@rajpalc

This comment has been minimized.

Show comment
Hide comment
@rajpalc

rajpalc Feb 21, 2018

Was facing the same issue: python ConnectionError: ('Connection aborted.', error(54, 'Connection reset by peer'))

pip install requests[security] resolved it.

rajpalc commented Feb 21, 2018

Was facing the same issue: python ConnectionError: ('Connection aborted.', error(54, 'Connection reset by peer'))

pip install requests[security] resolved it.

@requests requests locked as off topic and limited conversation to collaborators Feb 22, 2018

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.