SSL:CERTIFICATE_VERIFY_FAILED 当 运行 在 Python 3.8 上。适用于 python 的其他版本
SSL: CERTIFICATE_VERIFY_FAILED when running on Python 3.8. Works on other versions of python
我在 macOS 上使用 Python 3.8。我在这台机器上也有 Python 3.5 和 2.7。
我正在尝试使用以下代码段连接到 adwords.google.com:
import ssl, socket
context = ssl.create_default_context()
conn = context.wrap_socket(
socket.socket(socket.AF_INET),
server_hostname="adwords.google.com"
)
conn.connect(("adwords.google.com", 443))
cert = conn.getpeercert()
但我总是收到以下错误:
ssl.SSLCertVerificationError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate
运行 ssl.OPENSSL_VERSION 我收到:
'OpenSSL 1.1.1g 21 Apr 2020'
奇怪的是,如果我在 Python 3.5 或 Python 2.7 上 运行 相同的 ssl 连接片段,则连接有效。这仅发生在 Python 3.8.
为什么会这样?有什么想法吗?
我找到了阅读 this article and this question on SO: 的答案。
自 Python 3.6 起,Apple-supplied OpenSSL 库已弃用,不再使用。这也意味着由钥匙串访问应用程序和安全命令行实用程序管理的系统和用户钥匙串中的信任证书不再被 Python ssl 模块用作默认值。对于 3.6.0,示例命令脚本包含在 /Applications/Python 3.6 中,用于安装来自 third-party certifi 包 (https://pypi.python.org/pypi/certifi) [...]
所以,我只是 运行 sh /Applications/Python\ 3.8/Install\ Certificates.command,正确安装了 certifi,并且 ssl 连接也正常工作。
我在 macOS 上使用 Python 3.8。我在这台机器上也有 Python 3.5 和 2.7。
我正在尝试使用以下代码段连接到 adwords.google.com:
import ssl, socket
context = ssl.create_default_context()
conn = context.wrap_socket(
socket.socket(socket.AF_INET),
server_hostname="adwords.google.com"
)
conn.connect(("adwords.google.com", 443))
cert = conn.getpeercert()
但我总是收到以下错误:
ssl.SSLCertVerificationError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate
运行 ssl.OPENSSL_VERSION 我收到:
'OpenSSL 1.1.1g 21 Apr 2020'
奇怪的是,如果我在 Python 3.5 或 Python 2.7 上 运行 相同的 ssl 连接片段,则连接有效。这仅发生在 Python 3.8.
为什么会这样?有什么想法吗?
我找到了阅读 this article and this question on SO:
自 Python 3.6 起,Apple-supplied OpenSSL 库已弃用,不再使用。这也意味着由钥匙串访问应用程序和安全命令行实用程序管理的系统和用户钥匙串中的信任证书不再被 Python ssl 模块用作默认值。对于 3.6.0,示例命令脚本包含在 /Applications/Python 3.6 中,用于安装来自 third-party certifi 包 (https://pypi.python.org/pypi/certifi) [...]
所以,我只是 运行 sh /Applications/Python\ 3.8/Install\ Certificates.command,正确安装了 certifi,并且 ssl 连接也正常工作。