Showing IP address of a user... 
Author Message
 Showing IP address of a user...

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?



Sun, 15 Jan 2006 02:05:19 GMT  
 Showing IP address of a user...

Quote:

> 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?

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



Sun, 15 Jan 2006 04:54:55 GMT  
 Showing IP address of a user...

Quote:

> 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?

  os.environ['REMOTE_ADDR']

Jeremy.



Sun, 15 Jan 2006 04:56:08 GMT  
 Showing IP address of a user...

Quote:

>   os.environ['REMOTE_ADDR']

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



Sun, 15 Jan 2006 06:07:32 GMT  
 Showing IP address of a user...

Quote:

> 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?

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

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

-- Gerhard



Sun, 15 Jan 2006 06:43:59 GMT  
 Showing IP address of a user...
Thank you  Marnanel and everyone who replied.  This gives me a better understanding.


Sun, 15 Jan 2006 12:04:23 GMT  
 Showing IP address of a user...

Quote:

> 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?

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



Sun, 15 Jan 2006 19:01:48 GMT  
 Showing IP address of a user...

Quote:


> > 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?

> 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).

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


Wed, 18 Jan 2006 01:10:17 GMT  
 
 [ 8 post ] 

 Relevant Pages 

1. Anyway to get the server ip address not the browser ip address

2. how to get user's ip address

3. TCP/IP resource alias does not show up in MAX or Express

4. IP address

5. Expand IP address range

6. ip address from ifconfig

7. Extract IP addresses from text file

8. Matching Octet of IP address

9. IP Addresses and Port Numbers

10. Getting the IP Address within RB

11. Reach new site through IP address?

12. Internet connection IP address

 

 
Powered by phpBB® Forum Software