我得到以下错误:

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


当前回答

我对Mac OS X的解决方案:

使用从Python语言官方网站https://www.python.org/downloads/下载的本地应用程序Python安装程序升级到Python 3.6.5

我发现这个安装程序在更新新Python的链接和符号链接方面比自制的要好得多。

使用"安装新证书。/Install Certificates.command”,该命令位于刷新后的Python 3.6目录中

cd "/Applications/Python 3.6/"
sudo "./Install Certificates.command"

其他回答

使用pip安装PyOpenSSL对我来说是有效的(没有转换为PEM):

pip install PyOpenSSL

对于Centos 6/7、Fedora上的Python 3.4+,只需按如下方式安装受信任CA:

拷贝CA.crt到/etc/pki/ca-trust/source/anchors/ update-ca-trust force-enable update-ca-trust提取

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

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

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

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

import requests
requests.packages.urllib3.disable_warnings()

import ssl

try:
    _create_unverified_https_context = ssl._create_unverified_context
except AttributeError:
    # Legacy Python that doesn't verify HTTPS certificates by default
    pass
else:
    # Handle target environment that doesn't support HTTPS verification
    ssl._create_default_https_context = _create_unverified_https_context

从这里拍摄https://gist.github.com/michaelrice/a6794a017e349fc65d01

安装nltk的步骤(我已经在MAC OS X中安装了python3 (3.6.2)

sudo easy_install pip

使用ignore installed选项忽略卸载之前版本的6,否则,它会在卸载时给出一个错误,并且不进行电影转发

sudo pip3 install -U nltk --ignore-installed six

检查pip和python的安装,使用'3'版本

which python python2 python3
which pip pip2 pip3

检查NLTK是否安装

python3
import nltk
nltk.__path__
['/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/nltk']

在安装示例之前安装SSL证书,否则我们将在安装示例时证书错误

/Applications/Python\ 3.6/Install\ Certificates.command
python3 -m nltk.downloader book

这就成功地完成了nltk和nltk_ata的安装