Hostname, Internal and External IP address in Python

Let’s see the code:

import socket
print "Hostname:", socket.gethostname()
print "Internal IP:", socket.gethostbyname(socket.gethostname())
 
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect(("google.com", 80))
print "External IP:", s.getsockname()[0]
s.close()
Categories: Python Tags: , , , , ,
%d bloggers like this: