I wish to make a verified request (using Python 3.5's urllib) to a URL that is using my self-signed certificate.
Here is a minimal example:
import ssl
from urllib.request import urlopen
from urllib.error import URLError
# <version 1>
ssl_ctx = ssl.SSLContext(ssl.PROTOCOL_SSLv23)
ssl_ctx.verify_mode = ssl.CERT_REQUIRED
ssl_ctx.load_cert_chain(certfile="/path/to/cert.pem", keyfile="/path/to/cert.key")
# </version 1>
try:
urlopen("https://localhost/", context=ssl_ctx)
except URLError as e:
print(e)
This prints:
<urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:720)>
- I checked the value of
ssl_ctx.get_ca_certs()
->[]
- I also check the value of
ssl_ctx.cert_store_stats()
->{'x509_ca': 0, 'crl': 0, 'x509': 0}
As pointed by an answer given for Python 3 urllib with self-signed certificates, I can work around this with:
# <version 2>
ssl_ctx = ssl.create_default_context()
ssl_ctx.check_hostname = False
ssl_ctx.verify_mode = ssl.CERT_NONE
# </version 2>
But is there a way to actually verify my cert?