psf / requests

A simple, yet elegant, HTTP library.
https://requests.readthedocs.io/en/latest/
Apache License 2.0
52.1k stars 9.31k forks source link

SSLError: [Errno bad handshake] (-1, 'Unexpected EOF') #3337

Closed PabloLefort closed 8 years ago

PabloLefort commented 8 years ago

Hi, i have a service for send email through SMTP with Mandrill.

# Service
from django.core.mail import EmailMessage
e = EmailMessage('test subject', 'test message', 'test@mail.com', ['me@me.com'])
e.send()

Throws this exception trace:

Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "/opt/apps/project/virtualenv/project/lib/python2.7/site-packages/django/core/mail/message.py", line 286, in send
    return self.get_connection(fail_silently).send_messages([self])
  File "/opt/apps/project/virtualenv/project/lib/python2.7/site-packages/djrill/mail/backends/djrill.py", line 68, in send_messages
    sent = self._send(message)
  File "/opt/apps/project/virtualenv/project/lib/python2.7/site-packages/djrill/mail/backends/djrill.py", line 107, in _send
    response = requests.post(api_url, data=json.dumps(api_params, cls=JSONDateUTCEncoder))
  File "/opt/apps/project/virtualenv/project/lib/python2.7/site-packages/requests/api.py", line 109, in post
    return request('post', url, data=data, json=json, **kwargs)
  File "/opt/apps/project/virtualenv/project/lib/python2.7/site-packages/requests/api.py", line 50, in request
    response = session.request(method=method, url=url, **kwargs)
  File "/opt/apps/project/virtualenv/project/lib/python2.7/site-packages/requests/sessions.py", line 465, in request
    resp = self.send(prep, **send_kwargs)
  File "/opt/apps/project/virtualenv/project/lib/python2.7/site-packages/requests/sessions.py", line 573, in send
    r = adapter.send(request, **kwargs)
  File "/opt/apps/project/virtualenv/project/lib/python2.7/site-packages/requests/adapters.py", line 431, in send
    raise SSLError(e, request=request)
SSLError: [Errno bad handshake] (-1, 'Unexpected EOF')

Env: Red Hat 4.8.2-13, Python: 2.7.5 Libs:

Django==1.7.4
requests==2.7.0
djrill==1.3.0
pyOpenSSL==0.14

Any suggestions?

Going with this answer did not change anything.

Thanks in advance.

Lukasa commented 8 years ago

Unexpected EOF in this instance almost certainly means that there is a problem with your TLS handshake, and the server just tore the connection down rather than establish the TLS connection.

Firstly, you should install pyasn1 and ndg-httpsclient in addition to your current libraries: that will resolve your possible SNI issue. Then, if that didn't help, you should let me know what version of OpenSSL is installed in your system.

PabloLefort commented 8 years ago

@Lukasa Already installed those libs, alike, running with --upgrade tells Requirement already up-to-date

Lukasa commented 8 years ago

@PabloLefort And your OpenSSL version, please?

PabloLefort commented 8 years ago

@Lukasa OpenSSl version: OpenSSL 1.0.1e-fips 11 Feb 2013

Lukasa commented 8 years ago

Right, so there's no immediately obvious set of problems there. Are you familiar with Wireshark? If so, it'd be very convenient if you could grab a packet capture of the failing connection.

PabloLefort commented 8 years ago

I'm not familiar but i will try to grab the packet and post the response. Thanks.

PabloLefort commented 8 years ago

@Lukasa So, after some investigation, installed tcpdump to see whats happening with the packets. On my local env the connection was through TLS, but in the server first try to connect with TLS and fallback to SSL. This raise EOF Exception. Going foward, there was some firewall closing every connection. I changed it and it works like a charm!

Thanks for all.

ghost commented 7 years ago

I would like to note, that problems may be relating on the proxy settings. Unset https_proxy and http_proxy.

PabloLefort commented 7 years ago

Thanks! @VladislavMesh 🚀