How do I get user IP address in django? How do I get user IP address in django? python python

How do I get user IP address in django?


def get_client_ip(request):    x_forwarded_for = request.META.get('HTTP_X_FORWARDED_FOR')    if x_forwarded_for:        ip = x_forwarded_for.split(',')[0]    else:        ip = request.META.get('REMOTE_ADDR')    return ip

Make sure you have reverse proxy (if any) configured correctly (e.g. mod_rpaf installed for Apache).

Note: the above uses the first item in X-Forwarded-For, but you might want to use the last item (e.g., in the case of Heroku: Get client's real IP address on Heroku)

And then just pass the request as argument to it;

get_client_ip(request)


You can use django-ipware which supports Python 2 & 3 and handles IPv4 & IPv6.

Install:

pip install django-ipware

Simple Usage:

# In a view or a middleware where the `request` object is availablefrom ipware import get_client_ipip, is_routable = get_client_ip(request)if ip is None:    # Unable to get the client's IP addresselse:    # We got the client's IP address    if is_routable:        # The client's IP address is publicly routable on the Internet    else:        # The client's IP address is private# Order of precedence is (Public, Private, Loopback, None)

Advanced Usage:

  • Custom Header - Custom request header for ipware to look at:

    i, r = get_client_ip(request, request_header_order=['X_FORWARDED_FOR'])i, r = get_client_ip(request, request_header_order=['X_FORWARDED_FOR', 'REMOTE_ADDR'])
  • Proxy Count - Django server is behind a fixed number of proxies:

    i, r = get_client_ip(request, proxy_count=1)
  • Trusted Proxies - Django server is behind one or more known & trusted proxies:

    i, r = get_client_ip(request, proxy_trusted_ips=('177.2.2.2'))# For multiple proxies, simply add them to the listi, r = get_client_ip(request, proxy_trusted_ips=('177.2.2.2', '177.3.3.3'))# For proxies with fixed sub-domain and dynamic IP addresses, use partial patterni, r = get_client_ip(request, proxy_trusted_ips=('177.2.', '177.3.'))

Note: read this notice.


Alexander's answer is great, but lacks the handling of proxies that sometimes return multiple IP's in the HTTP_X_FORWARDED_FOR header.

The real IP is usually at the end of the list, as explained here: http://en.wikipedia.org/wiki/X-Forwarded-For

The solution is a simple modification of Alexander's code:

def get_client_ip(request):    x_forwarded_for = request.META.get('HTTP_X_FORWARDED_FOR')    if x_forwarded_for:        ip = x_forwarded_for.split(',')[-1].strip()    else:        ip = request.META.get('REMOTE_ADDR')    return ip