Python: Verify SSL certificates for HTTPS requests

Python Requests: Exercise-9 with Solution

Write a Python code to verify the SSL certificate for a website which is certified.

Sample Solution:

Python Code:

import requests   
#Requests ignore verifying the SSL certificate if you set verify to False
# Making a get request 
response = requests.get('https://rigaux.org/', verify=False)
#Requests verifies SSL certificates for HTTPS requests, just like a web browser.
response1 = requests.get('https://google.com/')
#Requests ignore verifying the SSL certificate if you set verify to True (Default value)
response1 = requests.get('https://rigaux.org/', verify=True)

Sample Output:

D:\Anaconda3\lib\site-packages\urllib3\connectionpool.py:847: InsecureRequestWarning: Unverified HTTPS request is being made. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.io/en/latest/advanced-usage.html#ssl-warnings
<Response [200]>


<Response [200]>


Traceback (most recent call last):

  File "<ipython-input-10-5ba32625e270>", line 1, in <module>
    runfile('C:/Users/User/.spyder-py3/temp.py', wdir='C:/Users/User/.spyder-py3')

  File "D:\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 827, in runfile
    execfile(filename, namespace)

  File "D:\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 110, in execfile
    exec(compile(f.read(), filename, 'exec'), namespace)

  File "C:/Users/User/.spyder-py3/temp.py", line 12, in 
    response1 = requests.get('https://rigaux.org/', verify=True)

  File "D:\Anaconda3\lib\site-packages\requests\api.py", line 75, in get
    return request('get', url, params=params, **kwargs)

  File "D:\Anaconda3\lib\site-packages\requests\api.py", line 60, in request
    return session.request(method=method, url=url, **kwargs)

  File "D:\Anaconda3\lib\site-packages\requests\sessions.py", line 533, in request
    resp = self.send(prep, **send_kwargs)

  File "D:\Anaconda3\lib\site-packages\requests\sessions.py", line 646, in send
    r = adapter.send(request, **kwargs)

  File "D:\Anaconda3\lib\site-packages\requests\adapters.py", line 514, in send
    raise SSLError(e, request=request)

SSLError: HTTPSConnectionPool(host='rigaux.org', port=443): Max retries exceeded with url: / (Caused by SSLError(SSLCertVerificationError("hostname 'rigaux.org' doesn't match 'www.zarb.org'")))

Python Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a Python code to send cookies to a given server and access cookies from the response of a server.

What is the difficulty level of this exercise?

Test your Python skills with w3resource's quiz

Python: Tips of the Day

Merging two dicts in Python 3.5+ with a single expression


# How to merge two dictionaries
# in Python 3.5+

x = {'p': 1, 'q': 3}
y = {'q': 5, 'r': 8}

z = {**x, **y}

{'r': 4, 'p': 1, 'q': 3}

z = dict(x, **y)


{'p': 1, 'q': 5, 'r': 8}