Solving Python requests SSL error – certificate verify failed

Python requests SSL error – certificate verify failed is a Python WebDriver error which occurs when the site you are visiting has a bad SSL implementation.

In this article I am going to try and solve the error while I try to explain why the error is actually popping up, we will also introduce some solutions which have worked for other developers and see if those solutions can solve the error in your particular situation.

Explaining the Error : Python requests SSL error – certificate verify failed

Before we solve this error, let us try to replicate a scenario where this takes place. This error will occur in any script that uses requests to access a URL, if the website being requested has a bad SSL certificate the error will occur.

                                                                       #
[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:777)
                                                                       #

Here is an example of code that will generate the error.

                                                                       #
import requests
requests.get("https://wordcounter.net")
                                                                       #

Bellow we will take care of the error using multiple possible solutions according to your needs.

Solution 1 : edit the .pem file

The first solution is simple, edit the .pem file.

Navigate to the path bellow on your machine

                                                                       #
/usr/lib/python3.6/site-packages/certifi/cacert.pem
                                                                       #

Try to edit the .pem file by adding the public certificate. The public certificate is inside the .pem file provided by the ssl certificate provider.

These are the links:

Missing chain certificate : https://www.censys.io/certificates/19400be5b7a31fb733917700789d2f0a2471c0c9d506c0e504c06c16d7cb17c0/pem

Root CA : https://www.censys.io/certificates/7431e5f4c3c1ce4690774f0b61e05440883ba9a01ed00ba6abd7806ed3b118cf/pem

Save the changes. This should be enough to solve the problem. If the issue persists try the solution bellow.

Solution 2 : A simple command

The second solution is to use the command bellow. This should be enough to solve the ssl issue.

                                                                       #
cat institution-certificate.pem >> venv/lib/python3.9/site-packages/certifi/cacert.pem
                                                                       #

I hope this guide solved your problem, thank you for reading.

Summing-up

The end, we arrived at the end of this article, I hope my article helped you solve your issue, I am sure there are at least 5 other reasons why this error occurs, I can not find them all and explain them, this is my best attempt guys,

Thank you for reading, cheers. If you want to learn more about Python, please check out the Python Documentation : https://docs.python.org/3/