requests.exceptions.SSLError: [Errno 8] _ssl.c:504

2019-02-20 06:25发布

问题:

I have tried everything on this q&a to solve it but I still receive that error.

My latest attempt is based on Lukasa's comment and my code looks like this:

import requests
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)

proxy = 'https://78.130.136.2:8080'
g = 'https://www.google.com/'


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

r = s.get(g, proxies={'https': proxy} )
print r.text.encode('utf-8')

The full error I recieve is:

Traceback (most recent call last):
  File "/Users/Dionysis_Lorentzos/Projects/getter/proxy.py", line 30, in <module>
    r = s.get(g, proxies={'https': proxy}, verify=True )
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/requests/sessions.py", line 347, in get
    return self.request('GET', url, **kwargs)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/requests/sessions.py", line 335, in request
    resp = self.send(prep, **send_kwargs)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/requests/sessions.py", line 438, in send
    r = adapter.send(request, **kwargs)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/requests/adapters.py", line 331, in send
    raise SSLError(e)
requests.exceptions.SSLError: [Errno 8] _ssl.c:504: EOF occurred in violation of protocol

So how can I get the site's data with the proxy over the https? (the proxy works fine in my Firefox)

回答1:

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



回答2:

You have to use http as the protocol for your proxy (although you are using https over it)

proxy = 'http://78.130.136.2:8080'

But like @sigmavirus24 said the current release version of requests has some problems with http proxies. With this setting you will be able to use the proxy (not all proxies but this proxy in particular works).

Be careful: With the current version the data is NOT encrypted between you and your proxy.

Repeat: The proxy and everything between it and you can read the data!