Categories
Python Answers

How to fix urllib and “SSL: CERTIFICATE_VERIFY_FAILED” Error with Python?

Spread the love

Sometimes, we want to fix urllib and "SSL: CERTIFICATE_VERIFY_FAILED" Error with Python.

In this article, we’ll look at how to fix urllib and "SSL: CERTIFICATE_VERIFY_FAILED" Error with Python.

How to fix urllib and "SSL: CERTIFICATE_VERIFY_FAILED" Error with Python?

To fix urllib and "SSL: CERTIFICATE_VERIFY_FAILED" Error with Python, we can work around the problem with SSLContext.

The workaround bypasses the verification of the certificate.

For instance, we write

url = "https://example.com"
req = urllib2.Request(url)
gcontext = ssl.SSLContext()
info = urllib2.urlopen(req, context=gcontext).read()

to create a SSLContext instance with

gcontext = ssl.SSLContext()

Then we call urlopen with gcontext to make a GET request to the url.

Conclusion

To fix urllib and "SSL: CERTIFICATE_VERIFY_FAILED" Error with Python, we can work around the problem with SSLContext.

The workaround bypasses the verification of the certificate.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *