Connecting Tech Pros Worldwide Help | Site Map

Showing IP address of a user...

Fazer
Guest
 
Posts: n/a
#1: Jul 18 '05
Hello,

I was wondering how I can show an IP address of a person who visits a
Python web-page? Would I have to use Environment variables to access
Apache's server variables which hold such values like in PHP or what?
Marnanel
Guest
 
Posts: n/a
#2: Jul 18 '05

re: Showing IP address of a user...


Fazer wrote:[color=blue]
> I was wondering how I can show an IP address of a person who visits a
> Python web-page? Would I have to use Environment variables to access
> Apache's server variables which hold such values like in PHP or what?[/color]

This is defined by the Common Gateway Interface, so it should be the
same across all HTTP servers. It's certainly going to work the same way
across all languages on the same HTTP server.

os.environ is your friend:

import os
print 'Content-Type: text/plain'
print
print os.environ['REMOTE_ADDR']

(<URL:http://hoohoo.ncsa.uiuc.edu/cgi/> has the specification of all the
environment variables, in case you need more.)

M

Irmen de Jong
Guest
 
Posts: n/a
#3: Jul 18 '05

re: Showing IP address of a user...


Jeremy Yallop wrote:
[color=blue]
> os.environ['REMOTE_ADDR'][/color]

Ofcourse, this is only available when you're in a CGI-like
environment. When running inside mod_python, or something
else, I doubt that this environment variable is available.
In those cases, there is usually a specific way of obtaining
the client's address, either directly or via the socket
that represents the network connection. But this depends
on what you're running!


--Irmen

Gerhard Häring
Guest
 
Posts: n/a
#4: Jul 18 '05

re: Showing IP address of a user...


Fazer wrote:[color=blue]
> Hello,
>
> I was wondering how I can show an IP address of a person who visits a
> Python web-page? Would I have to use Environment variables to access
> Apache's server variables which hold such values like in PHP or what?[/color]

Use this little CGI script to find the answer to your question:

#!/usr/bin/env python
import cgi
cgi.test()

-- Gerhard

Fazer
Guest
 
Posts: n/a
#5: Jul 18 '05

re: Showing IP address of a user...


Thank you Marnanel and everyone who replied. This gives me a better understanding.
Jim Dabell
Guest
 
Posts: n/a
#6: Jul 18 '05

re: Showing IP address of a user...


Fazer wrote:
[color=blue]
> I was wondering how I can show an IP address of a person who visits a
> Python web-page? Would I have to use Environment variables to access
> Apache's server variables which hold such values like in PHP or what?[/color]

I see you've already been given answers to this, however bear in mind that
what you are determining is the IP address of the client, not the IP
address of the person visiting the page. In many cases, the two are not
the same, such as when the visitor is using a proxy.

There's no reliable way of getting the IP address of the person, but you can
make things a little more reliable by examining the X_FORWARDED_FOR header
as well, since many proxies add this header to their requests (also bear in
mind that they may be private addresses, such as 10.0.0.1).

--
Jim Dabell

Fazer
Guest
 
Posts: n/a
#7: Jul 18 '05

re: Showing IP address of a user...


Jim Dabell <jim-usenet@jimdabell.com> wrote in message news:<pg2dnSgwIbO4O7qiRTvUqg@giganews.com>...[color=blue]
> Fazer wrote:
>[color=green]
> > I was wondering how I can show an IP address of a person who visits a
> > Python web-page? Would I have to use Environment variables to access
> > Apache's server variables which hold such values like in PHP or what?[/color]
>
> I see you've already been given answers to this, however bear in mind that
> what you are determining is the IP address of the client, not the IP
> address of the person visiting the page. In many cases, the two are not
> the same, such as when the visitor is using a proxy.
>
> There's no reliable way of getting the IP address of the person, but you can
> make things a little more reliable by examining the X_FORWARDED_FOR header
> as well, since many proxies add this header to their requests (also bear in
> mind that they may be private addresses, such as 10.0.0.1).[/color]

Oh, thanks for the reference Jim! I will keep that in mind.
Closed Thread