我得到以下错误:

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 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

这段代码将撤销验证,这样就不会验证ssl证书。

其他回答

在Mac上安装证书解决了我的问题:

pip install certifi

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

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

这不是您特定问题的解决方案,但我把它放在这里是因为这个线程是“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

进口的要求 Response =请求。Get("你想要访问的url/api ", verify=" SSL证书的路径")

对我来说,问题是上面的答案没有一个完全帮助我,但给了我正确的方向。

当然,SSL证书是必要的,但当您处于公司防火墙之后,那么公开可用的证书可能就没有帮助了。您可能需要联系您公司的IT部门来获得证书,因为每个公司都使用从他们签约服务的安全提供商那里获得的特殊证书。然后把它放到一个文件夹中并把路径作为验证参数传递给那个文件夹。

对我来说,即使在尝试了上述所有解决方案并使用了错误的证书后,我也无法使其工作。因此,请记住那些在公司防火墙后的人要获得正确的证书。它可以决定请求调用的成功和失败。

在我的例子中,我把证书放在下面的路径中,它像魔术一样起作用。

C:\Program Files\Common文件\ssl

您也可以参考https://2.python-requests.org/en/master/user/advanced/#id3,它讨论ssl验证

在Windows上,Python不会查看系统证书,它使用自己的位于?\lib\site-packages\certifi\cacert.pem的证书。

问题的解决方案:

下载“*”格式的域验证证书。CRT或*pem文件 在编辑器中打开文件并将其内容复制到剪贴板 找到你的cacert。Pem位置:来自请求。导入DEFAULT_CA_BUNDLE_PATH;打印(DEFAULT_CA_BUNDLE_PATH) 编辑cacert。Pem文件,并将您的域验证证书粘贴在文件的末尾。 保存文件并享受请求!