Login

Support IP ranges in INTERNAL_IPS

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

  1. Include entire networks in INTERNAL_IPS setting by pmclanahan 6 years, 5 months ago
  2. Globs for INTERNAL_IPS by kcarnold 6 years, 5 months ago
  3. Internal view decorator by gsakkis 5 years, 1 month ago
  4. Debug SQL Query in Template by dario.agliottone 3 years, 3 months ago
  5. Middleware to prevent access to the admin when user ip not in INTERNAL_IPS by jezdez 5 years, 1 month ago

Comments

Please login first before commenting.