Showing IP address of a user... 
July 18th, 2005, 02:06 AM
| | | |
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? | 
July 18th, 2005, 02:07 AM
| | | | 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 | 
July 18th, 2005, 02:07 AM
| | | | 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 | 
July 18th, 2005, 02:07 AM
| | | | 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 | 
July 18th, 2005, 02:07 AM
| | | | re: Showing IP address of a user...
Thank you Marnanel and everyone who replied. This gives me a better understanding. | 
July 18th, 2005, 02:08 AM
| | | | 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 | 
July 18th, 2005, 02:12 AM
| | | | 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. |  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 225,689 network members.
|