-from twisted.names import client
+from twisted.names import client, dns
from twisted.internet import reactor, defer
class LookupException(Exception): pass
def lookupPTR(ip, *args, **kwargs):
def callback(result):
answer, auth, add = result
-
+ answer = [x for x in answer if x.type == dns.PTR]
if len(answer) == 0:
raise LookupException, "No ANSWERS in PTR response for %s." % repr(ip)
return str(answer[0].payload.name)
return client.lookupPointer(ptr, **kwargs).addCallback(callback)
def expandIPv6(ip):
- expand_start, expand_end = ["".join(["{:0>4}".format(group)
+ expand_sections = ["".join(["{:0>4}".format(group)
for group in section.split(":")])
- for section in ip.split("::")]
- return expand_start + (32-len(expand_start)-len(expand_end))*"0" + expand_end
+ for section in ip.split("::", 1)]
+ if len(expand_sections) == 1:
+ return expand_sections[0]
+ return expand_sections[0] + "".join((32-sum([len(x) for x in expand_sections]))*"0") + expand_sections[1]
def lookupPTRv6(ip, *args, **kwargs):
def callback(result):
answer, auth, add = result
-
+ answer = [x for x in answer if x.type == dns.PTR]
if len(answer) == 0:
raise LookupException, "No ANSWERS in PTR response for %s." % repr(ip)
return str(answer[0].payload.name)
def lookupAs(hostname, *args, **kwargs):
def callback(result):
answer, auth, add = result
+ answer = [x for x in answer if x.type == dns.A]
if len(answer) == 0:
raise LookupException, "No ANSWERS in A response for %s." % repr(hostname)
return [x.payload.dottedQuad() for x in answer]
def lookupAAAAs(hostname, *args, **kwargs):
def callback(result):
answer, auth, add = result
+ answer = [x for x in answer if x.type == dns.AAAA]
if len(answer) == 0:
raise LookupException, "No ANSWERS in AAAA response for %s." % repr(hostname)
return [expandIPv6(x.payload._address) for x in answer]