使用urllib2时出现ssl:certificate_verify_failed

在使用urllib2做一个接口调用时,出现了ssl问题,之前只在使用requests库的时候遇到过,只是记得当时在请求函数中增加参数verify并设置为False,不过urllib2用这招不灵。

经过查找资料,发现解决方法:

使用ssl创建未验证的上下文,在url中传入上下文参数(当项目整体非常重视安全问题时,推荐这种方式,可以局部取消证书验证):


import ssl
 
context = ssl._create_unverified_context()

.
.
.

response = urllib2.urlopen(request, context=context)

今天在使用一份测试代码时,由于现在整体项目对安全性做了验证,直接导致测试代码中调用api的代码全部异常,因为有很多处都需要调用api,如果一个一个的改非常麻烦,幸好ssl还可以全局取消安全验证。

import ssl
 
ssl._create_default_https_context = ssl._create_unverified_context

参考链接:

https://blog.csdn.net/yixieling4397/article/details/79861379

猜你喜欢

转载自blog.csdn.net/qq_28292349/article/details/81281797