Python Code Snippet to Get External IP Address
UPDATE, 2011-04-17: New URL for query.
UPDATE, 2010-05-09: Corrected to return external IP even when on subnet.
The following function returns the current external ip address:
import urllib2 def get_external_ip(): ip = urllib2.urlopen("http://whatismyip.org").read() return ip
feed
Comments
3 comments postedwhat's my ip changed the location of this page, the correct snippet is
[code]
import urllib2
def get_external_ip():
ip = urllib2.urlopen("http://automation.whatismyip.com/n09230945.asp").read()
return ip
[/code]
Thanks. As it turns out, the URL can be even simpler, and I have amended the code above to show this.
Post new comment