[Tutor] Getting my IP

Sheila King sheila@thinkspot.net
Fri, 29 Jun 2001 20:54:26 -0700


On Sat, 30 Jun 2001 13:38:16 +1000 (EST), pd <pd@localhost.localdomain>
wrote about [Tutor] Getting my IP:

:Hi,
:   Im writing a script that posts IP number to a page on my website. This
:script is nearly complete except I cannot figure out how to actually get 
:my IP address. I have tried using the socket module, but it  appears that
:this justs takes a look in my /etc/hosts file. What I need is my dynamic
:IP that gets assigned every time I connect to the net.

What you want to use, are the environment variables from your CGI
script.

Here is a CGI script that will post all the environment variables, plus
some other stuff:

---------------------------------------------------------
#!/usr/bin/env python

import os

print "Content-type: text/html\n\n"

print "<html>\n"

print "<Head>"
print "<Title>Obligatory Python Test Script</Title>"
print "</Head>\n"

print "<Body>\n"
print "<H3>Hello</H3>"
print "<b>This is the obligatory<br> Python 'Hello\' test
script<br></b>"
print "<i>Ta-da</i><br><br>"

print "NEW STUFF: testing environment variables<br>"
print os.environ["REMOTE_ADDR"],"<br><br>"
print "<p>And now, all the environment variables:<br>"
print "<b>",
for el in os.environ.keys():
	print el, " = ", os.environ[el], "<br>"
print "</b>"
print "THE END"

print "</body>\n</html>"
---------------------------------------------------------

It should display your IP number. You can see this script run here:
http://www.thinkspot.net/cgi-bin/pytestscriptenviron.py
Notice that the environment variable you need is REMOTE_ADDR.

--
Sheila King
http://www.thinkspot.net/sheila/
http://www.k12groups.org/

--
Sheila King
http://www.thinkspot.net/sheila/
http://www.k12groups.org/