Python Requests Requests.Exceptions.Sslerror: [Errno 8] _Ssl.C:504: Eof Occurred in Violation of Protocol

Python Requests requests.exceptions.SSLError: [Errno 8] _ssl.c:504: EOF occurred in violation of protocol

Reposting this here for others from the requests issue page:

Requests' does not support doing this before version 1. Subsequent to version 1, you are expected to subclass the HTTPAdapter, like so:

from requests.adapters import HTTPAdapter
from requests.packages.urllib3.poolmanager import PoolManager
import ssl

class MyAdapter(HTTPAdapter):
def init_poolmanager(self, connections, maxsize, block=False):
self.poolmanager = PoolManager(num_pools=connections,
maxsize=maxsize,
block=block,
ssl_version=ssl.PROTOCOL_TLSv1)

When you've done that, you can do this:

import requests
s = requests.Session()
s.mount('https://', MyAdapter())

Any request through that session object will then use TLSv1.

python-requests 2.0.0 - [Errno 8] _ssl.c:504: EOF occurred in violation of protocol

The server requires that you use SNI, which isn't normally available in Python 2.x.

If you open that URL in a browser and use Wireshark to trace out the TLS handshake, you can see that Chrome proposes the server name and that the remote server uses it to determine which certificate to use.

To make this work in Requests you can either use Python 3, which includes SNI support and which Requests will transparently make use of, or you can install the required dependencies for SNI in Python 2.x in Requests from this answer:

  • pyopenssl
  • ndg-httpsclient
  • pyasn1

Either of those solutions will make your code work correctly.

requests.exceptions.SSLError: [Errno 8] _ssl.c:504: EOF occurred in violation of protocol

In all of the versions of Requests on PyPI there is no support for requesting HTTPS sites over a proxy since there is no support for the CONNECT verb. Our pre-release branch for 2.0 has this support and it works on every proxy I have tried. If you wish to check that out and try it with this, then you won't need the custom adapter either.

Otherwise, you will have to wait until we release 2.0

HTTPS proxies with Requests: [Errno 8] _ssl.c:504: EOF occurred in violation of protocol

I suspect your proxy is a http proxy over which you can use https (the common case)
The problem is, that requests uses https to talk to proxies if the request itself https.
Using an explicit protocol (http) for your proxy should fix things: proxies={'https': 'http://IP:PORT'}

Also have a look at https://github.com/kennethreitz/requests/issues/1182



Related Topics



Leave a reply



Submit