Basic gethostbyaddr() example : IP Address « Network « Python Tutorial






import sys, socket

def getipaddrs(hostname):
    result = socket.getaddrinfo(hostname, None, 0, socket.SOCK_STREAM)
    return [x[4][0] for x in result]

hostname = socket.gethostname()
print "Host name:", hostname

print socket.getfqdn(hostname)
try:
    print ", ".join(getipaddrs(hostname))
except socket.gaierror, e:
    print "Couldn't not get IP addresses:", e








21.8.IP Address
21.8.1.Basic gethostbyaddr() example
21.8.2.Basic getaddrinfo() basic example
21.8.3.Basic getaddrinfo() list example
21.8.4.Perform a reverse lookup on the IP address given on the command line
21.8.5.getaddrinfo() display