Python Code Snippet to Get External IP Address

15 Oct 2009
Posted by Jeet Sukumaran
UPDATE, 2010-05-09: Corrected to return external IP even when on subnet.

The following function returns the current external ip address:

import urllib
 
def get_external_ip():
    ip = urllib.urlopen("http://whatismyip.org").read()
    return ip
Tags:

It must be wrong.

This function returns Internal IP, not the external one.

Post new comment

The content of this field is kept private and will not be shown publicly.
CAPTCHA
This question is for testing whether you are a biological visitor and to prevent automated spam submissions.
Image CAPTCHA
Enter the characters shown in the image.