我收到以下错误:
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”的最佳 Google 结果,它让我大吃一惊。
如果您在 OSX 上安装了 Python 3.6,并且在尝试连接到 https://站点时遇到“SSL:CERTIFICATE_VERIFY_FAILED”错误,这可能是因为 OSX 上的 Python 3.6 根本没有证书,并且不能验证任何 SSL 连接。这是对 OSX 3.6 的更改,需要安装后的步骤,即安装 certifi
证书包。这记录在文件 ReadMe.rtf 中。 ,您可以在 /Applications/Python\3.6/ReadMe.rtf
中找到它(另请参阅文件 Conclusion.rtf 和脚本 build-installer.py
生成 macOS 安装程序)。
自述文件将让您在以下位置运行安装后脚本
/Applications/Python\3.10/Install\Certificates.command
(终端应用程序,仅此命令即可解决问题。请务必使用您当前的子版本更新文件路径。)
(其来源是 install_certificates.command
),其中:
- 第一 installs the Python package
certifi
, 和 - 然后creates a symbolic link从 OpenSSL 证书文件到包
certifi
安装的证书文件。
发行说明有更多信息: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
关于python - urllib 和 "SSL: CERTIFICATE_VERIFY_FAILED"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27835619/