python 2.7 - Query an array of DNS records with dnspython, and proceed to the next element based on a custom timeout -


i'm querying bunch of dns records dnspython, seems timeout when no reply received (dns.exception.timeout: dns operation timed out after 30.0006685256958 seconds).

here's function:

def dnstest(domain):     dnsrecords = []     record_types = ['a', 'aaaa', 'cname', 'mx', 'srv', 'txt', 'soa', 'ns']     dnsresolver = dns.resolver.resolver()     record in record_types:         dnsanswer = dnsresolver.query(domain, record)         rdata in dnsanswer:             dnsrecords.append(rdata)     return(dnsrecords)  print(dnstest("github.com")) 

whereas, if query dns records exist, answer received:
dnsanswer = dnsresolver.query(domain, 'a')

i'm guessing because records (such srv, aaaa etc.) doesn't exist github.com. however, exist other domains. there way can include timeout (like say, 5 seconds) per query within for loop?
-
if no response in 5 seconds, move next element in array
or there better approach exclude non-existent records?

the following worked me after quick googling.

import dns.resolver  def dnstest(domain):     dnsrecords = []     record_types = ['a', 'aaaa', 'cname', 'mx', 'srv', 'txt', 'soa', 'ns']     dnsresolver = dns.resolver.resolver()     dnsresolver.timeout = 1     dnsresolver.lifetime = 1     record in record_types:         try:             dnsanswer = dnsresolver.query(domain, record)             rdata in dnsanswer:                 dnsrecords.append(rdata)         except:             pass      return(dnsrecords) 

Comments

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -