Home >Backend Development >Python Tutorial >How to Fix the 'SSL: CERTIFICATE_VERIFY_FAILED' Error in urllib?
urllib and "SSL: CERTIFICATE_VERIFY_FAILED" Error
Encountering the "SSL: CERTIFICATE_VERIFY_FAILED" error when using urllib is a common issue, especially when trying to access HTTPS websites. This error typically indicates that the SSL certificate for the target website cannot be verified by your system.
Solution for Python 3.6 on macOS
If you're using Python 3.6 on macOS, the solution is to install the certifi package and create a symbolic link from the OpenSSL certificates file to the certificates file installed by certifi. This step is necessary because Python 3.6 on macOS does not come with pre-installed certificates.
To resolve the issue:
Bypass Certificate Verification
In some cases, you may want to bypass the SSL certificate verification altogether. This is not recommended for security reasons, but it can be done by modifying the urlopen request:
import urllib2 # Create a request object req = urllib2.Request(url) # Create a context object with no certificate verification context = ssl.SSLContext() context.verify_mode = ssl.CERT_NONE # Use the context object with the request req = urllib2.Request(url, context=context)
Note: This method should only be used in trusted environments, as it reduces the security of your application.
The above is the detailed content of How to Fix the 'SSL: CERTIFICATE_VERIFY_FAILED' Error in urllib?. For more information, please follow other related articles on the PHP Chinese website!