Friday, May 23, 2008

NSLOOKUP in Python

from socket import gethostbyaddr 

def nslooky(ip):
      try: 
           output = gethostbyaddr(ip)
           return output[0]
     except: 
           output = "not found" 
           return output

 your_ip = request.META.get('REMOTE_ADDR') 
# above is Django module object
 your_name = nslooky(your_ip)
Original code by Cluther

1 comment:

Anonymous said...

For a common nslookup, you may have a better result from the following:

return ''.join(output[2])