Package netaddr :: Package ip :: Class IPNetwork
[hide private]
[frames] | no frames]

Class IPNetwork

object --+    
         |    
    BaseIP --+
             |
            IPNetwork

An IPv4 or IPv6 network or subnet. A combination of an IP address and a network mask.

Accepts CIDR and several variants :-

a) Standard CIDR:

   x.x.x.x/y -> 192.0.2.0/24
   x::/y -> fe80::/10

b) Hybrid CIDR format (netmask address instead of prefix):

   x.x.x.x/y.y.y.y -> 192.0.2.0/255.255.255.0
   x::/y:: -> fe80::/ffc0::

where 'y' address represent a valid netmask.

c) ACL hybrid CIDR format (hostmask address instead of prefix):

   x.x.x.x/y.y.y.y -> 192.0.2.0/0.0.0.255
   x::/y:: -> fe80::/3f:ffff:ffff:ffff:ffff:ffff:ffff:ffff

where 'y' address represent a valid netmask.

This is like Cisco's ACL bitmasks.

d) Abbreviated CIDR format (as of netaddr 0.7.x this requires the optional constructor argument implicit_prefix=True):

   x       -> 192
   x/y     -> 10/8
   x.x/y   -> 192.168/16
   x.x.x/y -> 192.168.0/24

which are equivalent to:

   x.0.0.0/y   -> 192.0.0.0/24
   x.0.0.0/y   -> 10.0.0.0/8
   x.x.0.0/y   -> 192.168.0.0/16
   x.x.x.0/y   -> 192.168.0.0/24
Instance Methods [hide private]
 
__init__(self, addr, implicit_prefix=False)
Constructor.
 
_get_value(self)
 
_set_value(self, value)
 
_get_prefixlen(self)
 
_set_prefixlen(self, value)
 
__iadd__(self, num)
Increases the value of this IPNetwork object by the current size multiplied by num.
 
__isub__(self, num)
Decreases the value of this IPNetwork object by the current size multiplied by num.
 
__iter__(self)
Returns: An iterator providing access to all IPAddress objects within range represented by this IPNetwork object.
 
__getitem__(self, index)
Returns: The IP address(es) in this IPNetwork object referenced by index or slice.
 
__len__(self)
Returns: the number of IP addresses in this IPNetwork.
 
__contains__(self, other)
Returns: True if other falls within the boundary of this one, False otherwise.
 
key(self)
Returns: A key tuple used to uniquely identify this IPNetwork.
 
sort_key(self)
Returns: A key tuple used to compare and sort this IPNetwork correctly.
 
ipv4(self)
Returns: A numerically equivalent version 4 IPNetwork object.
 
ipv6(self, ipv4_compatible=False)
Please Note: the IPv4-mapped IPv6 address format is now considered deprecated.
 
previous(self, step=1)
Returns: The adjacent subnet preceding this IPNetwork object.
 
next(self, step=1)
Returns: The adjacent subnet succeeding this IPNetwork object.
 
supernet(self, prefixlen=0)
Provides a list of supernets for this IPNetwork object between the size of the current prefix and (if specified) an endpoint prefix.
 
subnet(self, prefixlen, count=None, fmt=None)
A generator that divides up this IPNetwork's subnet into smaller subnets based on a specified CIDR prefix.
 
iter_hosts(self)
Returns: An iterator that provides all IP addresses that can be assigned to hosts within the range of this IP object's subnet.
 
__str__(self)
Returns: this IPNetwork in CIDR format
 
__repr__(self)
Returns: Python statement to create an equivalent object

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

    Inherited from BaseIP
 
__eq__(self, other)
Returns: True if this IPAddress or IPNetwork object is equivalent to other, False otherwise.
 
__ge__(self, other)
Returns: True if this IPAddress or IPNetwork object is greater than or equal to other, False otherwise.
 
__gt__(self, other)
Returns: True if this IPAddress or IPNetwork object is greater than other, False otherwise.
 
__hash__(self)
Returns: A hash value uniquely indentifying this IP object.
 
__le__(self, other)
Returns: True if this IPAddress or IPNetwork object is less than or equal to other, False otherwise.
 
__lt__(self, other)
Returns: True if this IPAddress or IPNetwork object is less than other, False otherwise.
 
__ne__(self, other)
Returns: True if this IPAddress or IPNetwork object is not equivalent to other, False otherwise.
 
is_ipv4_compat(self)
Returns: True if this IP is IPv4-mapped IPv6 address, False otherwise.
 
is_ipv4_mapped(self)
Returns: True if this IP is IPv4-compatible IPv6 address, False otherwise.
 
is_link_local(self)
Returns: True if this IP is link-local address False otherwise.
 
is_loopback(self)
Returns: True if this IP is loopback address (not for network transmission), False otherwise.
 
is_multicast(self)
Returns: True if this IP is multicast, False otherwise
 
is_private(self)
Returns: True if this IP is for internal/private use only (i.e.
 
is_reserved(self)
Returns: True if this IP is in IANA reserved range, False otherwise.
 
is_unicast(self)
Returns: True if this IP is unicast, False otherwise
Properties [hide private]
  value
a positive integer representing the value of this IP address.
  prefixlen
size of the bitmask used to indentify and separate the network identifier from the host identifier in this IP address.
  ip
The IP address of this IPNetwork object.
  network
The network address of this IPNetwork object.
  broadcast
The broadcast address of this IPNetwork object
  first
The integer value of first IP address found within this IPNetwork object.
  last
The integer value of last IP address found within this IPNetwork object.
  netmask
The subnet mask of this IPNetwork object.
  hostmask
The host mask of this IPNetwork object.
  cidr
The true CIDR address for this IPNetwork object which omits any host bits to the right of the CIDR subnet prefix.
  size
The total number of IP addresses within this IPNetwork object.

Inherited from object: __class__

    Inherited from BaseIP
  info
A record dict containing IANA registration details for this IP address if available, None otherwise.
  version
the IP protocol version represented by this IP object.
Method Details [hide private]

__init__(self, addr, implicit_prefix=False)
(Constructor)

 

Constructor.

Parameters:
  • addr - an IPv4 or IPv6 address with optional CIDR prefix, netmask or hostmask. May be an IP address in representation (string) format, an integer or another IP object (copy construction).
  • implicit_prefix - if True, the constructor uses classful IPv4 rules to select a default prefix when one is not provided. If False it uses the length of the IP address version. (default: False).
Overrides: object.__init__

__iadd__(self, num)

 

Increases the value of this IPNetwork object by the current size multiplied by num.

Parameters:
  • num - (optional) number of IPNetwork blocks to increment this IPNetwork's value by.

    An IndexError is raised if result exceeds maximum IP address value.

__isub__(self, num)

 

Decreases the value of this IPNetwork object by the current size multiplied by num.

Parameters:
  • num - (optional) number of IPNetwork blocks to decrement this IPNetwork's value by.

    An IndexError is raised if result is less than zero.

__iter__(self)

 
Returns:
An iterator providing access to all IPAddress objects within range represented by this IPNetwork object.

__getitem__(self, index)
(Indexing operator)

 
Returns:
The IP address(es) in this IPNetwork object referenced by index or slice. As slicing can produce large sequences of objects an iterator is returned instead of the more usual list.

__len__(self)
(Length operator)

 
Returns:
the number of IP addresses in this IPNetwork. Raises an IndexError if size > sys.maxint (a Python 2.x limitation). Use the .size property for subnets of any size.

__contains__(self, other)
(In operator)

 
Parameters:
Returns:
True if other falls within the boundary of this one, False otherwise.

key(self)

 
Returns:
A key tuple used to uniquely identify this IPNetwork.
Overrides: BaseIP.key

sort_key(self)

 
Returns:
A key tuple used to compare and sort this IPNetwork correctly.
Overrides: BaseIP.sort_key

ipv4(self)

 
Returns:
A numerically equivalent version 4 IPNetwork object. Raises an AddrConversionError is IPv6 address cannot be converted to IPv4.

ipv6(self, ipv4_compatible=False)

 

Please Note: the IPv4-mapped IPv6 address format is now considered deprecated. See RFC 4291 or later for details.

Parameters:
  • ipv4_compatible - If True returns an IPv4-mapped address (::ffff:x.x.x.x), an IPv4-compatible (::x.x.x.x) address otherwise. Default: False (IPv4-mapped).
Returns:
A numerically equivalent version 6 IPNetwork object.

previous(self, step=1)

 
Parameters:
  • step - the number of IP subnets between this IPNetwork object and the expected subnet. Default: 1 (the previous IP subnet).
Returns:
The adjacent subnet preceding this IPNetwork object.

next(self, step=1)

 
Parameters:
  • step - the number of IP subnets between this IPNetwork object and the expected subnet. Default: 1 (the next IP subnet).
Returns:
The adjacent subnet succeeding this IPNetwork object.

supernet(self, prefixlen=0)

 

Provides a list of supernets for this IPNetwork object between the size of the current prefix and (if specified) an endpoint prefix.

Parameters:
  • prefixlen - (optional) a CIDR prefix for the maximum supernet. Default: 0 - returns all possible supernets.
Returns:
a tuple of supernet IPNetwork objects.

subnet(self, prefixlen, count=None, fmt=None)

 

A generator that divides up this IPNetwork's subnet into smaller subnets based on a specified CIDR prefix.

Parameters:
  • prefixlen - a CIDR prefix indicating size of subnets to be returned.
  • count - (optional) number of consecutive IP subnets to be returned.
Returns:
an iterator containing IPNetwork subnet objects.

iter_hosts(self)

 
Returns:
An iterator that provides all IP addresses that can be assigned to hosts within the range of this IP object's subnet.
  • for IPv4, the network and broadcast addresses are always excluded. Any subnet that contains less than 4 IP addresses yields an empty list.
  • for IPv6, only the unspecified address '::' is excluded from any yielded IP addresses.

__str__(self)
(Informal representation operator)

 

str(x)

Returns:
this IPNetwork in CIDR format
Overrides: object.__str__

__repr__(self)
(Representation operator)

 

repr(x)

Returns:
Python statement to create an equivalent object
Overrides: object.__repr__

Property Details [hide private]

value

a positive integer representing the value of this IP address.

Get Method:
_get_value(self)
Set Method:
_set_value(self, value)

prefixlen

size of the bitmask used to indentify and separate the network identifier from the host identifier in this IP address.

Get Method:
_get_prefixlen(self)
Set Method:
_set_prefixlen(self, value)

ip

The IP address of this IPNetwork object. This is may or may not be the same as the network IP address which varies according to the value of the CIDR subnet prefix.

Get Method:
unreachable.ip(self) - The IP address of this IPNetwork object.

network

The network address of this IPNetwork object.

Get Method:
unreachable.network(self) - The network address of this IPNetwork object.

broadcast

The broadcast address of this IPNetwork object

Get Method:
unreachable.broadcast(self) - The broadcast address of this IPNetwork object

first

The integer value of first IP address found within this IPNetwork object.

Get Method:
unreachable.first(self) - The integer value of first IP address found within this IPNetwork object.

last

The integer value of last IP address found within this IPNetwork object.

Get Method:
unreachable.last(self) - The integer value of last IP address found within this IPNetwork object.

netmask

The subnet mask of this IPNetwork object.

Get Method:
unreachable.netmask(self) - The subnet mask of this IPNetwork object.

hostmask

The host mask of this IPNetwork object.

Get Method:
unreachable.hostmask(self) - The host mask of this IPNetwork object.

cidr

The true CIDR address for this IPNetwork object which omits any host bits to the right of the CIDR subnet prefix.

Get Method:
unreachable.cidr(self) - The true CIDR address for this IPNetwork object which omits any host bits to the right of the CIDR subnet prefix.

size

The total number of IP addresses within this IPNetwork object.

Get Method:
unreachable.size(self) - The total number of IP addresses within this IPNetwork object.