IP Address Function
Nobody
nobody at nowhere.com
Thu Jul 9 00:38:32 EDT 2009
On Wed, 08 Jul 2009 20:53:12 -0700, Fred Atkinson wrote:
>>ipaddr = (getenv("HTTP_CLIENT_IP") or
>> getenv("HTTP_X_FORWARDED_FOR") or
>> getenv("HTTP_X_FORWARDED_FOR") or
>> getenv("REMOTE_ADDR") or
>> "UNKNOWN")
>>
>>print ipaddr
>
> That did it.
>
> I wonder why they don't just have a function to return it instead of
> putting you through all of that?
There's no unambiguous definition of "client IP", so you have to choose
which definition you want.
REMOTE_ADDR is set to the actual IP address from which the connection
originated (from getpeername()). If the connection was made via a proxy,
REMOTE_ADDR will contain the IP address of the proxy.
The others are set from HTTP headers. Proxies often add Client-IP
or X-Forwarded-For headers to indicate the originating IP address. OTOH,
there's no way to know if the headers are accurate; nuisance users may
add these headers to confuse poorly-conceived banning mechanisms.
More information about the Python-list
mailing list