Home > Uncategorized > Get External IP address in Python

Get External IP address in Python

Getting your External IP address requires a remote server to reply with your client ip. I used a one-liner PHP script with simply <?php echo $_SERVER[‘REMOTE_ADDR’]; ?>  hosted at dananos.brinkster.net/ip.php

Then I used the following python script to request the IP from this URL, and then save the output to file.

import httplib
conn = httplib.HTTPConnection(“dananos.brinkster.net”)
conn.request(“GET”, “/ip.php”)
r1 = conn.getresponse()
data1 = r1.read()
f = open(‘tempfile.txt’, ‘w’)
f.write(data1)
f.close()

Bit of trial-and-error, but it works.

Advertisements
Categories: Uncategorized
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: