Determine ip address
rbt
rbt at athop1.ath.vt.edu
Fri Apr 15 09:37:25 EDT 2005
codecraig wrote:
> hi,
> how can i use python to figure the ip address of the machine which
> the python script is running on? I dont mean like 127.0.0.1....but i
> want the external IP address (such as ipconfig on windows displays).
>
> any ideas??
>
> THanks
>
To get the public IP (like when you're behind a wirless router, etc) you
may try doing something like this:
import urllib
import re
import time
ip_search = re.compile ('\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}')
try:
f = urllib.urlopen("http://www.ipchicken.com")
data = f.read()
f.close()
current_ip = ip_search.findall(data)
if current_ip:
print current_ip
time.sleep(3)
except Exception:
pass
HTH,
rbt
More information about the Python-list
mailing list