Package netaddr :: Module address :: Class CIDR
[frames] | no frames]

Class CIDR

source code

object --+    
         |    
 AddrRange --+
             |
            CIDR

A block of contiguous IPv4 or IPv6 network addresses defined by a base network address and a bitmask prefix or subnet mask address indicating the size/extent of the subnet.

By default, this class does not allow any non-zero bits to be set right of the bitmask when it is applied to the supplied base address. Doing so raises an AddrFormatError exception. However, it is now configurable and will allow a less strict base address if you ask for one. Be aware though that the bitmask will be applied to the base address and and trailing non-zero bits removed losing the original address. It will *not* be preserved!

Contrast this behaviour with the IP class which is less strict and has a cidr() method for returning CIDR objects without any loss of information.

Examples of supported formats :-

  1. CIDR address format - <address>/<mask_length>:
       192.168.0.0/16
       fe80::/64
    
  2. Address and subnet mask combo :
       192.168.0.0/255.255.0.0 == 192.168.0.0/16
       fe80::/ffff:ffff:ffff:ffff::  == fe80::/64
    
  3. Partial or abbreviated formats (IPv4 only). Prefixes may be omitted and in this case older class-based default prefixes apply :
    10          ==  10.0.0.0/8
    10.0        ==  10.0.0.0/8
    10/8        ==  10.0.0.0/8
    
    128         ==  128.0.0.0/16
    128.0       ==  128.0.0.0/16
    128/16      ==  128.0.0.0/16
    
    192         ==  10.0.0.0/24
    192.168.0   ==  192.168.0.0/24
    192.168/16  ==  192.168.0.0/16
    
Instance Methods
 
__init__(self, cidr, klass=<class 'netaddr.address.IP'>, strict_bitmask=True)
Constructor.
source code
 
__sub__(self, other)
Subtract another CIDR from this one.
source code
 
netmask(self)
Returns: The subnet mask address of this CIDR range.
source code
 
hostmask(self)
Returns: The host mask address of this CIDR range.
source code
 
__str__(self)
str(x)
source code
 
__repr__(self)
Returns: An executable Python statement that can recreate an object with an equivalent state.
source code
 
wildcard(self)
Returns: A Wildcard object equivalent to this CIDR.
source code

Inherited from AddrRange: __contains__, __eq__, __ge__, __getitem__, __gt__, __hash__, __iadd__, __isub__, __iter__, __le__, __len__, __lt__, __ne__, data_flavour, size

Inherited from object: __delattr__, __getattribute__, __new__, __reduce__, __reduce_ex__, __setattr__

Static Methods
 
abbrev_to_verbose(abbrev_cidr)
A statis method that converts abbreviated CIDR addresses into their verbose form.
source code
Class Variables
  STRATEGIES = (netaddr.address.IPv4StrategyOpt(32, 8, '.', 4, F...
  ADDR_TYPES = (0, 4, 6)
  strategy = StrategyDescriptor(STRATEGIES)
  addr_type = AddrTypeDescriptor(ADDR_TYPES)
  prefixlen = PrefixLenDescriptor('CIDR')
  klass = KlassDescriptor(IP)

Inherited from AddrRange: first, last

Properties

Inherited from object: __class__

Method Details

abbrev_to_verbose(abbrev_cidr)
Static Method

source code 

A statis method that converts abbreviated CIDR addresses into their verbose form.

Parameters:
  • abbrev_cidr - an abbreviated CIDR network address.

    Uses the old-style classful IP address rules to decide on a default subnet prefix if one is not explicitly provided.

    Only supports IPv4 addresses.

    Examples :

       10                  - 10.0.0.0/8
       10/16               - 10.0.0.0/16
       128                 - 128.0.0.0/16
       128/8               - 128.0.0.0/8
       192.168             - 192.168.0.0/16
    
Returns:
A verbose CIDR from an abbreviated CIDR or old-style classful network address, None if format provided was not recognised or supported.

__init__(self, cidr, klass=<class 'netaddr.address.IP'>, strict_bitmask=True)
(Constructor)

source code 

Constructor.

Parameters:
  • cidr - a valid IPv4/IPv6 CIDR address or abbreviated IPv4 network address.
  • klass - (optional) type, BIF or class used to create each object returned. Default: IP class. See nrange() documentations for additional details on options.
  • strict_bitmask - (optional) performs a test to ensure there are no non-zero bits to the right of the subnet mask or prefix when it is applied to the base address. (default: True)
Overrides: object.__init__

__sub__(self, other)
(Subtraction operator)

source code 

Subtract another CIDR from this one.

Parameters:
  • other - a CIDR object that is greater than or equal to self.
Returns:
A list of CIDR objects than remain after subtracting other from self.

netmask(self)

source code 
Returns:
The subnet mask address of this CIDR range.

hostmask(self)

source code 
Returns:
The host mask address of this CIDR range.

__str__(self)
(Informal representation operator)

source code 

str(x)

Overrides: object.__str__
(inherited documentation)

__repr__(self)
(Representation operator)

source code 

repr(x)

Returns:
An executable Python statement that can recreate an object with an equivalent state.
Overrides: object.__repr__

wildcard(self)

source code 
Returns:
A Wildcard object equivalent to this CIDR.
  • If CIDR was initialised with klass=str a wildcard string is returned, in all other cases a Wildcard object is returned.
  • Only supports IPv4 CIDR addresses.

Class Variable Details

STRATEGIES

Value:
(netaddr.address.IPv4StrategyOpt(32, 8, '.', 4, False, False),
 netaddr.address.IPv6Strategy(128, 16, ':', 6, True, False))