加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

Python Twisted:SSL例程,ssl3_get_server_certificate错误

发布时间:2020-12-20 13:06:35 所属栏目:Python 来源:网络整理
导读:我正在使用 http://twistedmatrix.com/documents/current/core/howto/ssl.html中的check_server_certificate.py代码示例 ?????????????????????????????????????????????????????????????????????????????????为了从公共服务器获取证书. from __future__ imp
我正在使用 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.0rnrn")
            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,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')]

有任何想法吗?

解决方法

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

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读