How to find out DNS ?

Derrick 'dman' Hudson dman at dman.ddts.net
Thu Jun 27 13:55:20 EDT 2002


On Thu, Jun 27, 2002 at 07:03:34PM +0200, A wrote:

| Is there a way how to find out, from Python , what primary or 
| secondary DNS I use when connecting to internet?

f = open( "/etc/resolv.conf" , "r" )
nameservers = []
for line in f.xreadlines() :
    line = line.strip()
    if line.startswith( "nameserver" ) :
        _ , ip = line.split()
        nameservers.append( ip )
f.close()
for ns in nameserver :
    print "nameserver" , ns


Of course, this only works on a UNIX-like system where
/etc/resolv.conf is used to list the nameservers.  A common setup (on
unix systems) is for some entries to be in /etc/hosts which takes
precedence over DNS lookups.  Any given application can also do its
own thing too, if it wants.  For example, the 'host' command can be
told to contact any arbitrary host and submit a DNS query to it.  Some
setups will have LDAP (or maybe NIS) also serving host information, in
which case DNS isn't involved for those hosts.

Why do you want to know what the nameserver(s) are?  Usually it is
better to not know that and just let the underlying libresolv handle
name resolution for you.  (program to the interface, not the
implementation ...)

-D

-- 

Microsoft is to operating systems & security ....
                                     .... what McDonald's is to gourmet cooking
 
http://dman.ddts.net/~dman/

-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 248 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/python-list/attachments/20020627/464df0a4/attachment.sig>


More information about the Python-list mailing list