djangosnippets.org: Latest snippets tagged with 'ip'https://djangosnippets.org/tags/ip/2019-06-26T07:19:45.268959-05:00Geoip middleware to restrict users to a set of allowed countries
2019-06-26T07:19:45.268959-05:00pakalhttps://djangosnippets.org/snippets/10675/<p>This middleware uses Django's Geoip support (https://docs.djangoproject.com/fr/2.2/ref/contrib/gis/geoip2/), as well as axes's package helper to retrieve IP address (since Django's REMOTE_ADDR might be wrong when behind a reverse proxy).</p>
<p>Ensure your geolite DB files are up to date (eg. with https://djangosnippets.org/snippets/10674/).</p>
<p>The checker is optional, but ensures that security is not …</p>
Freely redistributableAdd get_addr() method to request object
2012-12-13T17:17:26.955015-06:00nikmolnarhttps://djangosnippets.org/snippets/2863/<p>I thought it would be useful to have a <code>get_addr()</code> method available on request objects, similar to the <code>get_host()</code> provided by Django. This middleware will add a <code>get_addr()</code> method to requests which uses the <code>X-Forwarded-For</code> header (useful if you're behind a proxy) if it's present and you have the <code>USE_X_FORWARDED_FOR …</code></p>
Freely redistributableGet Client IP Behind Proxy
2011-10-21T09:35:30.037659-05:00brianjaystanleyhttps://djangosnippets.org/snippets/2575/<p>If your application server is behind a proxy, <code>request.META["REMOTE_ADDR"]</code> will likely return the proxy server's IP, not the client's IP. The proxy server will usually provide the client's IP in the <code>HTTP_X_FORWARDED_FOR</code> header. This util function checks both headers. I use it behind Amazon's Elastic Load Balancer (ELB).</p>
Freely redistributableStoring IP address ModelField as Integer, without nasty hacks..
2011-07-19T07:15:12.577979-05:00sleepycalhttps://djangosnippets.org/snippets/2493/<p>See code</p>
Freely redistributableSupport IP ranges in INTERNAL_IPS
2010-01-09T18:28:11.151972-06:00jdunckhttps://djangosnippets.org/snippets/1862/<p>CIDR ( http://en.wikipedia.org/wiki/Classless_Inter-Domain_Routing ) is a well-known IP range syntax. This CIDR_LIST class can be used to make ranges of IPs considered "internal" for Django's debugging and security purposes. (Django only ever needs to do "ip in INTERNAL_IPS" so <strong>contains</strong> is sufficient for the purpose.)
</p>
<p>For example, to make localhost …</p>
Freely redistributableTuned IPAddressField with IPv4 & IPv6 support using Postgres Network Field type
2009-04-20T10:31:12.653189-05:00illscihttps://djangosnippets.org/snippets/1453/<p>I wanted to store ipv4 and ipv6 ip's in django but I wanted to use the postgresql inet network field type:</p>
<p>http://www.postgresql.org/docs/8.3/static/datatype-net-types.html</p>
<p>and I wanted to use IPy.py IP objects in python. I followed these very helpful examples along with the django documentation:</p>
<p>http://vaig.be/2009/03/numeric-ip-field-for-django.html
http://www.djangosnippets.org/snippets/1381/</p>
<p>It took me awhile to …</p>
Freely redistributableTuned IPAddressField with IPv4 & IPv6 support
2009-03-20T15:21:59.705446-05:00divermanhttps://djangosnippets.org/snippets/1381/<p>Tuned IPAddressField with IPv4 & IPv6 support</p>
Freely redistributableWorldIP - access to IP database over API
2008-09-11T15:22:25.088851-05:00Alrondhttps://djangosnippets.org/snippets/1046/<p>The WorldIP database provides real-world geographical location. Database is more correct than <a href="http://www.wipmania.com/en/blog/why-worldip-data-rather-than-whois-data-examples/">Whois records and Whois-based databases</a>, that show geographic locations of network owners, and not the geographic location of Internet-connected PC or appliance itself.
See more: <a href="http://www.wipmania.com">WIPmania.com</a></p>
Freely redistributableMaxMind(R) GeoIP Lite CSV Import
2007-07-18T20:55:43.363148-05:00jbronnhttps://djangosnippets.org/snippets/328/<p>Use this script to import the Maxmind GeoIP lite CSV datasets into your database. This takes at least 200MB of RAM; the resulting database will be ~400MB. Stick in the same directory as the <a href="http://www.djangosnippets.org/snippets/327/">models</a>. Make sure to set <code>DEBUG=False</code> to prevent running out of memory during import.</p>
Freely redistributableMaxMind(R) GeoIP Lite geolocation models
2007-07-18T20:49:11.932111-05:00jbronnhttps://djangosnippets.org/snippets/327/<p>This provides GeoDjango models for the maxmind GeoIP Lite data products. Use the corresponding <a href="http://www.djangosnippets.org/snippets/328/">CSV import script</a> for data import. Requires: <a href="http://code.djangoproject.com/wiki/GeoDjango">GeoDjango</a> and the <a href="http://code.djangoproject.com/attachment/ticket/399/django-bigint-20070712.patch">BigIntegerField patch</a> by Peter Nixon. </p>
Freely redistributable