找了一下,有人说原因是这样的:
Python 2.7.9 之后引入了一个新特性
当你urllib.urlopen一个 https 的时候会验证一次 SSL 证书
当目标使用的是自签名的证书时就会爆出一个
urllib2.URLError: <urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:581)> 的错误消息
确实我用urllib试了一下结果一样,requests跟urllib是一样的。
那么要解决这个问题,PEP-0476的文档说
For users who wish to opt out of certificate verification on a single connection, they can achieve this by providing the contextargument to urllib.urlopen :
https://blog.csdn.net/xiaopangxia/article/details/49908889
- import ssl
- # This restores the same behavior as before.
- context = ssl._create_unverified_context()
- urllib.urlopen("https://no-valid-cert", context=context)