Snippet List
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).
Ensure your geolite DB files are up to date (eg. with https://djangosnippets.org/snippets/10674/).
The checker is optional, but ensures that security is not broken due to a misspelled/missing GEOIP_COUNTRY_WHITELIST.
- middleware
- ip
- country
- geoip
- restriction
- blocker
I thought it would be useful to have a `get_addr()` method available on request objects, similar to the `get_host()` provided by Django. This middleware will add a `get_addr()` method to requests which uses the `X-Forwarded-For` header (useful if you're behind a proxy) if it's present and you have the `USE_X_FORWARDED_FOR` header set to `True` (default is `False`) and otherwise will use the `REMOTE_ADDR` environment variable. Note that if you are *not* behind a proxy and have `USE_X_FORWARDED_FOR` set to `True`, then clients can spoof their IP by simply setting the `X-Forwarded-For header`.
- request
- ip
- header
- address
- client
- remote-addr
- x-forwarded-for
- get-addr
If your application server is behind a proxy, `request.META["REMOTE_ADDR"]` 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 `HTTP_X_FORWARDED_FOR` header. This util function checks both headers. I use it behind Amazon's Elastic Load Balancer (ELB).
See code
- ip
- integer
- address
- modelfield
- int
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 __contains__ is sufficient for the purpose.)
For example, to make localhost and the entire block of 10.0.0.* considered to be internal, use:
INTERNAL_IPS = CIDR_LIST([
'127.0.0.1',
'10.0.0.0/24'
])
- debugging
- security
- ip
- cidr
- internal_ips
I wanted to store ipv4 and ipv6 ip's in django but I wanted to use the postgresql inet network field type:
http://www.postgresql.org/docs/8.3/static/datatype-net-types.html
and I wanted to use IPy.py IP objects in python. I followed these very helpful examples along with the django documentation:
http://vaig.be/2009/03/numeric-ip-field-for-django.html
http://www.djangosnippets.org/snippets/1381/
It took me awhile to figure out how this works (not that I completely understand it now..) and figured I would share it. If anyone finds problems with this or can make it better I will definitely use it.
- model
- field
- ip
- custom
- ip-address
- ip-addresses
- ipv4
- ipv6
- ipy
- postgresql-network-field
The WorldIP database provides real-world geographical location. Database is more correct than [Whois records and Whois-based databases](http://www.wipmania.com/en/blog/why-worldip-data-rather-than-whois-data-examples/), that show geographic locations of network owners, and not the geographic location of Internet-connected PC or appliance itself.
See more: [WIPmania.com](http://www.wipmania.com)
- ip
- geolocation
- geodjango
- worldip
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 [models](http://www.djangosnippets.org/snippets/327/). Make sure to set `DEBUG=False` to prevent running out of memory during import.
- log
- csv
- gis
- ip
- geolocation
- maxmind
- geodjango
- import
This provides GeoDjango models for the maxmind GeoIP Lite data products. Use the corresponding [CSV import script](http://www.djangosnippets.org/snippets/328/) for data import. Requires: [GeoDjango](http://code.djangoproject.com/wiki/GeoDjango) and the [BigIntegerField patch](http://code.djangoproject.com/attachment/ticket/399/django-bigint-20070712.patch) by Peter Nixon.
- log
- gis
- ip
- geolocation
- maxmind
- geodjango
10 snippets posted so far.