[Solved-5 Solutions] urllib and “SSL: CERTIFICATE_VERIFY_FAILED” Error - Python
Error Description:
urllib and “SSL: CERTIFICATE_VERIFY_FAILED” Error
Code:
Solution 1:
Installing PyOpenSSL using pip will work without converting to PEM
Solution 2:
- If you have installed Python 3.6 on OSX and are getting the "SSL: CERTIFICATE_VERIFY_FAILED" error when trying to connect to an https:// site, it's probably because Python 3.6 on OSX has no certificates at all, and can't validate any SSL connections.
- This is a change for 3.6 on OSX, and requires a post-install step, which installs the
certifi
package of certificates. - This is documented in the ReadMe, which you should find at
/Applications/Python\ 3.6/ReadMe.rtf
- The ReadMe will have you run this post-install script, which just installs
certifi
:/Applications/Python\ 3.6/Install\ Certificates.command
Solution 3:
- On Windows, Python does not look at the system certificate, it uses its own located at
?\lib\site-packages\certifi\cacert.pem.
- The solution to our problem:
- Download the domain validation certificate as *.crt or *pem file
- Open the file in editor and copy it's content to clipboard
- Find our
cacert.pem
location:from requests.utils import DEFAULT_CA_BUNDLE_PATH; print(DEFAULT_CA_BUNDLE_PATH)
- Edit the
cacert.pem
file and paste our domain validation certificate at the end of the file. - Save the file and enjoy requests!
Read Also
pg config executable not found.Solution 4:
- If we just want to bypass verification, we can create a new SSLContext. By default newly created contexts use CERT_NONE
- When calling the SSLContext constructor directly, CERT_NONE is the default.
- Since it does not authenticate the other peer, it can be insecure, especially in client mode where most of time we would like to ensure the authenticity of the server we’re talking to.
- Therefore, when in client mode, it is highly recommended to use CERT_REQUIRED.
- But if we want it to work for some other reason you can do the following, we'll have to
import ssl
as well:
- This should get round our problem but we're not really solving any of the issues, but we won't see the
[SSL: CERTIFICATE_VERIFY_FAILED]
because we now aren't verifying the cert!
- This PEP proposes to enable verification of X509 certificate signatures, as well as hostname verification for Python's HTTP clients by default, subject to opt-out on a per-call basis. This change would be applied to Python 2.7, Python 3.4, and Python 3.5.
- It also features a highly discouraged option via monkeypatching which we don't often see in python:
- Which overrides the default function for context creation with the function to create an unverified context.
Solution 5:
- In Python 3.6.1 on MacOs Sierra
Entering this in the bash terminal solved the problem: