我得到以下错误:
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。我怎样才能让它绕过验证呢?
水蟒的解决方案
我的设置是带有代理的MacOS上的Anaconda Python 3.7。路径不同。
这是如何获得正确的证书路径:
import ssl
ssl.get_default_verify_paths()
我的系统产生了什么
Out[35]: DefaultVerifyPaths(cafile='/miniconda3/ssl/cert.pem', capath=None,
openssl_cafile_env='SSL_CERT_FILE', openssl_cafile='/miniconda3/ssl/cert.pem',
openssl_capath_env='SSL_CERT_DIR', openssl_capath='/miniconda3/ssl/certs')
一旦知道了证书的位置,就可以将代理使用的证书连接到该文件的末尾。
我已经设置了conda与我的代理工作,通过运行:
conda config --set ssl_verify <pathToYourFile>.crt
如果你不记得你的证书在哪里,你可以在~/.condarc中找到它:
ssl_verify: <pathToYourFile>.crt
现在将该文件连接到/miniconda3/ssl/cert.pem文件的末尾
请求应该起作用,尤其是sklearn。数据集和类似的工具
应该工作。
进一步的说明
其他解决方案没有工作,因为Anaconda设置略有不同:
路径为Applications/Python\ 3。X根本不存在。
下面命令提供的路径是错误的路径
from requests.utils import DEFAULT_CA_BUNDLE_PATH
DEFAULT_CA_BUNDLE_PATH
另一个水蟒解决方案。我在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
I was getting the same error, and also went on a wild goose chase for quite a while before I gave up and started trying things on my own. I eventually figured it out, so I thought I'd share. In my case, I am running Python 2.7.10 (due to reasons beyond my control) on Linux, don't have access to the requests module, can't install certificates globally at the OS or Python level, can't set any environment variables, and need to access a specific internal site that uses internally issued certificates.
注意:禁用SSL验证从来不是一个选项。我正在下载一个脚本,它可以立即以根用户的身份运行。没有SSL验证,任何web服务器都可以假装是我的目标主机,而我只是接受他们给我的任何东西,并以root身份运行它!
我将根证书和中间证书(可能不止一个)以pem格式保存到一个文件中,然后使用以下代码:
import ssl,urllib2
data = urllib2.build_opener(urllib2.HTTPSHandler(context=ssl.create_default_context(cafile='/path/to/ca-cert-chain.pem')), urllib2.ProxyHandler({})).open('https://your-site.com/somefile').read()
print(data)
注意,我在那里添加了urllib2.ProxyHandler({})。这是因为在我们的环境中,代理是默认设置的,但它们只能访问外部站点,不能访问内部站点。如果没有代理绕过,我就无法访问内部站点。如果你没有这个问题,你可以简化如下:
data = urllib2.build_opener(urllib2.HTTPSHandler(context=ssl.create_default_context(cafile='/path/to/ca-cert-chain.pem'))).open('https://your-site.com/somefile').read()
工作起来很有魅力,而且不会危及安全。
享受吧!