top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

DNS query against a specific server using python

+1 vote
5,105 views

socket.gethostbyname sends the DNS resolution query to the DNS server specified by the OS. Is there an easy way to send a query to a *different* server?

I see that twisted.names allows you to do this, but, having all of twisted as dependency to my project when all I need to do is a simple DNS query seems a bit extreme. I also found pydns, but that looks fairly outdated and unmaintained.

posted Sep 30, 2013 by Bob Wise

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

2 Answers

+1 vote
 
Best answer

there is a dns modul for Python (I don't know is it part of standard Python library or not), on most Linux distribution you can find it, eg. in Debian it's called python-dnspython.

It can handle different nameserver, than OS knows - here is a sample code:

import dns.resolver

r = dns.resolver.Resolver()
r.namerservers = ['127.0.0.1']
# or any other IP, in my case I'm using PDNS, which have two
# parts: a recursor and a resolver; recursor allows requests only
# on localhost

mxservers = r.query("python.org", 'MX').response
answer Sep 30, 2013 by Anderson
Indeed, this looks much nicer than both twisted or pydns. I think I'll go with that one. Thanks....
+1 vote

It isn't pure python, but you would be pretty much guaranteed a maintained solution if you use the name server lookup in your OS. Something like:

 import subprocess
 nsl_reslt = subprocess.Popen(['nslookup', '' ],stderr = subprocess.PIPE, stdout = subprocess.PIPE).communicate()[0]

Hope this helps,

answer Sep 30, 2013 by Jai Prakash
I had this option in mind, but opening a subprocess for something as small as this seemed a bit error-prone. If something on the system changes, nslookup replaced by dig or nslookup output changes for example your application will bug out.

Granted, the chance of this happening is slim, but using a fixed-version dependency in your setup script gives you a much safer solution IMO.

I know I may be splitting hairs. Any of the mentioned solutions are fine. But I am curious to see if something like this is not yet implemented in a more standard way. I was surprised to see that ``gethostbyname`` does not take an optional parameter for this task.
gethostbyname is a simple function with not much flexibility. (You can't, for instance, look up a TXT record.) For anything more complex, you want a proper DNS implementation.

There are a few Python DNS modules. It means adding another dependency, but perhaps not as large as twisted. And of course, you could always manually send UDP packets and listen for responses, but that seems a little unnecessary :)
Similar Questions
+3 votes

If a APN is SIPTO enabled, MME has to select the geographically closer GWs during GW selection and continue to do so during UE Mobility. My question is what are the parameters of DNS query and what is the actual syntax of that query ?

0 votes

Please give me some idea about how we can query the DNS
How you have divided the passive dns functioning into two parts as recursor and resolver.
Can you please give some idea about how they function, and when they are employed after the query is submitted to the pdns
How the data packet will be analyzed for ddos attack.

...