1

I have a new (May 2020) python 3.8.3 installation on windows 10 machine.

When I try to read an https url with requests.get, it fails with SSL error 'certificate verify failed'. But when I try to read the same url using urllib.request.urlopen, it works fine. Raising this as a new issue because if certificate/permission/etc. was an issue urllib.requests should have also failed.

I tried the suggestions given for Python Requests throwing SSL Error issue but without any success. Would appreciate any help.

urllib.request.urlopen works:

>>> import ssl
>>> import urllib.request
>>> ssl_context = ssl.create_default_context()
>>> r = urllib.request.urlopen('https://google.com', context = ssl_context).getcode()
>>> r
200

Requests.get Fails:

>>> import requests
>>> r = requests.get('https://google.com')
Traceback (most recent call last):
  File "C:\Program Files\Python\Python38\lib\site-packages\urllib3\connectionpool.py", line 670, in urlopen
    httplib_response = self._make_request(
  File "C:\Program Files\Python\Python38\lib\site-packages\urllib3\connectionpool.py", line 381, in _make_request
    self._validate_conn(conn)
  File "C:\Program Files\Python\Python38\lib\site-packages\urllib3\connectionpool.py", line 976, in _validate_conn
    conn.connect()
  File "C:\Program Files\Python\Python38\lib\site-packages\urllib3\connection.py", line 361, in connect
    self.sock = ssl_wrap_socket(
  File "C:\Program Files\Python\Python38\lib\site-packages\urllib3\util\ssl_.py", line 377, in ssl_wrap_socket
    return context.wrap_socket(sock, server_hostname=server_hostname)
  File "C:\Program Files\Python\Python38\lib\ssl.py", line 500, in wrap_socket
    return self.sslsocket_class._create(
  File "C:\Program Files\Python\Python38\lib\ssl.py", line 1040, in _create
    self.do_handshake()
  File "C:\Program Files\Python\Python38\lib\ssl.py", line 1309, in do_handshake
    self._sslobj.do_handshake()
ssl.SSLCertVerificationError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1108)
  • `... ', context = ssl_context ` - what is `ssl_context`? How it is created? My guess is that is a SSL context which simply disables verification since no useful CA's are installed. – Steffen Ullrich Jun 10 '20 at 19:35
  • I should have provided that info. It's default ssl context created by using the following command: >>> ssl_context = ssl.create_default_context() – Bhuwan Pandey Jun 11 '20 at 04:23
  • *"I tried the suggestions given for Python Requests throwing SSL Error issue but without any success."* - there are many suggestions in the post you refer to. Which of these did you try and what was the result? Specifically, did you try the one with setting `verify=False`? While this is a bad idea in production I would find it strange if it still fails for the same reason. – Steffen Ullrich Jun 11 '20 at 04:48
  • It failed with verify = False too (though not useful even if it works). >>> r = requests.get('https://google.com', verify = False) C:\Program Files\Python\Python38\lib\site-packages\urllib3\connectionpool.py:979: InsecureRequestWarning: Unverified HTTPS request is being made to host 'google.com'. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.io/en/latest/advanced-usage.html#ssl-warnings warnings.warn( – Bhuwan Pandey Jun 11 '20 at 11:38
  • There were some other suggestion like providing the certificate path with verify = path/to/cacert.pem, upgrading pip, downloading new cacert.pem, etc. None worked. – Bhuwan Pandey Jun 11 '20 at 11:52
  • *"There were some other suggestion like ..."* - unfortunately this is fairly unspecific and it makes it needlessly hard to help you. Which `cacert.pem` did you try? Did you try to install/upgrade certifi? Please update your question with more details on what exactly you've tried and how exactly you've failed. – Steffen Ullrich Jun 11 '20 at 12:13

0 Answers0