python - SSL:在 Python 3.8 上运行时的 CERTIFICATE_VERIFY_FAILED。适用于其他版本的python

标签 python ssl openssl

我在 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还有这个问题:Django 1.11: Python 3.6 Upgrade causes issues with SSL connections .
从 Python 3.6 开始,[...] 已弃用的 Apple 提供的 OpenSSL 库不再使用。这也意味着由 Keychain Access 应用程序和安全命令行实用程序管理的系统和用户钥匙串(keychain)中的信任证书不再被 Python ssl 模块用作默认值。对于 3.6.0,/Applications/Python 3.6 中包含一个示例命令脚本,用于从第三方 certifi 包 (https://pypi.python.org/pypi/certifi) [...]
所以,我只运行了 sh/Applications/Python\3.8/Install\Certificates.command,certifi 已正确安装,并且 ssl 连接也正常工作。

关于python - SSL:在 Python 3.8 上运行时的 CERTIFICATE_VERIFY_FAILED。适用于其他版本的python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63801481/

相关文章:

Python:使用reduce交替加法和乘法

生成器生成的 Python 3 队列,由多进程消耗

ssl - 如何使用 https 服务 devpi?

java - 客户端应该具有与服务器相同的 keystore 吗?

c - OpenSSL 1_1_0e BN_print_fp 不工作

python - 在 Docker Alpine Linux 3.3 中无法使用 OpenSSL 1.0.2g 和 Python 2.7 出现 "pip install cryptography"

c - 使用 openssl evp api(aes256cbc) 加密文件的问题

python - 从 QTreeWidgetItem 子类 PyQt/PySide 发出信号的替代方案

.net - 确保 .net librdkafka 应用程序经过 TLS 加密

python - 为什么元素的计算结果为 false?