Maya
Maya

Reputation: 47

What is the best way of sending many tls requests at once in Python?

I'm trying to figure out which domains are blocked by a firewall. Let's say sites contains around 2 million domains.

Is ThreadPoolExecutor the fastest way for doing this or is there any faster method?

Thanks

import socket
import ssl
from concurrent.futures import ThreadPoolExecutor

context = ssl.create_default_context()
socket.setdefaulttimeout(1)

sites = ['abc.com','def.com','geh.com']
def tls_check(domain):
    try:
        conn = ssl.create_connection(('1.1.1.1', 443))
    except socket.timeout:
        print(domain + "timed out")
        return
    except:
        return
    try:
        context.wrap_socket(conn, server_hostname=domain)
        conn.close()
    except ConnectionResetError:
        cx = open("blockedsites.txt", "a")
        cx.write("\n" + domain)
        cx.close()
        conn.close()
        return
    except:
        return
with ThreadPoolExecutor(max_workers=20) as pool:
    pool.map(tls_check, sites)

Upvotes: 0

Views: 161

Answers (0)

Related Questions