[Python] How can I retrieve the TLS/SSL peer certificate of a remote host using python?

Discussão em 'Python' iniciado por Stack, Março 28, 2025.

  1. Stack

    Stack Membro Participativo

    I need to scan through a list of IPs and retrieve the common name from the certificate on that IP (for every IP that allows port 443 connections). I have been able to successfully do this using the sockets and ssl modules. It works for all IPs with valid, signed certificates but it isn't working for self-signed certificates.

    If I use this method, it requires a valid cert that is verified by my CA-bundle:

    from socket import socket
    import ssl

    s = socket()
    c = ssl.wrap_socket(s,cert_reqs=ssl.CERT_REQUIRED, ca_certs='ca-bundle.crt')
    c.connect(('127.0.0.1', 443))

    print c.getpeercert()


    If I remove the cert_reqs=ssl.CERT_REQUIRED then it connects but doesn't get the certificate at all.

    How can I retrieve the common name for a certificate on an IP whether it validates against the ca-bundle or not?

    Continue reading...

Compartilhe esta Página