Package: django-ipware
Django IPwareA Django application to retrieve client's IP addressAlternative packageIf you prefer a python only version that does not integrate with Django directly, but allows for more flexibility and advanced features, you can use thepython-ipwarepackage instead.django-ipwareis a wrapper usingpython-ipwareunder the hood staring from version6.0.0.OverviewBest attemptto get client's IP address while keeping itDRY.NoticeThere is no perfectout-of-the-boxsolution against fake IP addresses, akaIP Address Spoofing.
You are encouraged to read the (Advanced users) section of this page and
usetrusted_proxies_ipsand/orproxy_countfeatures to match your needs, especiallyifyou are
planning to includeipwarein any authentication, security oranti-fraudrelated architecture.This is an open source project, with the source code visible to all. Therefore, it may be exploited through unimplemented, or improperly implemented features.Please use ipwareONLYas a complement to yourfirewallsecurity measures!How to install1. easy_install django-ipware
2. pip install django-ipware
3. git clone http://github.com/un33k/django-ipware
    a. cd django-ipware
    b. run python setup.py install
4. wget https://github.com/un33k/django-ipware/zipball/master
    a. unzip the downloaded file
    b. cd into django-ipware-* directory
    c. run python setup.py installHow to use# In a view or a middleware where the `request` object is availablefromipwareimportget_client_ipclient_ip,is_routable=get_client_ip(request)ifclient_ipisNone:# Unable to get the client's IP addresselse:# We got the client's IP addressifis_routable:# The client's IP address is publicly routable on the Internetelse:# The client's IP address is privateAdvanced users:Precedence OrderThe default meta precedence order is top to bottom. You may customize the order
by providing your ownIPWARE_META_PRECEDENCE_ORDERby adding it to your project's settings.py# The default meta precedence order (update as needed)IPWARE_META_PRECEDENCE_ORDER=("X_FORWARDED_FOR",# AWS ELB (default client is `left-most` [`<client>, <proxy1>, <proxy2>`])"HTTP_X_FORWARDED_FOR",# Similar to X_FORWARDED_TO"HTTP_CLIENT_IP",# Standard headers used by providers such as Amazon EC2, Heroku etc."HTTP_X_REAL_IP",# Standard headers used by providers such as Amazon EC2, Heroku etc."HTTP_X_FORWARDED",# Squid and others"HTTP_X_CLUSTER_CLIENT_IP",# Rackspace LB and Riverbed Stingray"HTTP_FORWARDED_FOR",# RFC 7239"HTTP_FORWARDED",# RFC 7239"HTTP_VIA",# Squid and others"X-CLIENT-IP",# Microsoft Azure"X-REAL-IP",# NGINX"X-CLUSTER-CLIENT-IP",# Rackspace Cloud Load Balancers"X_FORWARDED",# Squid"FORWARDED_FOR",# RFC 7239"CF-CONNECTING-IP",# CloudFlare"TRUE-CLIENT-IP",# CloudFlare Enterprise,"FASTLY-CLIENT-IP",# Firebase, Fastly"FORWARDED",# RFC 7239"CLIENT-IP",# Akamai and Cloudflare: True-Client-IP and Fastly: Fastly-Client-IP"REMOTE_ADDR",# Default)Alternatively, you can provide your customrequest header meta precedence orderwhen callingget_client_ip().get_client_ip(request,request_header_order=['X_FORWARDED_FOR'])get_client_ip(request,request_header_order=['X_FORWARDED_FOR','HTTP_X_FORWARDED_FOR'])Proxy CountThe default meta proxy count is 0 unless explictly provided as an argument toget_client_ip(). You may customize the order
by providing your ownIPWARE_META_PROXY_COUNTby adding it to your project's settings.pyTrusted ProxiesIf your Django server is behind one or more known proxy server(s), you can filter out unwanted requests
by providing thetrustedproxy list when callingget_client_ip(request, proxy_trusted_ips=['177.139.233.133']).
In the following example, your load balancer (LB) can be seen as atrustedproxy.`Real` Client  <public> <---> <public> LB (Server) <private> <--------> <private> Django Server
                                                                   ^
                                                                   |
 `Fake` Client  <private> <---> <private> LB (Server) <private> ---^# In the above scenario, use your load balancer IP address as a way to filter out unwanted requests.client_ip,is_routable=get_client_ip(request,proxy_trusted_ips=['177.139.233.133'])# If you have multiple proxies, simply add them to the listclient_ip,is_routable=get_client_ip(request,proxy_trusted_ips=['177.139.233.133','177.139.233.134'])# For proxy servers with fixed sub-domain and dynamic IP, use the following pattern.client_ip,is_routable=get_client_ip(request,proxy_trusted_ips=['177.139.','177.140'])client_ip,is_routable=get_client_ip(request,proxy_trusted_ips=['177.139.233.','177.139.240'])Please note:By default, theright-mostproxy in the chain is thetrustedproxy and that is the one your django
server talks to. Therefore,ipwarechecks to see if theright-mostproxy address starts with any ip pattern that was
passed in via theproxy_trusted_ipslist.Proxy CountIf your Django server is behind aknownnumber of proxy server(s), you can filter out unwanted requests
by providing thenumberof proxies when callingget_client_ip(request, proxy_count=1).
In the following example, your load balancer (LB) can be seen as theonlyproxy.`Real` Client  <public> <---> <public> LB (Server) <private> <--------> <private> Django Server
                                                                   ^
                                                                   |
                                       `Fake` Client  <private> ---^# In the above scenario, the total number of proxies can be used as a way to filter out unwanted requests.client_ip,is_routable=get_client_ip(request,proxy_count=1)# The above may be very useful in cases where your proxy server's IP address is assigned dynamically.# However, If you have the proxy IP address, you can use it in combination to the proxy count.client_ip,is_routable=get_client_ip(request,proxy_count=1,proxy_trusted_ips=['177.139.233.133'])Originating RequestIf your proxy server is configured such that the right-most IP address is that of the originating client, you
can indicateright-mostas yourproxy_orderwhen callingget_client_ip(request, proxy_order="right-most").
Please note that thede-factostandard
for the originating client IP address is theleft-mostas per<client>, <proxy1>, <proxy2>.Running the testsTo run the tests against the current environment:python manage.py testLicenseReleased under a (MIT) license.VersionX.Y.Z Version`MAJOR` version -- when you make incompatible API changes,
`MINOR` version -- when you add functionality in a backwards-compatible manner, and
`PATCH` version -- when you make backwards-compatible bug fixes.SponsorsNeekware Inc.