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' ])
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | class CIDR_LIST(list):
def __init__(self, cidrs):
self.cidrs = []
try:
#http://cheeseshop.python.org/pypi/IPv4_Utils/0.35
import ipv4
for cidr in cidrs:
self.cidrs.append(ipv4.CIDR(cidr))
except ImportError:
pass
def __contains__(self, ip):
import ipv4
try:
for cidr in self.cidrs:
if ipv4.CIDR(ip) in cidr:
return True
except:
pass
return False
|
More like this
- Template tag - list punctuation for a list of items by shapiromatron 11 months, 2 weeks ago
- JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 11 months, 3 weeks ago
- Serializer factory with Django Rest Framework by julio 1 year, 6 months ago
- Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 7 months ago
- Help text hyperlinks by sa2812 1 year, 8 months ago
Comments
Please login first before commenting.