我得到以下错误:

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


当前回答

这不是您特定问题的解决方案,但我把它放在这里是因为这个线程是“SSL: CERTIFICATE_VERIFY_FAILED”的顶部谷歌结果,它导致我在一个徒劳的追逐。

If you have installed Python 3.6 on OSX and are getting the "SSL: CERTIFICATE_VERIFY_FAILED" error when trying to connect to an https:// site, it's probably because Python 3.6 on OSX has no certificates at all, and can't validate any SSL connections. This is a change for 3.6 on OSX, and requires a post-install step, which installs the certifi package of certificates. This is documented in the file ReadMe.rtf, which you can find at /Applications/Python\ 3.6/ReadMe.rtf (see also the file Conclusion.rtf, and the script build-installer.py that generates the macOS installer).

ReadMe会让你运行安装后的脚本

/Applications/Python\ 3.10/Install\ Certificates.command(终端应用程序,这个命令应该单独解决问题。请确保使用当前的subversion更新文件路径。)

(它的源是install_certificates.command),其中:

首先安装Python包certifi,然后 然后创建一个从OpenSSL证书文件到包certificate安装的证书文件的符号链接。

发布说明有更多信息:https://www.python.org/downloads/release/python-360/

在较新的Python版本中,有更多关于此的文档:

https://github.com/python/cpython/blob/e05a703848473b0365886dcc593cbddc46609f29/Mac/BuildScript/resources/ReadMe.rtf#L22-L34 https://github.com/python/cpython/blob/e05a703848473b0365886dcc593cbddc46609f29/Mac/BuildScript/resources/Conclusion.rtf#L15-L19 https://github.com/python/cpython/blob/e05a703848473b0365886dcc593cbddc46609f29/Mac/BuildScript/resources/Welcome.rtf#L23-L25 https://github.com/python/cpython/blob/e05a703848473b0365886dcc593cbddc46609f29/Mac/BuildScript/resources/install_certificates.command https://github.com/python/cpython/blob/e05a703848473b0365886dcc593cbddc46609f29/Mac/BuildScript/README.rst https://github.com/python/cpython/blob/e05a703848473b0365886dcc593cbddc46609f29/Mac/BuildScript/build-installer.py#L239-L246

其他回答

就像我在评论中写的,这个问题可能与这个SO答案有关。

简而言之:有多种方法来验证证书。OpenSSL使用的验证与系统上的受信任根证书不兼容。OpenSSL是Python使用的。

您可以尝试获取Verisign Class 3 Public Primary Certification Authority缺少的证书,然后根据Python文档使用cafile选项:

urllib2.urlopen(req, cafile="verisign.pem")

安装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的安装

对于任何使用mechanize遇到这个问题的人,下面是如何将相同的技术应用到mechanize Browser实例:

br = mechanize.Browser()
context = ssl._create_unverified_context()
br.set_ca_data(context=context)

Amazon EC2上的Python 2.7,附带centOS 7

我必须设置env变量SSL_CERT_DIR指向我的ca-bundle,它位于/etc/ssl/certs/ca-bundle.crt

我对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"