1 # $Id: lazy.py,v 1.5.2.4 2011/03/19 22:15:01 customdesigned Exp $
3 # This file is part of the pydns project.
4 # Homepage: http://pydns.sourceforge.net
6 # This code is covered by the standard Python License. See LICENSE for details.
9 # routines for lazy people.
13 from Base import DNSError
16 "convenience routine for doing a reverse lookup of an address"
17 names = revlookupall(name)
18 if not names: return None
19 return names[0] # return shortest name
21 def revlookupall(name):
22 "convenience routine for doing a reverse lookup of an address"
23 # FIXME: check for IPv6
24 a = string.split(name, '.')
26 b = string.join(a, '.')+'.in-addr.arpa'
27 names = dnslookup(b, qtype = 'ptr')
28 # this will return all records.
29 names.sort(key=str.__len__)
32 def dnslookup(name,qtype):
33 "convenience routine to return just answer data for any query type"
34 if Base.defaults['server'] == []: Base.DiscoverNameServers()
35 result = Base.DnsRequest(name=name, qtype=qtype).req()
36 if result.header['status'] != 'NOERROR':
37 raise DNSError("DNS query status: %s" % result.header['status'])
38 elif len(result.answers) == 0 and Base.defaults['server_rotate']:
39 # check with next DNS server
40 result = Base.DnsRequest(name=name, qtype=qtype).req()
41 if result.header['status'] != 'NOERROR':
42 raise DNSError("DNS query status: %s" % result.header['status'])
43 return map(lambda x: x['data'],result.answers)
47 convenience routine for doing an MX lookup of a name. returns a
48 sorted list of (preference, mail exchanger) records
50 l = dnslookup(name, qtype = 'mx')
56 # Revision 1.5.2.4 2011/03/19 22:15:01 customdesigned
57 # Added rotation of name servers - SF Patch ID: 2795929
59 # Revision 1.5.2.3 2011/03/16 20:06:24 customdesigned
60 # Expand convenience methods.
62 # Revision 1.5.2.2 2011/03/08 21:06:42 customdesigned
63 # Address sourceforge patch requests 2981978, 2795932 to add revlookupall
64 # and raise DNSError instead of IndexError on server fail.
66 # Revision 1.5.2.1 2007/05/22 20:23:38 customdesigned
67 # Lazy call to DiscoverNameServers
69 # Revision 1.5 2002/05/06 06:14:38 anthonybaxter
70 # reformat, move import to top of file.
72 # Revision 1.4 2002/03/19 12:41:33 anthonybaxter
73 # tabnannied and reindented everything. 4 space indent, no tabs.
76 # Revision 1.3 2001/08/09 09:08:55 anthonybaxter
77 # added identifying header to top of each file
79 # Revision 1.2 2001/07/19 06:57:07 anthony