is it possible to parallel resolve lists from two dns servers?

I’m not very experienced with networking but I would guess most of the execution time of your script comes from communication with the DNS server, which means that your CPU is mostly just waiting for data, which means that you should be able to optimize the task by the use of multiple threads.

It is the easiest to use a ThreadPool:

from multiprocessing.pool import ThreadPool
import socket

import dns.resolver

my_list = [

resolver = dns.resolver.Resolver()

w = open("resolved.txt", "w")
x = open("not_resolved.txt", "w")

def resolve(domain):
        q = resolver.query(domain, "A")
        for ipval in q:
            print(domain, ipval, file=w)
    except dns.resolver.NXDOMAIN:
        print(domain, "NXDOMAIN", file=x)
    except dns.resolver.NoNameservers:
        print(domain, "NoNameservers", file=x)
    except dns.resolver.NoAnswer:
        print(domain, "NoAnswer", file=x)
        print(domain, "BadEscape", file=x)

pool = ThreadPool(processes=10)  # increasing this number may speed things up
results =, my_list)



$ cat not_resolved.txt
doesnt.exist NXDOMAIN
$ cat resolved.txt

The above code doesn’t attempt to distribute the list of domains among the available DNS servers, unless the dnspython package does it under the hood. But I would expect that even a single DNS server will respond really quickly to concurrent queries, because it probably uses multiple threads itself.

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top