Showing IP address of a user...

Discussion in 'Python' started by Fazer, Jul 29, 2003.

  1. Fazer

    Fazer Guest

    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?
    Fazer, Jul 29, 2003
    #1
    1. Advertising

  2. Fazer

    Marnanel Guest

    Fazer wrote:
    > 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
    Marnanel, Jul 29, 2003
    #2
    1. Advertising

  3. Jeremy Yallop wrote:

    > 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
    Irmen de Jong, Jul 29, 2003
    #3
  4. Fazer wrote:
    > 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
    =?ISO-8859-1?Q?Gerhard_H=E4ring?=, Jul 29, 2003
    #4
  5. Fazer

    Fazer Guest

    Thank you Marnanel and everyone who replied. This gives me a better understanding.
    Fazer, Jul 30, 2003
    #5
  6. Fazer

    Jim Dabell Guest

    Fazer wrote:

    > 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
    Jim Dabell, Jul 30, 2003
    #6
  7. Fazer

    Fazer Guest

    Jim Dabell <> wrote in message news:<>...
    > Fazer wrote:
    >
    > > 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.
    Fazer, Aug 1, 2003
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. avnrao
    Replies:
    1
    Views:
    7,217
    avnrao
    May 4, 2004
  2. Patrice
    Replies:
    0
    Views:
    880
    Patrice
    May 4, 2004
  3. Raterus
    Replies:
    0
    Views:
    432
    Raterus
    Jun 2, 2004
  4. Alex Stevens
    Replies:
    0
    Views:
    775
    Alex Stevens
    Aug 10, 2004
  5. Ray Booysen
    Replies:
    2
    Views:
    642
    Ray Booysen
    Mar 28, 2006
Loading...

Share This Page