Skip to content

Instantly share code, notes, and snippets.

@ihadgraft
Last active June 25, 2018 13:27
Show Gist options
  • Save ihadgraft/3efad3ff806759979586eb2f762c537c to your computer and use it in GitHub Desktop.
Save ihadgraft/3efad3ff806759979586eb2f762c537c to your computer and use it in GitHub Desktop.
DNS queries with Python

DNS queries with Python

These examples use DNSPython

pip install dnspython
import dns.resolver
try:
answer = dns.resolver.query(domain, 'A')
for record in answer:
cname = answer.canonical_name.to_text().strip('.')
yield {
'name': domain,
'cname': cname,
'address': record.address,
'rectype': 'a' if domain == cname else 'cname'
}
except dns.resolver.NXDOMAIN as e:
# Custom handling
raise e
except dns.resolver.NoAnswer as e:
# Custom handling
raise e
import dns.resolver
resolver = dns.resolver.Resolver()
resolver.nameservers = ['127.0.0.1']
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment