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
- Browser-native date input field by kytta 4 weeks ago
- Generate and render HTML Table by LLyaudet 1 month, 1 week ago
- My firs Snippets by GutemaG 1 month, 1 week ago
- FileField having auto upload_to path by junaidmgithub 2 months, 2 weeks ago
- LazyPrimaryKeyRelatedField by LLyaudet 2 months, 3 weeks ago
Comments
Please login first before commenting.