0

I want to download the TLS certificate chain for a given website. I have a running code using blocking sockets, code provided here, Getting certificate chain with Python 3.3 SSL module.

from OpenSSL import SSL
import socket

def get_certificates(hostname, port):
    context = SSL.Context(method=SSL.TLSv1_METHOD)
    conn = SSL.Connection(context, socket=socket.socket(family=socket.AF_INET, type=socket.SOCK_STREAM))
    conn.settimeout(1)
    conn.connect((hostname, port))
    conn.setblocking(1)
    conn.do_handshake()
    conn.set_tlsext_host_name(hostname.encode())
    chain = conn.get_peer_cert_chain()
    conn.close()
    return chain

def main():
    hostname = 'www.google.com'
    port = 443
    chain = get_certificates(hostname, port)

This code is running fine. I want to use async to make multiprocessing with a large list of hostnames more performant. I didn't find a clear way to do it. What's the best way?

  • There is no way to make a single HTTPS connection more "performant" using concurrency features. Now if you are doing different or multiple IO/networking tasks at the same time then that's a different story. – President James K. Polk Sep 28 '21 at 15:44
  • There are plenty of python async/await tutorials on the web, those should be your first stop. – President James K. Polk Sep 28 '21 at 15:47
  • @PresidentJamesK.Polk thaks, of course I understand that, the idea is to improve performance when making lots of connections. I am aware of async/await usage. I'm not sure how to implement that with sockets and/or ssl context manager – Santiago Rodriguez Sep 28 '21 at 16:41
  • I cannot recommend it yet but I'm intrigued by a async python library called [Trio](https://trio.readthedocs.io/en/stable/). I may do some experimentation with it. It has an [SSLStream class](https://trio.readthedocs.io/en/stable/reference-io.html#trio.SSLStream). The docs include a [tutorial](https://trio.readthedocs.io/en/stable/tutorial.html) as well. – President James K. Polk Sep 28 '21 at 18:23

0 Answers0