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

Dav*_*ein 3 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'>>]
Run Code Online (Sandbox Code Playgroud)

我使用的是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()
Run Code Online (Sandbox Code Playgroud)

提前致谢!

Gly*_*yph 5

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

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