近期使用了一段开源的python脚本,脚本中使用了urllib2来作为HTTP客户端,在访问https站点时报下面的错误:
First request to SP failed: <urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] cert
ificate verify failed (_ssl.c:726)>
代码如下:
# create a cookie jar and cookie handler
cookie_jar = cookielib.LWPCookieJar()
cookie_handler = urllib2.HTTPCookieProcessor(cookie_jar)
# need an instance of HTTPS handler to do HTTPS
httpsHandler = urllib2.HTTPSHandler(debuglevel = 0)
if debug:
httpsHandler.set_http_debuglevel(1)
# create the base opener object
opener = urllib2.build_opener(cookie_handler, httpsHandler)
# headers needed to indicate to the SP an ECP request
headers = {
'Accept' : 'text/html; application/vnd.paos+xml',
'PAOS' : 'ver="urn:liberty:paos:2003-08";"urn:oasis:names:tc:SAML:2.0:profiles:SSO:ecp"'
}
# request target from SP
request = urllib2.Request(url=sp_target,headers=headers)
try:
response = opener.open(request)
except Exception, e:
print >>sys.stderr, "First request to SP failed: %s" % e
sys.exit(1)
最终通过设置ssl unverified的context解决。
import ssl
ssl._create_default_https_context = ssl._create_unverified_context
Python urllib2 访问HTTPS证书验证失败解决方案
在尝试使用urllib2访问HTTPS站点时遇到CERTIFICATE_VERIFY_FAILED错误。解决方法是通过设置SSL未验证的上下文环境。
5万+

被折叠的 条评论
为什么被折叠?



