解决python2.x用urllib2证书验证错误, _create_unverified_context

解决以下错误:

错误1:AttributeError: 'module' object has no attribute '_create_unverified_context',

错误2:URLError: <urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:661)>

 

在代码中加入以下代码:

复制代码
 1 import ssl
 2 
 3 try:
 4     _create_unverified_https_context = ssl._create_unverified_context
 5 except AttributeError:
 6     # Legacy Python that doesn't verify HTTPS certificates by default
 7     pass
 8 else:
 9     # Handle target environment that doesn't support HTTPS verification
10     ssl._create_default_https_context = _create_unverified_https_context
posted @ 2020-08-18 09:47  sunyllove  阅读(646)  评论(0编辑  收藏  举报