Specify DNS server when resolving address

2

I would like to do something like an "nslookup" to resolve an address of a site through my DNS servers, because in the example below, I only get through DNS that I have in my machine.

How could I tell the IP of my server to resolve the Google address for example?

import socket

dns = socket.gethostbyname('www.google.com')
print(dns)
    
asked by anonymous 11.03.2017 / 20:05

1 answer

5

Natively there seems to be no way to do this.

Alternatively, you can use the dns.resolver module. of the dnspython tool set.

To install in Ubuntu / Debian, via apt-get , do:

sudo apt-get install python-dnspython

If you prefer to install via pip :

sudo pip install dnspython

To indicate the server, use the nameservers of the object Resolver .

import dns.resolver

resolver = dns.resolver.Resolver(configure=False)

# 8.8.8.8 é o DNS público do Google
resolver.nameservers = ['8.8.8.8']

answers = resolver.query('google.com')

for rdata in answers:
    print (rdata.address)

More information:

11.03.2017 / 23:08