Python 蟒蛇3';s urllib验证自签名证书

Python 蟒蛇3';s urllib验证自签名证书,python,ssl,urllib,self-signed,Python,Ssl,Urllib,Self Signed,我希望向使用我的自签名证书的URL发出一个经过验证的请求(使用Python 3.5的urllib) 下面是一个简单的例子: 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 s

我希望向使用我的自签名证书的URL发出一个经过验证的请求(使用Python 3.5的urllib)

下面是一个简单的例子:

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)

“奥利弗,查尔斯沃思,我想确认我得到的SSL证书确实是我的自我签名证书,还是放在我的前面,如果我相信我自己的密钥,我的SSL会话应该是有效的,因此这里没有中间人。”
<urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:720)>
# <version 2>
ssl_ctx = ssl.create_default_context()
ssl_ctx.check_hostname = False
ssl_ctx.verify_mode = ssl.CERT_NONE
# </version 2>