Get Cliet IP Address

Piet van Oostrum piet at
Sun Aug 2 20:26:55 CEST 2009

>>>>> Fred Atkinson <fatkinson at> (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

I use getenv() rather than environ[] to avoid exceptions.
Piet van Oostrum <piet at>
URL: [PGP 8DAE142BE17999C4]
Private email: piet at

More information about the Python-list mailing list