Get Cliet IP Address

Piet van Oostrum piet at cs.uu.nl
Sun Aug 2 20:26:55 CEST 2009


>>>>> Fred Atkinson <fatkinson at mishmash.com> (FA) wrote:

>FA> 	What is the function to obtain the client browser's IP
>FA> address?   

You mean in a web server?

The following should work (and was posted by me not long ago):

from os import getenv

ip = (getenv("HTTP_CLIENT_IP") or
      getenv("HTTP_X_FORWARDED_FOR") or
      getenv("HTTP_X_FORWARDED_FOR") or
      getenv("REMOTE_ADDR") or
      "UNKNOWN")

I use getenv() rather than environ[] to avoid exceptions.
-- 
Piet van Oostrum <piet at cs.uu.nl>
URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4]
Private email: piet at vanoostrum.org



More information about the Python-list mailing list