Skip to content
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

SSLError: [Errno bad handshake] (32, 'EPIPE') #242

Open
maanas opened this issue Apr 20, 2015 · 14 comments
Open

SSLError: [Errno bad handshake] (32, 'EPIPE') #242

maanas opened this issue Apr 20, 2015 · 14 comments

Comments

@maanas
Copy link

maanas commented Apr 20, 2015

I m trying to implement httpretty to mock a https endpoint. I m getting this error.

  File "util/requests_client.py", line 18, in http_do
    verify=False
  File "/usr/local/lib/python2.7/dist-packages/requests/api.py", line 50, in request
    response = session.request(method=method, url=url, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/requests/sessions.py", line 464, in request
    resp = self.send(prep, **send_kwargs)
  File "/usr/local/lib/python2.7/dist-packages/requests/sessions.py", line 576, in send
    r = adapter.send(request, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/requests/adapters.py", line 431, in send
    raise SSLError(e, request=request)
SSLError: [Errno bad handshake] (32, 'EPIPE')
-------------------- >> begin captured logging << --------------------
requests.packages.urllib3.connectionpool: INFO: Starting new HTTPS connection (1): gateway.agms.com
--------------------- >> end captured logging << ---------------------

@briancorysherwin
Copy link

Hi. I had something like this problem. Trying to write unittests.
I had a dependency using ndg-httpsclient 0.4.0
It called in a weird dependency on PyOpenSSL 0.15.1

If you pin to ndg-httpsclient 0.3.0 and PyOpenSSL < 0.14, that might work.

Hopefully this helps..

@mass
Copy link

mass commented May 28, 2015

I too had this same problem. Downgrading to ndg-httpsclient:0.3.0 fixed the problem for me as well. Any idea why this is happening?

@briancorysherwin
Copy link

ndg-httpsclient is magical monkey patcher. I didn't look deeply into it but the a handshake error usually means something like httpretty is expecting sslv3 and received sslv2.

Conversely if you pin PyOpenSSL to >=0.15.1 and ndg-httpsclient to 0.4.0 it will work as well.

andresriancho added a commit to tagcubeio/tagcube-cli that referenced this issue Jun 29, 2015
andresriancho added a commit to tagcubeio/tagcube-cli that referenced this issue Jun 29, 2015
@jmcbailey
Copy link

We had this problem too. Downgrading ndg-httpsclient:0.3.0 fixed the tests that were failing, but caused some other tests to fail with the exception

File "/home/work/.virtualenvs/develop/local/lib/python2.7/site-packages/urllib3/contrib/pyopenssl.py", line 48, in <module>
  from ndg.httpsclient.ssl_peer_verification import SUBJ_ALT_NAME_SUPPORT
ImportError: cannot import name SUBJ_ALT_NAME_SUPPORT

Rather than trying to figure out what combination of ndg-httpsclient and PyOpenSSL would work for everything, we decided to go back to ndg-httpsclient:0.4.0 and work around the issue by just monkey-patching httpretty itself.

The problem is that PyOpenSSL monkey-patches the ssl_wrap_socket function in requests/urllib3, and this version won't work with httpretty. But PyOpenSSL conveniently has inject_into_urllib3 and extract_from_urllib3 functions for doing and undoing the monkey-patch, respectively. So our solution was to just extend HTTPretty to call these functions in its disable() and enable() methods, respectively; then substitute in our subclass into the httpretty module.

Code attached here.

Just use

from mypretty import httpretty

Not sure if there's a case for a pull request to merge the fix into httpretty core itself but there it is. It fixed all our failing tests!

@rosscdh
Copy link

rosscdh commented Feb 3, 2016

+1 experiencing this here too

@alexandre
Copy link

Not sure if there's a case for a pull request to merge the fix into httpretty core itself but there it is. It fixed all our failing tests!

This issue helped me too. Would be useful create a kind of FAQ to highlight issues like this one, woudn't ?

@clintonb
Copy link

clintonb commented Mar 3, 2016

Any update on this? Is there a plan to implement a fix?

@jrpope2014
Copy link
Contributor

I second Clinton's question. ndg-httpsclient is part of a requests[security] update that we need in order for some code to function properly, but we have several tests that use httpretty, so our tests break upon installing this package. Do we know about how long it would take such an update to be merged?

@jrpope2014
Copy link
Contributor

Hey guys, just a heads up, I just submitted an MR for this issue. The changes that I implemented seem to work on my end without issue (note that they are the same as the changes mentioned above by jmcbailey, thanks for that by the way! but implemented directly into the httpretty.core file).

@hussaintamboli
Copy link

So what's the final fix for this issue? When I downgrade ndg-httpsclient to 0.3.0 and test a call to my api, I get

<html>
<head><title>400 The plain HTTP request was sent to HTTPS port</title></head>
<body bgcolor="white">
<center><h1>400 Bad Request</h1></center>
<center>The plain HTTP request was sent to HTTPS port</center>
</body>
</html>

@clintonb
Copy link

I replaced httpretty with the responses library.

mredar added a commit to mredar/harvester that referenced this issue May 3, 2017
jonnybazookatone pushed a commit to jonnybazookatone/ads that referenced this issue May 27, 2017
There is still an ongoing ticket on HTTPretty, that is yet to be addressed
and closed:

gabrielfalcao/HTTPretty#242
gabrielfalcao/HTTPretty#289

For now, used the suggested fix from @jmcbailey in the same thread.
@traut
Copy link

traut commented Sep 11, 2017

this issue is still reproducible in 0.8.14.

In the shell I see:

In [9]: import requests
   ...: import httpretty
   ...:
   ...: @httpretty.activate
   ...: def run(url):
   ...:     httpretty.register_uri(httpretty.POST, url, body='somebody')
   ...:     rs = requests.post(url)
   ...:     return rs
   ...:

In [10]: run('http://google.com')
Out[10]: <Response [200]>

In [11]: run('https://google.com')
...
/Users/traut/.p3/lib/python3.6/site-packages/requests/sessions.py in send(self, request, **kwargs)
    607
    608         # Send the request
--> 609         r = adapter.send(request, **kwargs)
    610
    611         # Total elapsed time of the request (approximately)

/Users/traut/.p3/lib/python3.6/site-packages/requests/adapters.py in send(self, request, stream, timeout, verify, cert, proxies)
    495         except (_SSLError, _HTTPError) as e:
    496             if isinstance(e, _SSLError):
--> 497                 raise SSLError(e, request=request)
    498             elif isinstance(e, ReadTimeoutError):
    499                 raise ReadTimeout(e, request=request)

SSLError: ('bad handshake: WantWriteError()',)

also, Trevis build for my project fails with similar error:

SSLError: HTTPSConnectionPool(host='example.locahost', port=443): Max retries exceeded with url: /some/discovery/path (Caused by SSLError(SSLError("bad handshake: SysCallError(32, 'EPIPE')",),))

nijel added a commit to WeblateOrg/weblate that referenced this issue Oct 6, 2017
@nijel
Copy link

nijel commented Oct 6, 2017

Any chance to get this issue addressed? There is pull request https://docs.weblate.org/en/latest/faq.html#why-do-i-get-a-warning-about-not-reflected-changes-on-database-migration which seems to fix this and several projects have already copied that fix to their codebase to address this.

Apparently the problem is not really bound to ndg-httpsclient, it just happens with requests + pyopenssl (though not on all setups).

@wbolster
Copy link

wbolster commented Nov 9, 2017

i usually do not like promoting other projects in a projects bug tracker, but since this issue has been around for quite a while, here's a tip: using ‘responses’ instead of ‘httpretty’ seems to solve this issue with a modern requests/pyopenssl/crypography stack.

https://github.com/getsentry/responses

the api is very similar for simple use cases.

lnielsen pushed a commit to inveniosoftware/datacite that referenced this issue Nov 13, 2017
* Mocked HTTPretty, which has a bug causing SSL requests
  fail for certain versions of openssl library.
  Related: gabrielfalcao/HTTPretty#242

Signed-off-by: Krzysztof Nowak <k.nowak@cern.ch>
mindflayer added a commit to mindflayer/python-mocket that referenced this issue Aug 2, 2018
mindflayer added a commit to mindflayer/python-mocket that referenced this issue Aug 2, 2018
* Fix for #65, thanks to the @jmcbailey patch at gabrielfalcao/HTTPretty#242.
* Improving the check, removing dependency of `requests` which is only acting as wrapper.
mindflayer added a commit to mindflayer/python-mocket that referenced this issue Sep 7, 2018
* Adding yaml for Shippable CI.
* Fix for pip 10 which misses .main()
* Adding `recv_into` support (#72)
* Adding socket.recv_into() plus its test (#71).
* Forcing `pip` upgrade.
* Fix for #65, thanks to the @jmcbailey patch at gabrielfalcao/HTTPretty#242.
* Improving the check, removing dependency of `requests` which is only acting as wrapper.
* Increasing stability of `true_sendall` function.
* Improving the `flake8` usage.
* Minor changes.
* Status badge from Shippable
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests