- Author:
- jdunck
- Posted:
- January 9, 2010
- Language:
- Python
- Version:
- 1.1
- Tags:
- debugging security ip cidr internal_ips
- Score:
- 0 (after 0 ratings)
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
- Automatically setup raw_id_fields ForeignKey & OneToOneField by agusmakmun 8 months, 1 week ago
- Crispy Form by sourabhsinha396 9 months ago
- ReadOnlySelect by mkoistinen 9 months, 2 weeks ago
- Verify events sent to your webhook endpoints by santos22 10 months, 1 week ago
- Django Language Middleware by agusmakmun 10 months, 3 weeks ago
Comments
Please login first before commenting.