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.
1.11.2. Classes¶
-
class
IPv4Address
(address)[source]¶ Represent and manipulate single IPv4 Addresses.
-
is_link_local
[source]¶ Test if the address is reserved for link-local.
- Returns:
- A boolean, True if the address is link-local per RFC 3927.
-
is_loopback
[source]¶ Test if the address is a loopback address.
- Returns:
- A boolean, True if the address is a loopback per RFC 3330.
-
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.
-
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.
-
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.
-
-
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
-
class
IPv6Address
(address)[source]¶ Represent and manipulate single IPv6 Addresses.
-
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.
-
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.
-
is_link_local
[source]¶ Test if the address is reserved for link-local.
- Returns:
- A boolean, True if the address is reserved per RFC 4291.
-
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.
-
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.
-
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.
-
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.
-
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.
-
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.
-
-
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.
-
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.