Python: Verify SSL certificates for HTTPS requests
Python Requests: Exercise-9 with Solution
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, inresponse1 = 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.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://w3resource.com/python-exercises/requests/python-request-exercise-9.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics