Showing IP address of a user...

F

Fazer

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

Marnanel

Fazer said:
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
 
I

Irmen de Jong

Jeremy said:
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
 
?

=?ISO-8859-1?Q?Gerhard_H=E4ring?=

Fazer said:
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
 
J

Jim Dabell

Fazer said:
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).
 
F

Fazer

Jim Dabell said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top