我得到以下错误:

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


当前回答

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

其他回答

像你一样,我在我的旧iMac (OS X 10.6.8)上使用python 2.7,我也遇到了这个问题,使用urllib2。urlopen:

urlopen error [SSL: CERTIFICATE_VERIFY_FAILED]

我的程序运行得很好,没有SSL证书问题,突然(在下载程序后),它们因SSL错误而崩溃。

问题是使用的python版本:

https://www.python.org/downloads和python-2.7.9-macosx10.6.pkg没有问题 Homebrew工具安装的“brew install python”有问题,版本位于/usr/local/bin。

在/Applications/Python 2.7/ReadMe.rtf中,有一章名为证书验证和OpenSSL[已更改为Python 2.7.9],详细解释了这个问题。

因此,检查,下载并将正确版本的python放入您的PATH中。

如果你有私人证书要处理,比如你的组织自己的CA根和链的中间部分,那么最好将证书添加到CA文件中,即cacert。pem,而不是绕过整个安全设备(verify=False)。下面的代码让你在2.7+和3+

考虑添加整个证书链,当然您只需要这样做一次。

import certifi

cafile=certifi.where() # cacert file
with open ('rootca.pem','rb') as infile:
    customca=infile.read()
    with open(cafile,'ab') as outfile:
        outfile.write(customca)
with open ('interca.pem','rb') as infile:
    customca=infile.read()
    with open(cafile,'ab') as outfile:
        outfile.write(customca)
with open ('issueca.pem','rb') as infile:
    customca=infile.read()
    with open(cafile,'ab') as outfile:
        outfile.write(customca)

那这个应该能让你振作起来

import requests
response = requests.request("GET", 'https://yoursecuresite.com',  data = {})
print(response.text.encode('utf8'))

希望这能有所帮助

如果证书是通过Let's encrypt颁发的,请确保在客户机上的中间证书存储中删除过期的DST根CA X3颁发的R3证书。

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

我在我的一台Linux机器上遇到了类似的问题。生成新的证书并导出指向证书目录的环境变量,为我修复了它:

$ sudo update-ca-certificates --fresh
$ export SSL_CERT_DIR=/etc/ssl/certs