1.11. ipaddress

This module provides functionality for dealing with an external “ipaddress” module in a Python 2 backwards compatible way. In Python 2 all string address arguments are converted to unicode which removes the ability to specify addresses as packed binary strings.

1.11.1. Functions

ip_address(address)[source]

Take an IP string/int and return an object of the correct type.

Args:
address: A string or integer, the IP address. Either IPv4 or

IPv6 addresses may be supplied; integers less than 2**32 will be considered to be IPv4 by default.

Returns:

An IPv4Address or IPv6Address object.

Raises:
ValueError: if the address passed isn’t either a v4 or a v6

address

ip_network(address, strict=True)[source]

Take an IP string/int and return an object of the correct type.

Args:
address: A string or integer, the IP network. Either IPv4 or

IPv6 networks may be supplied; integers less than 2**32 will be considered to be IPv4 by default.

Returns:

An IPv4Network or IPv6Network object.

Raises:
ValueError: if the string passed isn’t either a v4 or a v6

address. Or if the network has host bits set.

ip_interface(address)[source]

Take an IP string/int and return an object of the correct type.

Args:
address: A string or integer, the IP address. Either IPv4 or

IPv6 addresses may be supplied; integers less than 2**32 will be considered to be IPv4 by default.

Returns:

An IPv4Interface or IPv6Interface object.

Raises:
ValueError: if the string passed isn’t either a v4 or a v6

address.

Notes:

The IPv?Interface classes describe an Address on a particular Network, so they’re basically a combination of both the Address and Network classes.

is_loopback(address)[source]

Check if an address is a loopback address or a common name for the loopback interface.

Parameters

address (str) – The address to check.

Returns

Whether or not the address is a loopback address.

Return type

bool

is_valid(address)[source]

Check that the string specified appears to be either a valid IPv4 or IPv6 address.

Parameters

address (str) – The IP address to validate.

Returns

Whether the IP address appears to be valid or not.

Return type

bool

1.11.2. Classes

class IPv4Address(address)[source]

Represent and manipulate single IPv4 Addresses.

Test if the address is reserved for link-local.

Returns:

A boolean, True if the address is link-local per RFC 3927.

property is_loopback[source]

Test if the address is a loopback address.

Returns:

A boolean, True if the address is a loopback per RFC 3330.

property is_multicast[source]

Test if the address is reserved for multicast use.

Returns:

A boolean, True if the address is multicast. See RFC 3171 for details.

property is_private[source]

Test if this address is allocated for private networks.

Returns:

A boolean, True if the address is reserved per iana-ipv4-special-registry.

property is_reserved[source]

Test if the address is otherwise IETF reserved.

Returns:

A boolean, True if the address is within the reserved IPv4 Network range.

property is_unspecified[source]

Test if the address is unspecified.

Returns:

A boolean, True if this is the unspecified address as defined in RFC 5735 3.

property packed[source]

The binary representation of this address.

class IPv4Network(address, strict=True)[source]

This class represents and manipulates 32-bit IPv4 network + addresses..

Attributes: [examples for IPv4Network(‘192.0.2.0/27’)]

.network_address: IPv4Address(‘192.0.2.0’) .hostmask: IPv4Address(‘0.0.0.31’) .broadcast_address: IPv4Address(‘192.0.2.32’) .netmask: IPv4Address(‘255.255.255.224’) .prefixlen: 27

property is_global[source]

Test if this address is allocated for public networks.

Returns:

A boolean, True if the address is not reserved per iana-ipv4-special-registry.

class IPv6Address(address)[source]

Represent and manipulate single IPv6 Addresses.

property ipv4_mapped[source]

Return the IPv4 mapped address.

Returns:

If the IPv6 address is a v4 mapped address, return the IPv4 mapped address. Return None otherwise.

property is_global[source]

Test if this address is allocated for public networks.

Returns:

A boolean, true if the address is not reserved per iana-ipv6-special-registry.

Test if the address is reserved for link-local.

Returns:

A boolean, True if the address is reserved per RFC 4291.

property is_loopback[source]

Test if the address is a loopback address.

Returns:

A boolean, True if the address is a loopback address as defined in RFC 2373 2.5.3.

property is_multicast[source]

Test if the address is reserved for multicast use.

Returns:

A boolean, True if the address is a multicast address. See RFC 2373 2.7 for details.

property is_private[source]

Test if this address is allocated for private networks.

Returns:

A boolean, True if the address is reserved per iana-ipv6-special-registry, or is ipv4_mapped and is reserved in the iana-ipv4-special-registry.

property is_reserved[source]

Test if the address is otherwise IETF reserved.

Returns:

A boolean, True if the address is within one of the reserved IPv6 Network ranges.

property is_site_local[source]

Test if the address is reserved for site-local.

Note that the site-local address space has been deprecated by RFC 3879. Use is_private to test if this address is in the space of unique local addresses as defined by RFC 4193.

Returns:

A boolean, True if the address is reserved per RFC 3513 2.5.6.

property is_unspecified[source]

Test if the address is unspecified.

Returns:

A boolean, True if this is the unspecified address as defined in RFC 2373 2.5.2.

property packed[source]

The binary representation of this address.

property scope_id[source]

Identifier of a particular zone of the address’s scope.

See RFC 4007 for details.

Returns:

A string identifying the zone of the address if specified, else None.

property sixtofour[source]

Return the IPv4 6to4 embedded address.

Returns:

The IPv4 6to4-embedded address if present or None if the address doesn’t appear to contain a 6to4 embedded address.

property teredo[source]

Tuple of embedded teredo IPs.

Returns:

Tuple of the (server, client) IPs or None if the address doesn’t appear to be a teredo address (doesn’t start with 2001::/32)

class IPv6Network(address, strict=True)[source]

This class represents and manipulates 128-bit IPv6 networks.

Attributes: [examples for IPv6(‘2001:db8::1000/124’)]

.network_address: IPv6Address(‘2001:db8::1000’) .hostmask: IPv6Address(‘::f’) .broadcast_address: IPv6Address(‘2001:db8::100f’) .netmask: IPv6Address(‘ffff:ffff:ffff:ffff:ffff:ffff:ffff:fff0’) .prefixlen: 124

hosts()[source]

Generate Iterator over usable hosts in a network.

This is like __iter__ except it doesn’t return the Subnet-Router anycast address.

property is_site_local[source]

Test if the address is reserved for site-local.

Note that the site-local address space has been deprecated by RFC 3879. Use is_private to test if this address is in the space of unique local addresses as defined by RFC 4193.

Returns:

A boolean, True if the address is reserved per RFC 3513 2.5.6.