Fix Python SSL Error: Certificate Verify Failed with Requests Library
If you're working with Python and trying to make a request to a secure website (i.e. using HTTPS), you may have encountered the following error:
SSL Certificate Verification Failed: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:749)
This error occurs because Python's SSL module is not able to verify the SSL certificate provided by the server. In this case, we are using the popular Requests library, which relies on Python's SSL module.
To fix this error, we need to tell Python's SSL module to trust the SSL certificate provided by the server. We can do this by pointing Python to a file containing the SSL certificate.
First, we need to obtain the SSL certificate from the server. This can usually be done by visiting the website in a browser and clicking on the padlock icon in the address bar. Then, click on "Certificate" or "View Certificate" to download the certificate file.
Once we have the certificate file, we can tell Requests to use it by passing in the
verify parameter with the path to the certificate file:
import requests response = requests.get("https://example.com", verify="/path/to/certificate.pem")
Alternatively, we can tell Python to trust all SSL certificates by setting the
verify parameter to
import requests import urllib3 urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning) response = requests.get("https://example.com", verify=False)
Note that disabling SSL verification is not recommended in production environments.
If you're encountering SSL certificate verification errors when using Python's Requests library, the solution is to point Python to a file containing the SSL certificate or to disable SSL verification altogether. However, it's important to only disable SSL verification in development environments and to use SSL verification in production environments to ensure secure communication.