Monday, 3 September 2012

Get IP of the Current Host

If a host is connected to the internet, the following script running on the host, can return its IP.

Source Code


import socket

s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect(("gmail.com",80))
print(s.getsockname()[0])
s.close()

Reference


http://stackoverflow.com/questions/166506/finding-local-ip-addresses-using-pythons-stdlib

No comments:

Post a Comment