Home >Backend Development >Python Tutorial >How to Fix the 'SSL: CERTIFICATE_VERIFY_FAILED' Error in urllib?

How to Fix the 'SSL: CERTIFICATE_VERIFY_FAILED' Error in urllib?

Mary-Kate Olsen
Mary-Kate OlsenOriginal
2024-12-14 01:10:11154browse

How to Fix the

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:

  1. Install the certifi package: pip install certifi
  2. Run the following command in Terminal: /Applications/Python 3.6/Install Certificates.command

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn