使用Python3获取TLS证书颁发者

时间:2019-11-17 15:28:57

标签: python ssl

我正在尝试使用Python3获得网站的TLS证书颁发者。有许多适用于Python2的示例,但没有一个适用于Python3。例如,以下代码可在Python2上运行:

import ssl, socket

hostname = 'google.com'
ctx = ssl.create_default_context()
s = ctx.wrap_socket(socket.socket(), server_hostname=hostname)
s.connect((hostname, 443))
cert = s.getpeercert()

subject = dict(x[0] for x in cert['subject'])
issued_to = subject['commonName']
issuer = dict(x[0] for x in cert['issuer'])
issued_by = issuer['commonName']

但是,当我使用Python3运行它时,出现此错误:

  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/ssl.py", line 689, in do_handshake
    self._sslobj.do_handshake()
ssl.SSLError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:833)

如何在Python3中实现相同的目的?

0 个答案:

没有答案