1# $Id$ 2# 3# This file is part of the pydns project. 4# Homepage: http://pydns.sourceforge.net 5# 6# This code is covered by the standard Python License. See LICENSE for details. 7# 8 9# routines for lazy people. 10from . import Base 11from . Base import ServerError 12 13class NoDataError(IndexError): pass 14class StatusError(IndexError): pass 15 16def revlookup(name,timeout=30): 17 "convenience routine for doing a reverse lookup of an address" 18 if Base.defaults['server'] == []: Base.DiscoverNameServers() 19 names = revlookupall(name, timeout) 20 if not names: return None 21 return names[0] # return shortest name 22 23def revlookupall(name,timeout=30): 24 "convenience routine for doing a reverse lookup of an address" 25 # FIXME: check for IPv6 26 a = name.split('.') 27 a.reverse() 28 b = '.'.join(a)+'.in-addr.arpa' 29 qtype='ptr' 30 names = dnslookup(b, qtype, timeout) 31 # this will return all records. 32 names.sort(key=str.__len__) 33 return names 34 35def dnslookup(name,qtype,timeout=30): 36 "convenience routine to return just answer data for any query type" 37 if Base.defaults['server'] == []: Base.DiscoverNameServers() 38 result = Base.DnsRequest(name=name, qtype=qtype).req(timeout=timeout) 39 if result.header['status'] != 'NOERROR': 40 raise ServerError("DNS query status: %s" % result.header['status'], 41 result.header['rcode']) 42 elif len(result.answers) == 0 and Base.defaults['server_rotate']: 43 # check with next DNS server 44 result = Base.DnsRequest(name=name, qtype=qtype).req(timeout=timeout) 45 if result.header['status'] != 'NOERROR': 46 raise ServerError("DNS query status: %s" % result.header['status'], 47 result.header['rcode']) 48 return [x['data'] for x in result.answers] 49 50def mxlookup(name,timeout=30): 51 """ 52 convenience routine for doing an MX lookup of a name. returns a 53 sorted list of (preference, mail exchanger) records 54 """ 55 qtype = 'mx' 56 l = dnslookup(name, qtype, timeout) 57 return l 58 59# 60# $Log$ 61# Revision 1.5.2.1.2.2 2011/03/23 01:42:07 customdesigned 62# Changes from 2.3 branch 63# 64# Revision 1.5.2.1.2.1 2011/02/18 19:35:22 customdesigned 65# Python3 updates from Scott Kitterman 66# 67# Revision 1.5.2.1 2007/05/22 20:23:38 customdesigned 68# Lazy call to DiscoverNameServers 69# 70# Revision 1.5 2002/05/06 06:14:38 anthonybaxter 71# reformat, move import to top of file. 72# 73# Revision 1.4 2002/03/19 12:41:33 anthonybaxter 74# tabnannied and reindented everything. 4 space indent, no tabs. 75# yay. 76# 77# Revision 1.3 2001/08/09 09:08:55 anthonybaxter 78# added identifying header to top of each file 79# 80# Revision 1.2 2001/07/19 06:57:07 anthony 81# cvs keywords added 82# 83# 84