starcluster.iptools.IpRangeList

Package starcluster :: Module iptools :: Class IpRangeList |
[hide private]
[frames] | no frames]

# Class IpRangeList

source code

List of IpRange objects.

Converts a list of dotted quad ip address and/or CIDR addresses into a list of IpRange objects. This list can perform ``in`` and ``not in`` tests and iterate all of the addresses in the range.

This can be used to convert django's conf.settings.INTERNAL_IPS list into a smart object which allows CIDR notation.

    >>> private_range = ('192.168.0.1','192.168.255.255')
    >>> INTERNAL_IPS = IpRangeList('127.0.0.1','10/8', private_range)
    >>> '127.0.0.1' in INTERNAL_IPS
    True
    >>> '10.10.10.10' in INTERNAL_IPS
    True
    >>> '192.168.192.168' in INTERNAL_IPS
    True
    >>> '172.16.0.1' in INTERNAL_IPS
    False

Instance Methods | [hide private]
__init__(self, *args) x.__init__(...) initializes x; see x.__class__.__doc__ for signature | source code
__repr__(self) | source code
__contains__(self, item) Implements membership test operators `in` and `not in` for the address range. | source code
__iter__(self) | source code
Inherited from `object`: `__delattr__`, `__format__`, `__getattribute__`, `__hash__`, `__new__`, `__reduce__`, `__reduce_ex__`, `__setattr__`, `__sizeof__`, `__str__`, `__subclasshook__`

Properties | [hide private]
Inherited from `object`: `__class__`

Method Details | [hide private]

### __init__(self, *args) (Constructor) | source code
x.__init__(...) initializes x; see x.__class__.__doc__ for signature Overrides: object.__init__ (inherited documentation)

### __repr__(self) (Representation operator) | source code
    >>> repr = IpRangeList('127.0.0.1', '10/8', '192.168/16').__repr__()
    >>> repr = eval(repr)
    >>> assert repr[0] == ('127.0.0.1', '127.0.0.1')
    >>> assert repr[1] == ('10.0.0.0', '10.255.255.255')
    >>> assert repr[2] == ('192.168.0.0', '192.168.255.255')
Overrides: object.__repr__

### __contains__(self, item) (In operator) | source code
    Implements membership test operators `in` and `not in` for the address
    range.

    >>> r = IpRangeList('127.0.0.1', '10/8', '192.168/16')
    >>> '127.0.0.1' in r
    True

    >>> '10.0.0.1' in r
    True

    >>> 2130706433 in r
    True

    >>> 'invalid' in r
    Traceback (most recent call last):
        ...
    TypeError: expected dotted-quad ip address or 32-bit integer

    Args:
        item: Dotted-quad ip address
    Returns:
        True if address is in range, False otherwise