pythonpython-3.xlistdnspython

Python does not import list in dns.resolver


I am writing a code which will resolve CNAME from the list of domains but my current script only resolve the first line of the list.txt and skips the rest.

I check "dl" import all the lines into list by running print(dl) but dns.resolver is not running check on each list data.

My sample Code is:

#/usr/bin/python3

import dns.resolver

d = open("list.txt" , "r")
dl = d.read().splitlines()

try:
    for dom in dl:
        answer = dns.resolver.resolve(dom, 'CNAME')
        for rdata in answer:
            print("CNAME", ':', rdata.to_text())
        
except Exception as e:
    print(e)

list.txt is:

www.google.com
www.github.com
www.facebook.com

Desired output is:

CNAME : www.gcaname.com
CNAME : www.gcaname.com
The DNS response does not contain an answer to the question: www.github.com. IN CNAME

Solution

  • This worked:

    for dom in dl:
            try:    
                answer = dns.resolver.resolve(dom, 'CNAME')
                for rdata in answer:
                    print("CNAME of %s" % dom, ':', rdata.to_text())
            except Exception as e:
                pass