当我在 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/