我得到以下错误:

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上的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

其他回答

我通过关闭Fiddler(一个HTTP调试代理)来解决这个问题,检查是否启用了代理并重试。

在Windows上,Python不会查看系统证书,它使用自己的位于?\lib\site-packages\certifi\cacert.pem的证书。

问题的解决方案:

下载“*”格式的域验证证书。CRT或*pem文件 在编辑器中打开文件并将其内容复制到剪贴板 找到你的cacert。Pem位置:来自请求。导入DEFAULT_CA_BUNDLE_PATH;打印(DEFAULT_CA_BUNDLE_PATH) 编辑cacert。Pem文件,并将您的域验证证书粘贴在文件的末尾。 保存文件并享受请求!

在Mac上安装证书解决了我的问题:

pip install certifi

如果你在vCenter 6上,你应该将你vCenter的vmware证书颁发机构证书添加到你的操作系统的受信任CA列表中。要下载证书,请执行以下操作

打开Web浏览器。 导航到https:// 在右下角单击“下载受信任的根CA”链接

在Fedora

解压缩并将扩展名从.0更改为.cer 将其复制到/etc/pki/ca-trust/source/anchors/ 执行update-ca-trust命令。

链接:

https://virtualizationreview.com/articles/2015/04/02/install-root-self-signed-certificate-vcenter-6.aspx?m=1 http://forums.fedoraforum.org/showthread.php?t=293856

Craig Glennie的回答是:

MacOs Sierra上的Python 3.6.1

在bash终端中输入这个可以解决问题:

pip install certifi
/Applications/Python\ 3.6/Install\ Certificates.command