I’m trying to test my blog site by the urllib.request. Unfortunately, my blog site has SSL (Secure Sockets Layer) enabled, and returns an urlopen error, which is shown below:
The following simple code is running on my Macbook Pro. But these three lines Python code run successfully without any error on Linux.
from urllib.request import urlopen
zheli = urlopen(‘https://zheli.com.au’)
print(zheli.read())
To fix this error, I import ssl to create SSLContext object which makes the system’s default CA certificates to be trusted.
from urllib.request import urlopen
import sslctx = ssl.create_default_context()
ctx.check_hostname = False
ctx.verify_mode = ssl.CERT_NONEzheli = urlopen(‘https://zheli.com.au’, context=ctx)
print(zheli.read())
The question is why the same Python code has no error on Linux, but gets an ssl failed error on MacBook Pro.
References: