w3resource

Python: Verify SSL certificates for HTTPS requests


9. SSL Certificate Verifier

Write a Python program to verify the SSL certificate for a website that 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)
print(response) 
print("\n=======================================================\n")
#Requests verifies SSL certificates for HTTPS requests, just like a web browser.
response1 = requests.get('https://google.com/')
print(response1)
print("\n=======================================================\n")
#Requests ignore verifying the SSL certificate if you set verify to True (Default value)
response1 = requests.get('https://rigaux.org/', verify=True)
print(response1) 

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
  InsecureRequestWarning)
<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'")))

For more Practice: Solve these Related Problems:

  • Write a Python program to send a request to a secure website and verify its SSL certificate by checking the response using requests.get() with verify=True.
  • Write a Python function that attempts to access a website with an invalid certificate and catches the resulting exception, then prints a custom error message.
  • Write a Python script to compare the SSL certificate details (issuer, subject) of two different secure websites by sending requests and printing the certificate information.
  • Write a Python program to validate the SSL certificate for a certified website, then print the certificate's expiry date and other key details.

Go to:


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

Next: SQLite Database Exercises Home.

Python Code Editor:


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

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.