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

Class IpRange

source code


Range of ip addresses.

Converts a CIDR notation address, tuple of ip addresses or start and end addresses into a smart object which can perform ``in`` and ``not in`` tests and iterate all of the addresses in the range.

>>> r = IpRange('127.0.0.1', '127.255.255.255')
>>> '127.127.127.127' in r
True
>>> '10.0.0.1' in r
False
>>> 2130706433 in r
True
>>> r = IpRange('127/24')
>>> print(r)
('127.0.0.0', '127.0.0.255')
>>> r = IpRange('127/30')
>>> for ip in r:
...     print(ip)
127.0.0.0
127.0.0.1
127.0.0.2
127.0.0.3
>>> print(IpRange('127.0.0.255', '127.0.0.0'))
('127.0.0.0', '127.0.0.255')
Instance Methods [hide private]
 
__init__(self, start, end=None)
Args:...
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)
Return an iterator over the range.
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, start, end=None)
(Constructor)

source code 

Args:
    start: Ip address in dotted quad format or CIDR notation or tuple
        of ip addresses in dotted quad format
    end: Ip address in dotted quad format or None

Overrides: object.__init__

__repr__(self)
(Representation operator)

source code 
>>> print(IpRange('127.0.0.1'))
('127.0.0.1', '127.0.0.1')
>>> print(IpRange('10/8'))
('10.0.0.0', '10.255.255.255')
>>> print(IpRange('127.0.0.255', '127.0.0.0'))
('127.0.0.0', '127.0.0.255')
Overrides: object.__repr__

__contains__(self, item)
(In operator)

source code 

Implements membership test operators `in` and `not in` for the address
range.


>>> r = IpRange('127.0.0.1', '127.255.255.255')
>>> '127.127.127.127' in r
True

>>> '10.0.0.1' in r
False

>>> 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

__iter__(self)

source code 

Return an iterator over the range.

>>> iter = IpRange('127/31').__iter__()
>>> next(iter)
'127.0.0.0'
>>> next(iter)
'127.0.0.1'
>>> next(iter)
Traceback (most recent call last):
    ...
StopIteration