You are trying to resolve "40 someurl.com.". please get rid of the number at the beggining and of the dot at the very end. You are also not looking up for any preference, you are justing picking the first entry. And lastly, you are checking for None when should be not None.
The correct would be:
import dns.resolver
to_domain = 'gmail.com'
records = dns.resolver.query(to_domain, 'MX')
lowestnumber = None
bestdomain = None
for r in records:
t = r.to_text()
i = t.find(' ')
n = int(t[:i])
if lowestnumber is None or n < lowestnumber:
lowestnumber = n
bestdomain = t[n+1:-1] # here we skip the initial number and the final dot
if bestdomain is not None:
records = dns.resolver.query(bestdomain, 'A')
for r in records:
print(r.to_text())
else:
raise ValueError('Bad Record')
At least for me, it is dns.resolver.query
instead of dns.resolver.resolve
. Please make your adaptations to make it to work for you.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…