我想certification validation在使用内部企业链接向服务器发出请求时忽略它.
使用python requests库我会这样做:
r = requests.get(link, allow_redirects=False,verify=False)
Run Code Online (Sandbox Code Playgroud)
如何使用urllib2库进行相同的操作?
这项工作很好:
import urllib2
opener = urllib2.build_opener(
urllib2.HTTPHandler(),
urllib2.HTTPSHandler(),
urllib2.ProxyHandler({'http': 'http://user:pass@proxy:3128'}))
urllib2.install_opener(opener)
print urllib2.urlopen('http://www.google.com').read()
Run Code Online (Sandbox Code Playgroud)
但是,如果http更改为https:
...
print urllib2.urlopen('https://www.google.com').read()
Run Code Online (Sandbox Code Playgroud)
有错误:
Traceback (most recent call last):
File "D:\Temp\6\tmp.py", line 13, in <module>
print urllib2.urlopen('https://www.google.com').read()
File "C:\Python26\lib\urllib2.py", line 124, in urlopen
return _opener.open(url, data, timeout)
File "C:\Python26\lib\urllib2.py", line 389, in open
response = self._open(req, data)
File "C:\Python26\lib\urllib2.py", line 407, in _open
'_open', req)
File "C:\Python26\lib\urllib2.py", line 367, in _call_chain
result = func(*args)
File "C:\Python26\lib\urllib2.py", line 1154, in https_open
return self.do_open(httplib.HTTPSConnection, …Run Code Online (Sandbox Code Playgroud) 我使用代理集作为环境变量(export http_proxy = example.com).对于使用urllib2的一个调用,我需要暂时禁用它,即.取消设置http_proxy.我已经尝试了文档和互联网中建议的各种方法,但到目前为止一直无法取消代理.到目前为止,我尝试过:
# doesn't work
req = urllib2.Request('http://www.google.com')
req.set_proxy(None,None)
urllib2.urlopen(req)
# also doesn't work
urllib.getproxies = lambda x = None: {}
Run Code Online (Sandbox Code Playgroud)