Code

Opened 7 years ago

Closed 4 years ago

#3237 closed enhancement (wontfix)

[patch] CIDR in INTERNAL_IPS

Reported by: Jeremy Dunck <jdunck@…> Owned by: nobody
Component: Core (Other) Version:
Severity: normal Keywords: CIDR INTERNAL_IPS
Cc: Triage Stage: Design decision needed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description

It'd be nice if INTERNAL_IPS could support CIDR expressions:

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


INTERNAL_IPS = CIDR_LIST([

'127.0.0.1',
'10.0.0.0/24'
])

Attachments (0)

Change History (9)

comment:1 Changed 7 years ago by Jeremy Dunck <jdunck@…>

code, un-hosed:

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
    
INTERNAL_IPS = CIDR_LIST([
    '127.0.0.1',
    '10.0.0.0/24'
    ])

comment:2 Changed 7 years ago by Simon G. <dev@…>

  • Has patch set
  • Keywords CIDR INTERNAL_IPS added
  • Triage Stage changed from Unreviewed to Design decision needed

comment:3 Changed 7 years ago by Simon G. <dev@…>

  • Summary changed from CIDR in INTERNAL_IPS to [patch] CIDR in INTERNAL_IPS

comment:4 Changed 6 years ago by jacob

  • Resolution set to wontfix
  • Status changed from new to closed

I don't think this is something that should be added to django proper, but you can bet your butt I'll use this code myself!

comment:5 Changed 5 years ago by kcarnold

  • Has patch unset
  • Resolution wontfix deleted
  • Status changed from closed to reopened

A simpler version (see http://www.djangosnippets.org/snippets/1380/)

from fnmatch import fnmatch
class glob_list(list):
    def __contains__(self, key):
        for elt in self:
            if fnmatch(key, elt): return True
        return False

INTERNAL_IPS = glob_list([
    '127.0.0.1',
    '18.85.*.*'
    ])

This is simple and useful enough that I'll entertain the thought of putting something like it in Django proper.

comment:6 Changed 5 years ago by gav

If the first one didn't belong in Django (which uses standard CIDR syntax accepted by sysadmins everywhere), this one definitely shouldn't be part of Django, with non-standard and very inflexible syntax that only supports a tiny subset of what CIDR is used for. Nominating for closure.

comment:7 Changed 5 years ago by kcarnold

gav's right about my solution being very limited. If something is going to go in Django, it should be reasonably general.

Here's an adaptation to use ipaddr-py, which has been added to the Python standard library in 3.1 (so eventually it wouldn't be a separate dependency):

from ipaddr import IP
class IpList(object):
    def __init__(self, addresses):
        self.addresses = [IP(address) for address in addresses]
    def __contains__(self, ip):
        ip = IP(ip)
        for address in self.addresses:
            if ip in address:
                return True
        return False
    
INTERNAL_IPS = IpList([
    '127.0.0.1',
    '10.0.0.0/24'
    ])

(I don't subclass list because we don't intend to support the full list interface.)

btw, "I don't think this is something that should be added to django proper, but you can bet your butt I'll use this code myself!" -- isn't that what "contrib" is usually for?

comment:8 Changed 5 years ago by ubernostrum

The ipaddr module was actually yanked out of 3.1 prior to final release, due to some controversy over whether it was really a good API for this. So I'd be somewhat wary of depending on it; whatever ends up in later 3.x Pythons may not resemble it at all.

comment:9 Changed 4 years ago by jdunck

  • Resolution set to wontfix
  • Status changed from reopened to closed

I made this a snippet:
http://www.djangosnippets.org/snippets/1862/

Closing.

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.