Accessing REMOTE_ADDR from web page

Michael P. Reilly arcege at shore.net
Wed May 19 10:24:38 EDT 1999


Rico Albanese <r.albanese at qut.edu.au> wrote:
: Hi everyone,

: Can someone tell me how to access the REMOTE_ADDR shell environment
: variable which is sent with a web page request to a web server.  I have
: a job logging application which uses a web page which calls a Python
: script to enter a request into a database.  I wish to collect also the
: ip address of the workstation that is making the requesting so that we
: may better manage a our ip addresses in our faculty.

: I have used the following code :

: import cgi
: .
: .
: .
: theenviron=cgi.print_environ()
: .
: .
: print theenviron

: This produces the following output:

: <DT> PATH <DD> C:\Python;C:\Perl\bin;C:\WINNT\system32;C:\WINNT;
: <DT> PYTHONPATH <DD> c:\python
: <DT> QUERY_STRING <DD>
: Name=&PhNumb=&Locn=&Schl=NS&urgency=D&JobDesc=&
: <DT> REMOTE_ADDR <DD> 131.181.38.141
: <DT> REMOTE_PORT <DD> 1288
: <DT> REQUEST_METHOD <DD> POST
: <DT> REQUEST_URI <DD> /cgi-bin/ra2jblog.py

: My question is how do I access the REMOTE_ADDR??  Is "theenviron" a
: string,list, tuple what??

You can access all the environment variables thru cgi.os.environ ( just
reference to os.environ), which often is a UserDict subclass.

  you_are_from_where = cgi.os.environ['REMOTE_ADDR']

The environ object is documented in the os module (http://www.python.org/
doc/current/lib/os-procinfo.html).  You might want to look at the
print_environ function and others in the cgi.py code for more
information.

  -Arcege





More information about the Python-list mailing list