Accessing REMOTE_ADDR from web page

Tom Bryan tbryan at zarlut.utexas.edu
Tue May 18 07:46:46 EDT 1999


Rico Albanese wrote:
 
> 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.  
[...detailed description and code cut...]
> My question is how do I access the REMOTE_ADDR??  Is "theenviron" a
> string,list, tuple what??

This is one of the places where the source is your friend!  I have 
found most of the Python library source code to be very clear.
On my machine, I have something like the following definition in 
cgi.py

def print_environ(environ=os.environ):
    """Dump the shell environment as HTML."""
    keys = environ.keys()
    keys.sort()
    print
    print "<H3>Shell Environment:</H3>"
    print "<DL>"
    for key in keys:
        print "<DT>", escape(key), "<DD>", escape(environ[key])
    print "</DL>" 
    print

Thus, you can see that the cgi script's "shell environment" is 
accessed just like the environment *always* is....through os.environ.
os.environ is a dictionary of all of the available environment 
variables.  Try printing os.environ['REMOTE_ADDR'].  Is that what you
were looking for?

-- 
tbryan at zarlut.utexas.edu
Remove the z from this address to reply.
Stop spam!  http://spam.abuse.net/spam/




More information about the Python-list mailing list