Personal tools
You are here: Home Documentation How To's How to obtain the referrer IP address from a Plone request

How to obtain the referrer IP address from a Plone request

by T. Kim Nguyen last modified Mar 14, 2011 10:52 AM

This is taken from http://plone.org/documentation/manual/plone-community-developer-documentation/serving/http-request-and-response#request-client-ip

 

def get_ip(request):
    """  Extract the client IP address from the HTTP request in proxy compatible way.

    @return: IP address as a string or None if not available
    """
    if "HTTP_X_FORWARDED_FOR" in request.environ:
        # Virtual host
        ip = request.environ["HTTP_X_FORWARDED_FOR"]
    elif "HTTP_HOST" in request.environ:
        # Non-virtualhost
        ip = request.environ["REMOTE_ADDR"]
    else:
        # Unit test code?
        ip = None

    return ip

 

Document Actions
  • Print this
  • Bookmarks