我得到以下错误:
Exception in thread Thread-3:
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/threading.py", line 810, in __bootstrap_inner
self.run()
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/threading.py", line 763, in run
self.__target(*self.__args, **self.__kwargs)
File "/Users/Matthew/Desktop/Skypebot 2.0/bot.py", line 271, in process
info = urllib2.urlopen(req).read()
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 154, in urlopen
return opener.open(url, data, timeout)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 431, in open
response = self._open(req, data)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 449, in _open
'_open', req)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 409, in _call_chain
result = func(*args)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 1240, in https_open
context=self._context)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 1197, in do_open
raise URLError(err)
URLError: <urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:581)>
下面是导致这个错误的代码:
if input.startswith("!web"):
input = input.replace("!web ", "")
url = "https://domainsearch.p.mashape.com/index.php?name=" + input
req = urllib2.Request(url, headers={ 'X-Mashape-Key': 'XXXXXXXXXXXXXXXXXXXX' })
info = urllib2.urlopen(req).read()
Message.Chat.SendMessage ("" + info)
我正在使用的API要求我使用HTTPS。我怎样才能让它绕过验证呢?
进口的要求
Response =请求。Get("你想要访问的url/api ", verify=" SSL证书的路径")
对我来说,问题是上面的答案没有一个完全帮助我,但给了我正确的方向。
当然,SSL证书是必要的,但当您处于公司防火墙之后,那么公开可用的证书可能就没有帮助了。您可能需要联系您公司的IT部门来获得证书,因为每个公司都使用从他们签约服务的安全提供商那里获得的特殊证书。然后把它放到一个文件夹中并把路径作为验证参数传递给那个文件夹。
对我来说,即使在尝试了上述所有解决方案并使用了错误的证书后,我也无法使其工作。因此,请记住那些在公司防火墙后的人要获得正确的证书。它可以决定请求调用的成功和失败。
在我的例子中,我把证书放在下面的路径中,它像魔术一样起作用。
C:\Program Files\Common文件\ssl
您也可以参考https://2.python-requests.org/en/master/user/advanced/#id3,它讨论ssl验证
我也遇到了类似的问题,不过我在Python 3.4、3.5和3.6中使用了urllib.request.urlopen。(这是Python 3中urllib2的一部分,根据Python 2的urllib2文档页面头部的注释。)
我的解决方案是pip install certifi来安装certifi,它有:
... 一个精心策划的根证书集合,用于验证SSL证书的可信度,同时验证TLS主机的身份。
然后,在我的代码中,我之前只有:
import urllib.request as urlrq
resp = urlrq.urlopen('https://example.com/bar/baz.html')
我将其修改为:
import urllib.request as urlrq
import certifi
resp = urlrq.urlopen('https://example.com/bar/baz.html', cafile=certifi.where())
如果我读取urllib2。Urlopen文档正确,它也有一个cafile参数。所以,urllib2.urlopen([…], certificate .where())可能也适用于Python 2.7。
更新(2020-01-01):自Python 3.6起,已弃用urlopen的cafile参数,取而代之的应该是指定context参数。我发现以下功能在3.5到3.8版本中同样有效:
import urllib.request as urlrq
import certifi
import ssl
resp = urlrq.urlopen('https://example.com/bar/baz.html', context=ssl.create_default_context(cafile=certifi.where()))
另一个水蟒解决方案。我在macOS上的Python 2.7环境中获得了CERTIFICATE_VERIFY_FAILED。事实证明conda路径很糟糕:
基地(3.7)环境:
>>> import ssl
>>> ssl.get_default_verify_paths()
DefaultVerifyPaths(cafile='/usr/local/anaconda3/ssl/cert.pem', capath=None, openssl_cafile_env='SSL_CERT_FILE', openssl_cafile='/usr/local/anaconda3/ssl/cert.pem', openssl_capath_env='SSL_CERT_DIR', openssl_capath='/usr/local/anaconda3/ssl/certs')
2.7 environment(路径不存在!)
DefaultVerifyPaths(cafile='', capath=None, openssl_cafile_env='SSL_CERT_FILE', openssl_cafile='/usr/local/anaconda3/envs/py27/ssl/cert.pem', openssl_capath_env='SSL_CERT_DIR', openssl_capath='/usr/local/anaconda3/envs/py27/ssl/certs')
解决办法:
cd /usr/local/anaconda3/envs/py27/
mkdir ssl
cd ssl
ln -s ../../../ssl/cert.pem
如果你有私人证书要处理,比如你的组织自己的CA根和链的中间部分,那么最好将证书添加到CA文件中,即cacert。pem,而不是绕过整个安全设备(verify=False)。下面的代码让你在2.7+和3+
考虑添加整个证书链,当然您只需要这样做一次。
import certifi
cafile=certifi.where() # cacert file
with open ('rootca.pem','rb') as infile:
customca=infile.read()
with open(cafile,'ab') as outfile:
outfile.write(customca)
with open ('interca.pem','rb') as infile:
customca=infile.read()
with open(cafile,'ab') as outfile:
outfile.write(customca)
with open ('issueca.pem','rb') as infile:
customca=infile.read()
with open(cafile,'ab') as outfile:
outfile.write(customca)
那这个应该能让你振作起来
import requests
response = requests.request("GET", 'https://yoursecuresite.com', data = {})
print(response.text.encode('utf8'))
希望这能有所帮助