2024-06-28 15:02:17 发布
网友
我想在请求带有内部公司链接的服务器时忽略certification validation。
certification validation
使用pythonrequests库,我可以做到:
requests
r = requests.get(link, allow_redirects=False,verify=False)
如何处理urllib2库?
默认情况下,urllib2不验证服务器证书。检查这个documentation.
编辑:正如下面的注释所指出的,对于Python的较新版本(似乎>;=2.7.9)来说,这已经不再是事实。请参阅下面的ANSWER
最简单的方法:
python 2
import urllib2, ssl request = urllib2.Request('https://somedomain.co/') response = urllib2.urlopen(request, context=ssl._create_unverified_context())
python 3
from urllib.request import urlopen import ssl response = urlopen('https://somedomain.co', context=ssl._create_unverified_context())
同时,urllib2似乎在默认情况下验证服务器证书。 我和2.7.9的warning, that was shown in the pastdisappeared在一个带有自签名证书的测试环境(和Python 2.7.9)中遇到了这个问题。
My邪恶的解决方案(不要在生产中这样做!)以下内容:
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)
直接调用SSLContext构造函数也应该有效。我没试过。
默认情况下,urllib2不验证服务器证书。检查这个documentation.
编辑:正如下面的注释所指出的,对于Python的较新版本(似乎>;=2.7.9)来说,这已经不再是事实。请参阅下面的ANSWER
最简单的方法:
python 2
python 3
同时,urllib2似乎在默认情况下验证服务器证书。 我和2.7.9的warning, that was shown in the pastdisappeared在一个带有自签名证书的测试环境(和Python 2.7.9)中遇到了这个问题。
My邪恶的解决方案(不要在生产中这样做!)以下内容:
直接调用SSLContext构造函数也应该有效。我没试过。
相关问题 更多 >
编程相关推荐