我得到以下错误:

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


当前回答

I was getting the same error, and also went on a wild goose chase for quite a while before I gave up and started trying things on my own. I eventually figured it out, so I thought I'd share. In my case, I am running Python 2.7.10 (due to reasons beyond my control) on Linux, don't have access to the requests module, can't install certificates globally at the OS or Python level, can't set any environment variables, and need to access a specific internal site that uses internally issued certificates.

注意:禁用SSL验证从来不是一个选项。我正在下载一个脚本,它可以立即以根用户的身份运行。没有SSL验证,任何web服务器都可以假装是我的目标主机,而我只是接受他们给我的任何东西,并以root身份运行它!

我将根证书和中间证书(可能不止一个)以pem格式保存到一个文件中,然后使用以下代码:

import ssl,urllib2
data = urllib2.build_opener(urllib2.HTTPSHandler(context=ssl.create_default_context(cafile='/path/to/ca-cert-chain.pem')), urllib2.ProxyHandler({})).open('https://your-site.com/somefile').read()
print(data)

注意,我在那里添加了urllib2.ProxyHandler({})。这是因为在我们的环境中,代理是默认设置的,但它们只能访问外部站点,不能访问内部站点。如果没有代理绕过,我就无法访问内部站点。如果你没有这个问题,你可以简化如下:

data = urllib2.build_opener(urllib2.HTTPSHandler(context=ssl.create_default_context(cafile='/path/to/ca-cert-chain.pem'))).open('https://your-site.com/somefile').read()

工作起来很有魅力,而且不会危及安全。

享受吧!

其他回答

在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

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

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

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

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

我很惊讶所有这些指导都没有解决我的问题。尽管如此,诊断是正确的(顺便说一句,我使用Mac和Python3.6.1)。所以,总结一下正确的部分:

在Mac上,苹果放弃了OpenSSL Python现在使用它自己的CA根证书集 二进制Python安装提供了一个脚本来安装Python所需的CA根证书("/Applications/Python 3.6/ install Certificates.command") 详细信息请阅读“/Applications/Python 3.6/ReadMe.rtf”

对我来说,脚本不能工作,所有那些证书和openssl安装也未能修复。也许是因为我安装了多个python2和python3,以及许多virtualenv。最后,我需要手工修理它。

pip install certifi   # for your virtualenv
mkdir -p /Library/Frameworks/Python.framework/Versions/3.6/etc/openssl
cp -a <your virtualenv>/site-package/certifi/cacert.pem \
  /Library/Frameworks/Python.framework/Versions/3.6/etc/openssl/cert.pem

如果你还不满意的话。然后重新安装OpenSSL。

port install openssl

Try

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

这对我很管用。

如果你有私人证书要处理,比如你的组织自己的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'))

希望这能有所帮助