我有一个用于测试的服务器设置,带有自签名证书,并且希望能够测试它.
How do you ignore SSL verification in the Python 3 version of 100?
我找到的所有信息都是关于urllib2
或Python 2的.
python 3中的urllib
已从urllib2
更改为:
Python 2, urllib2: urllib2.urlopen(url[, data[, timeout[, cafile[, capath[, cadefault[, context]]]]])
https://docs.python.org/2/library/urllib2.html#urllib2.urlopen
Python 3: urllib.request.urlopen(url[, data][, timeout])
所以我知道这可以在Python2中通过以下方式实现.然而,Python 3 urlopen
缺少上下文参数.
import urllib2
import ssl
ctx = ssl.create_default_context()
ctx.check_hostname = False
ctx.verify_mode = ssl.CERT_NONE
urllib2.urlopen("https://your-test-server.local", context=ctx)
是的,我知道这是个坏主意.这只适用于在专用服务器上进行测试.
我在Python3文档或任何其他问题中都找不到应该如何做到这一点.即使是那些明确提到Python 3的人,仍然有一个针对urllib2/Python 2的解决方案.