[Tutor] Re: How to find out DNS ?

Matthew Sherborne miracle@paradise.net.nz
Fri, 28 Jun 2002 08:50:16 +1200


A wrote:

>Hi,
>Is there a way how to find out, from Python , what primary or 
>secondary DNS I use when connecting to internet?
>Thanks for help
>Ladislav
>
>_______________________________________________
>ActivePython mailing list
>ActivePython@listserv.ActiveState.com
>To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
>Other options: http://listserv.ActiveState.com/mailman/listinfo/ActivePython
>
>
>
>  
>
    def getServersFromOS(self):
        if sys.platform in ('win32', 'nt'): return 
self.getServersFromWin32()
        elif sys.platform == 'posix': return self.getServersFromPosix()
        else: return []
       
    def getServersFromWin32(self):
        import _winreg
        servers = []
        x = _winreg.ConnectRegistry(None,_winreg.HKEY_LOCAL_MACHINE)
        try:
            y = _winreg.OpenKey(x, 
r'SYSTEM\CurrentControlSet\Services\Tcpip\Parameters')
        except EnvironmentError: # so it isn't NT/2000/XP
            # windows ME, perhaps?
            try: # for Windows ME
                y = _winreg.OpenKey(x, 
r'SYSTEM\CurrentControlSet\Services\VxD\MSTCP')
            nameserver = _winreg.QueryValueEx(y, 'NameServer')[0]
            if nameserver and not (nameserver in servers):
                servers += nameserver.split(',')
        except EnvironmentError: pass
        return servers

    def getServersFromPosix(self):
        """"Parses the /etc/resolv.conf file and sets defaults for name 
servers""""
        lines = open('/etc/resolv.conf').readlines()
        servers = []
        for line in lines:
            line = line.strip()
            if not line or line[0] == ';' or line[0] == '#': continue
            fields=string.split(line)
            if fields[0] == 'domain': defaults['domain'] = fields[1]
            elif fields[0] == 'search': pass
            elif fields[0] == 'options': pass
            elif fields[0] == 'sortlist': pass
            elif fields[0] == 'nameserver': servers.append(fields[1])
        return servers

GBU
Matthew Sherborne