{"id":13758166,"url":"https://github.com/un33k/django-ipware","last_synced_at":"2025-05-14T09:06:38.074Z","repository":{"id":8221351,"uuid":"9659227","full_name":"un33k/django-ipware","owner":"un33k","description":"A Django application to retrieve client's IP address","archived":false,"fork":false,"pushed_at":"2024-04-19T20:02:51.000Z","size":192,"stargazers_count":1044,"open_issues_count":4,"forks_count":96,"subscribers_count":15,"default_branch":"master","last_synced_at":"2025-05-14T09:05:23.958Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/un33k.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2013-04-24T22:47:22.000Z","updated_at":"2025-05-12T17:52:34.000Z","dependencies_parsed_at":"2024-01-11T19:17:32.503Z","dependency_job_id":"a7c02429-8cc0-4b1a-b397-804c381b60ed","html_url":"https://github.com/un33k/django-ipware","commit_stats":{"total_commits":179,"total_committers":22,"mean_commits":8.136363636363637,"dds":0.3240223463687151,"last_synced_commit":"930f3293eb11b2bfc221959434e8501c165a2b61"},"previous_names":[],"tags_count":44,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/un33k%2Fdjango-ipware","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/un33k%2Fdjango-ipware/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/un33k%2Fdjango-ipware/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/un33k%2Fdjango-ipware/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/un33k","download_url":"https://codeload.github.com/un33k/django-ipware/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254110374,"owners_count":22016391,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-03T12:01:03.750Z","updated_at":"2025-05-14T09:06:38.021Z","avatar_url":"https://github.com/un33k.png","language":"Python","readme":"# Django IPware \n\n**A Django application to retrieve client's IP address**\n\n[![status-image]][status-link]\n[![version-image]][version-link]\n[![coverage-image]][coverage-link]\n\n# Alternative package\n\nIf you prefer a python only version that does not integrate with Django directly, but allows for more flexibility and advanced features, you can use the [python-ipware](https://github.com/un33k/python-ipware) package instead.  `django-ipware` is a wrapper using [python-ipware](https://github.com/un33k/python-ipware) under the hood staring from version `6.0.0`.\n\n# Overview\n\n**Best attempt** to get client's IP address while keeping it **DRY**.\n\n# Notice\n\nThere is no perfect `out-of-the-box` solution against fake IP addresses, aka `IP Address Spoofing`.\nYou are encouraged to read the ([Advanced users](README.md#advanced-users)) section of this page and\nuse `trusted_proxies_ips` and/or `proxy_count` features to match your needs, especially `if` you are\nplanning to include `ipware` in any authentication, security or `anti-fraud` related architecture.\n\nThis is an open source project, with the source code visible to all. Therefore, it may be exploited through unimplemented, or improperly implemented features.\n\nPlease use ipware `ONLY` as a complement to your `firewall` security measures!\n\n# How to install\n\n    1. easy_install django-ipware\n    2. pip install django-ipware\n    3. git clone http://github.com/un33k/django-ipware\n        a. cd django-ipware\n        b. run python setup.py install\n    4. wget https://github.com/un33k/django-ipware/zipball/master\n        a. unzip the downloaded file\n        b. cd into django-ipware-* directory\n        c. run python setup.py install\n\n# How to use\n\n```python\n # In a view or a middleware where the `request` object is available\n\n from ipware import get_client_ip\n client_ip, is_routable = get_client_ip(request)\n if client_ip is None:\n    # Unable to get the client's IP address\n else:\n     # We got the client's IP address\n     if is_routable:\n         # The client's IP address is publicly routable on the Internet\n     else:\n         # The client's IP address is private\n```\n\n# Advanced users:\n\n- ### Precedence Order\n\n  The default meta precedence order is top to bottom. You may customize the order\n  by providing your own `IPWARE_META_PRECEDENCE_ORDER` by adding it to your project's settings.py\n\n  ```python\n   # The default meta precedence order (update as needed)\n   IPWARE_META_PRECEDENCE_ORDER = (\n        \"X_FORWARDED_FOR\",  # Load balancers or proxies such as AWS ELB (default client is `left-most` [`\u003cclient\u003e, \u003cproxy1\u003e, \u003cproxy2\u003e`])\n        \"HTTP_X_FORWARDED_FOR\",  # Similar to X_FORWARDED_TO\n        \"HTTP_CLIENT_IP\",  # Standard headers used by providers such as Amazon EC2, Heroku etc.\n        \"HTTP_X_REAL_IP\",  # Standard headers used by providers such as Amazon EC2, Heroku etc.\n        \"HTTP_X_FORWARDED\",  # Squid and others\n        \"HTTP_X_CLUSTER_CLIENT_IP\",  # Rackspace LB and Riverbed Stingray\n        \"HTTP_FORWARDED_FOR\",  # RFC 7239\n        \"HTTP_FORWARDED\",  # RFC 7239\n        \"HTTP_CF_CONNECTING_IP\",  # CloudFlare\n        \"X-CLIENT-IP\",  # Microsoft Azure\n        \"X-REAL-IP\",  # NGINX\n        \"X-CLUSTER-CLIENT-IP\",  # Rackspace Cloud Load Balancers\n        \"X_FORWARDED\",  # Squid\n        \"FORWARDED_FOR\",  # RFC 7239\n        \"CF-CONNECTING-IP\",  # CloudFlare\n        \"TRUE-CLIENT-IP\",  # CloudFlare Enterprise,\n        \"FASTLY-CLIENT-IP\",  # Firebase, Fastly\n        \"FORWARDED\",  # RFC 7239\n        \"CLIENT-IP\",  # Akamai and Cloudflare: True-Client-IP and Fastly: Fastly-Client-IP\n        \"REMOTE_ADDR\",  # Default\n    )\n  ```\n\n  **Alternatively**, you can provide your custom _request header meta precedence order_ when calling `get_client_ip()`.\n\n```python\nget_client_ip(request, request_header_order=['X_FORWARDED_FOR'])\nget_client_ip(request, request_header_order=['X_FORWARDED_FOR', 'HTTP_X_FORWARDED_FOR'])\n```\n\n- ### Proxy Count\n\n  The default meta proxy count is 0 unless explictly provided as an argument to `get_client_ip()`. You may customize the order\n  by providing your own `IPWARE_META_PROXY_COUNT` by adding it to your project's settings.py\n\n### Trusted Proxies\n\nIf your Django server is behind one or more known proxy server(s), you can filter out unwanted requests\nby providing the `trusted` proxy list when calling `get_client_ip(request, proxy_trusted_ips=['177.139.233.133'])`.\nIn the following example, your load balancer (LB) can be seen as a `trusted` proxy.\n\n```\n `Real` Client  \u003cpublic\u003e \u003c---\u003e \u003cpublic\u003e LB (Server) \u003cprivate\u003e \u003c--------\u003e \u003cprivate\u003e Django Server\n                                                                   ^\n                                                                   |\n `Fake` Client  \u003cprivate\u003e \u003c---\u003e \u003cprivate\u003e LB (Server) \u003cprivate\u003e ---^\n```\n\n```python\n# In the above scenario, use your load balancer IP address as a way to filter out unwanted requests.\nclient_ip, is_routable = get_client_ip(request, proxy_trusted_ips=['177.139.233.133'])\n\n# If you have multiple proxies, simply add them to the list\nclient_ip, is_routable = get_client_ip(request, proxy_trusted_ips=['177.139.233.133', '177.139.233.134'])\n\n# For proxy servers with fixed sub-domain and dynamic IP, use the following pattern.\nclient_ip, is_routable = get_client_ip(request, proxy_trusted_ips=['177.139.', '177.140'])\nclient_ip, is_routable = get_client_ip(request, proxy_trusted_ips=['177.139.233.', '177.139.240'])\n```\n\n`Please note:` By default, the `right-most` proxy in the chain is the `trusted` proxy and that is the one your django\nserver talks to. Therefore, `ipware` checks to see if the `right-most` proxy address starts with any ip pattern that was\npassed in via the `proxy_trusted_ips` list.\n\n### Proxy Count\n\nIf your Django server is behind a `known` number of proxy server(s), you can filter out unwanted requests\nby providing the `number` of proxies when calling `get_client_ip(request, proxy_count=1)`.\nIn the following example, your load balancer (LB) can be seen as the `only` proxy.\n\n```\n `Real` Client  \u003cpublic\u003e \u003c---\u003e \u003cpublic\u003e LB (Server) \u003cprivate\u003e \u003c--------\u003e \u003cprivate\u003e Django Server\n                                                                   ^\n                                                                   |\n                                       `Fake` Client  \u003cprivate\u003e ---^\n```\n\n```python\n# In the above scenario, the total number of proxies can be used as a way to filter out unwanted requests.\nclient_ip, is_routable = get_client_ip(request, proxy_count=1)\n\n# The above may be very useful in cases where your proxy server's IP address is assigned dynamically.\n# However, If you have the proxy IP address, you can use it in combination to the proxy count.\nclient_ip, is_routable = get_client_ip(request, proxy_count=1, proxy_trusted_ips=['177.139.233.133'])\n```\n\n### Originating Request\n\nIf your proxy server is configured such that the right-most IP address is that of the originating client, you\ncan indicate `right-most` as your `proxy_order` when calling `get_client_ip(request, proxy_order=\"right-most\")`.\nPlease note that the [de-facto](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/X-Forwarded-For) standard\nfor the originating client IP address is the `left-most` as per `\u003cclient\u003e, \u003cproxy1\u003e, \u003cproxy2\u003e`.\n\n# Running the tests\n\nTo run the tests against the current environment:\n\n    python manage.py test\n\n# License\n\nReleased under a ([MIT](LICENSE)) license.\n\n# Version\n\nX.Y.Z Version\n\n    `MAJOR` version -- when you make incompatible API changes,\n    `MINOR` version -- when you add functionality in a backwards-compatible manner, and\n    `PATCH` version -- when you make backwards-compatible bug fixes.\n\n[status-image]: https://github.com/un33k/django-ipware/actions/workflows/ci.yml/badge.svg\n[status-link]: https://github.com/un33k/django-ipware/actions/workflows/ci.yml\n[version-image]: https://img.shields.io/pypi/v/django-ipware.svg\n[version-link]: https://pypi.python.org/pypi/django-ipware\n[coverage-image]: https://coveralls.io/repos/un33k/django-ipware/badge.svg\n[coverage-link]: https://coveralls.io/r/un33k/django-ipware\n\n# Sponsors\n\n[Neekware Inc.](http://neekware.com)\n","funding_links":[],"categories":["Other","Python","其它"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fun33k%2Fdjango-ipware","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fun33k%2Fdjango-ipware","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fun33k%2Fdjango-ipware/lists"}