采取
requests.get("https://151.101.1.69") # stackoverflow's ip
例如:
try:
requests.get("https://151.101.1.69")
except requests.exceptions.SSLError as e:
cert = e.args[0].reason.args[0]._peer_cert
那么
cert
是包含对等方证书的dict。因为我不太熟悉
SSL
顺便说一句,在这种情况下,错误是
"hostname '151.101.1.69' doesn't match either of '*.stackexchange.com', ...omitted
. 我不确定在你的真实案例中异常的结构,所以你可能需要自己找到它。我想应该同名
_peer_cert
.
当握手失败时,上述方法不起作用。。。但仍然可以做到:
try:
requests.get("https://fpslinux1.finalphasesystems.com/")
except requests.exceptions.SSLError:
import ssl
import OpenSSL
cert = ssl.get_server_certificate(('fpslinux1.finalphasesystems.com', 443))
cert = OpenSSL.crypto.load_certificate(OpenSSL.crypto.FILETYPE_PEM, cert)
print(cert.get_issuer())
print(cert.get_subject().get_components())
是的,它有点脏,但是我没有更好的方法,因为ssl套接字没有
甚至从C级返回无效证书:/
使用
OpenSSL
,您需要安装
pyopenssl
.