from cryptography.x509 import load_der_x509_certificate
from urllib3.connection import HTTPSConnection
https_conn = HTTPSConnection("example.com", server_hostname="example.com")
https_conn.connect()
ssl_sock = https_conn.sock
cert1 = ssl_sock.getpeercert(binary_form=False)
# cert1 is a dict with many useful fields. Might be all you need
print(cert1)
cert2 = ssl_sock.getpeercert(binary_form=True)
# cert2 is the full DER encoded certificate, which you can supply to
# other libraries to do more advanced stuff
x509_cert = load_der_x509_certificate(cert2)
print(x509_cert.serial_number)
我似乎找不到直接的方法。 urllib3 似乎
ssl.SSLSocket
在底层使用了标准 python。 这有几种可能有用的方法,请参阅链接中的详细信息,但似乎提供您想要的方法的是getpeercert()
。这里有一个小例子来说明:
请注意,这只是建立了连接,尚未完成任何有用的事情。