I’m currently writing a basic dispatch model server based on the Python Eventlet library (http://eventlet.net/doc/). Having looked at the WSGI docs on Eventlet (http://eventlet.net/doc/modules/wsgi.html), I can see that the eventlet.wsgi.server function logs the x-forwarded-for header in addition to the client IP address.
However, the way to obtain this is to attach a file-like object (the default which is sys.stderr) and then have the server pipe that to that object.
I would like to be able to obtain the client IP from within the application itself (i.e. the function that has start_response and environ as parameters). Indeed, an environ key would be perfect for this. Is there a way to obtain the IP address simply (i.e. through the environ dictionary or similar), without having to resort to redirecting the log object somehow?
What you want is in the wsgi environ, specifically
environ['REMOTE_ADDR'].However, if there is a proxy involved, then
REMOTE_ADDRwill be the address of the proxy, and the client address will be included (most likely) inHTTP_X_FORWARDED_FOR.Here’s a function that should do what you want, for most cases (all credit to Sævar):
You can easily see what is included in the wsgi environ by writing a simple wsgi app and pointing a browser at it, for example:
And combining the two …