I'm querying a bunch of DNS records with dnspython
, it seems to timeout when no reply is received (dns.exception.Timeout: The DNS operation timed out after 30.0006685256958 seconds
).
Here's the function:
def dnstest(domain):
dnsrecords = []
record_types = ['A', 'AAAA', 'CNAME', 'MX', 'SRV', 'TXT', 'SOA', 'NS']
dnsResolver = dns.resolver.Resolver()
for record in record_types:
dnsAnswer = dnsResolver.query(domain, record)
for rdata in dnsAnswer:
dnsrecords.append(rdata)
return(dnsrecords)
print(dnstest("github.com"))
whereas, if I query the DNS records that exist, an answer is received:
dnsAnswer = dnsResolver.query(domain, 'A')
I'm guessing this is because certain records (such as SRV
, AAAA
etc.) doesn't exist for github.com
. However, they do exist for other domains. Is there a way I can include a timeout (like say, 5 seconds) per query within the for
loop?
Something like -
if no response in 5 seconds, move to next element in the array
Or is there a better approach to exclude non-existent records?