我得到以下错误:

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。我怎样才能让它绕过验证呢?


当前回答

对于Linux Python3.6,这对我来说是可行的。

从命令行安装pyopenssl和certifi

sudo pip3 install -U pyopenssl
sudo pip3 install certifi

在我的python3脚本中,添加了verify='/usr/lib/python3.6/site-packages/certifi/cacert。Pem '是这样的:

import requests
from requests.auth import HTTPBasicAuth
import certifi

auth = HTTPBasicAuth('username', 'password')
body = {}

r = requests.post(url='https://your_url.com', data=body, auth=auth, verify='/usr/lib/python3.6/site-packages/certifi/cacert.pem')

其他回答

我有点羞愧地低下头,因为我也遇到了同样的问题,只不过在我的情况下,我点击的URL是有效的,证书是有效的。无效的是我的网络连接。我未能将代理详细信息添加到浏览器(在这种情况下是IE)。这阻止了验证过程的正确进行。 添加了代理细节,我的python非常高兴。

这里已经有很多答案了,但我们在一个非常具体的案例中遇到了这个问题,花了很多时间调查,所以再加一个。我们在下面的例子中看到:

在一个德比安式细长的码头集装箱里 默认Python 3.5.3 easy_install3 对于在Kubernetes集群中使用cert-manager注册的LetsEncrypt证书

pip3和openssl命令行都能够验证该证书,easy_install3能够成功验证其他LetsEncrypt证书。

解决办法是从源代码构建最新的Python(当时是3.7.3)。这里的说明很详细,很容易理解。

Try

PIP install——trusted.host pypi.python.org packagename

这对我很管用。

在python 2.7中,在文件C:\Python27\lib\site-packages\certifi\cacert中添加受信任根CA的详细信息。pem帮助

之后我运行(使用管理员权限) pip install——truste- host pypi.python.org——truste- host pypi.org——truste- host files.pythonhosted.org packageName

Ln -s /usr/local/share/certs/ca-root-nss。crt /etc/ssl/cert.pem

(10.1 FreeBSD)