HTTPS proxies with Requests: [Errno 8] _ssl.c:504:

2019-08-13 03:19发布

问题:

I am using Requests 1.2.3 on Windows 7 x64 and am trying to connect to (any) site via HTTPS using a HTTPS proxy by passing the proxies argument to the request.

I don't experience this error when using urllib2's ProxyHandler, so I don't think it's on my proxy's side.

>>> opener = urllib2.build_opener(urllib2.ProxyHandler({'https': 'IP:PORT'}))
>>> resp = opener.open('https://www.google.com')
>>> resp.url
'https://www.google.co.uk/'
>>> resp = requests.get('https://www.google.com', proxies={'https': 'IP:PORT'})
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Python27\lib\site-packages\requests\api.py", line 55, in get
    return request('get', url, **kwargs)
  File "C:\Python27\lib\site-packages\requests\api.py", line 44, in request
    return session.request(method=method, url=url, **kwargs)
  File "C:\Python27\lib\site-packages\requests\sessions.py", line 335, in request
    resp = self.send(prep, **send_kwargs)
  File "C:\Python27\lib\site-packages\requests\sessions.py", line 438, in send
    r = adapter.send(request, **kwargs)
  File "C:\Python27\lib\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

I should probably note that the same error still happens if I pass verify=False to the request.

Any suggestions? I've looked at related questions but there was nothing that worked for me.

回答1:

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