由于 Windows 上的信任库为空,python treq 因 Twisted/OpenSSL 错误而失败

标签 python windows ssl twisted

当我在 http://www.google.com 上运行以下示例代码时, 它工作正常 但是当我试穿 https://www.google.com 时,我得到这个错误:

Requesting https://www.google.com

Unhandled error in Deferred:
Unhandled Error
Traceback (most recent call last):
Failure: twisted.web._newclient.ResponseNeverReceived: [<twisted.python.failure.Failure <class 'OpenSSL.SSL.Error'>>]

我正在使用 python 2.7.8、twisted 14.0.2、service_identity 14.0.0、treq 0.2.1、OpenSSL 0.14

import treq
from twisted.internet import reactor, defer
import sys

@defer.inlineCallbacks
def doit(url):
    print "Requesting "+ url + "\n"
    results = yield treq.get(url)
    print "...got results\n"
    content = yield results.content()
    print "%s"%content
    reactor.stop()

def main():
    url = sys.argv[1]
    reactor.callLater(0, doit, url)
    reactor.run()

if __name__ == '__main__':
    main()

提前致谢!

最佳答案

这里的问题很可能是您没有选择任何信任根,因此 OpenSSL 无法验证连接。

您可以通过执行 pip install certifi 以 OpenSSL 可以使用的格式获得一些信任根,然后将您的 SSL_CERT_FILE 环境变量设置为指向 python -m 证书。在 Python 中,您可以使用 import certifi; os.environ["SSL_CERT_FILE"] = certifi.where() 在脚本的最顶部(在导入任何 OpenSSL 绑定(bind)之前)。

关于由于 Windows 上的信任库为空,python treq 因 Twisted/OpenSSL 错误而失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34358935/

相关文章:

python - 网格中的 Gtk 滚动窗口大小

windows - 使用 win 批处理脚本并排比较文件

c - memset用于内存分配?

java - Spring Webflux WebClient - 指定发送出站请求时使用的 TLS 版本(介于 TLSv1.2 和 TLSv1.3 之间)

JAVA 使用两个 keystore 相同的应用程序

python - 哪个python脚本正在运行

python - 如何使用 pandas DataFrame 将此计算推广到任意数量的列?

Python 3 if/elif/else 运算符总是转到 else

linux - 可以在 Linux 上编译 Windows 代码吗?

java - "java.net.SocketTimeoutException: connect timed out"我的客户端无法调用网络服务