Python Twisted:SSL例程,ssl3_get_server_certificate错误

时间:2015-08-03 18:18:10

标签: python ssl twisted

我正在使用http://twistedmatrix.com/documents/current/core/howto/ssl.html中的 check_server_certificate.py 代码示例                                                                                  为了从公共服务器获取证书。

from __future__ import print_function
import sys
from twisted.internet import defer, endpoints, protocol, ssl, task, error

def main(reactor, host, port=443):
    options = ssl.optionsForClientTLS(hostname=host.decode('utf-8'))
    port = int(port)

    class ShowCertificate(protocol.Protocol):
        def connectionMade(self):
            self.transport.write(b"GET / HTTP/1.0\r\n\r\n")
            self.done = defer.Deferred()
        def dataReceived(self, data):
            certificate = ssl.Certificate(self.transport.getPeerCertificate())
            print("OK:", certificate)
            self.transport.abortConnection()
        def connectionLost(self, reason):
            print("Lost.")
            if not reason.check(error.ConnectionClosed):
                print("BAD:", reason.value)
            self.done.callback(None)

    return endpoints.connectProtocol(
        endpoints.SSL4ClientEndpoint(reactor, host, port, options),
        ShowCertificate()
    ).addCallback(lambda protocol: protocol.done)

task.react(main, sys.argv[1:])

从PyPI安装了必要的“service_identity”和“idna”软件包并仍然出现此错误:

$ python check_server_certificate.py www.twistedmatrix.com
Lost.
BAD: [('SSL routines', 'ssl3_get_server_certificate', 'certificate verify failed')]

任何想法?

1 个答案:

答案 0 :(得分:0)

SSL_CERT_FILE = "$(python -m certifi)" 
python check_server_certificate.py www.twistedmatrix.com

尝试使用它,它对我有用。