Reverse lookup

This command obtains the host name from the IP address. For this task, we can use the gethostbyaddr() function. In this script, we obtain the host name from the IP address of 8.8.8.8.

You can find the following code in the socket_reverse_lookup.py file:

import sys, sockettry :    result=socket.gethostbyaddr("8.8.8.8")    print "The host name is:"    print " "+result[0]    print "\nAddress:"    for item in result[2]:        print " "+itemexcept socket.herror,e:    print "error for resolving ip address:",e

Get Mastering Python for Networking and Security now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.