🔥 HOT: /library/ipaddress.html - Full Archive
ipaddress — IPv4/IPv6 manipulation library¶
Source code: Lib/ipaddress.py
ipaddress provides the capabilities to create, manipulate and
operate on IPv4 and IPv6 addresses and networks.
The functions and classes in this module make it straightforward to handle various tasks related to IP addresses, including checking whether or not two hosts are on the same subnet, iterating over all hosts in a particular subnet, checking whether or not a string represents a valid IP address or network definition, and so on.
This is the full module API reference—for an overview and introduction, see An introduction to the ipaddress module.
Added in version 3.3.
Convenience factory functions¶
The ipaddress module provides factory functions to conveniently create
IP addresses, networks and interfaces:
- ipaddress.ip_address(address)¶
Return an
IPv4AddressorIPv6Addressobject depending on the IP address passed as argument. Either IPv4 or IPv6 addresses may be supplied; integers less than2**32will be considered to be IPv4 by default. AValueErroris raised if address does not represent a valid IPv4 or IPv6 address.>>> ipaddress.ip_address('192.168.0.1') IPv4Address('192.168.0.1') >>> ipaddress.ip_address('2001:db8::') IPv6Address('2001:db8::')
- ipaddress.ip_network(address, strict=True)¶
Return an
IPv4NetworkorIPv6Networkobject depending on the IP address passed as argument. address is a string or integer representing the IP network. Either IPv4 or IPv6 networks may be supplied; integers less than2**32will be considered to be IPv4 by default. strict is passed toIPv4NetworkorIPv6Networkconstructor. AValueErroris raised if address does not represent a valid IPv4 or IPv6 address, or if the network has host bits set.>>> ipaddress.ip_network('192.168.0.0/28') IPv4Network('192.168.0.0/28')
- ipaddress.ip_interface(address)¶
Return an
IPv4InterfaceorIPv6Interfaceobject depending on the IP address passed as argument. address is a string or integer representing the IP address. Either IPv4 or IPv6 addresses may be supplied; integers less than2**32will be considered to be IPv4 by default. AValueErroris raised if address does not represent a valid IPv4 or IPv6 address.
One downside of these convenience functions is that the need to handle both IPv4 and IPv6 formats means that error messages provide minimal information on the precise error, as the functions don’t know whether the IPv4 or IPv6 format was intended. More detailed error reporting can be obtained by calling the appropriate version specific class constructors directly.
IP Addresses¶
Address objects¶
The IPv4Address and IPv6Address objects share a lot of common
attributes. Some attributes that are only meaningful for IPv6 addresses are
also implemented by IPv4Address objects, in order to make it easier to
write code that handles both IP versions correctly. Address objects are
hashable, so they can be used as keys in dictionaries.
- class ipaddress.IPv4Address(address)¶
Construct an IPv4 address. An
AddressValueErroris raised if address is not a valid IPv4 address.The following constitutes a valid IPv4 address:
A string in decimal-dot notation, consisting of four decimal integers in the inclusive range 0–255, separated by dots (e.g.
192.168.0.1). Each integer represents an octet (byte) in the address. Leading zeroes are not tolerated to prevent confusion with octal notation.An integer that fits into 32 bits.
An integer packed into a
bytesobject of length 4 (most significant octet first).
>>> ipaddress.IPv4Address('192.168.0.1') IPv4Address('192.168.0.1') >>> ipaddress.IPv4Address(3232235521) IPv4Address('192.168.0.1') >>> ipaddress.IPv4Address(b'\xC0\xA8\x00\x01') IPv4Address('192.168.0.1')
Changed in version 3.8: Leading zeros are tolerated, even in ambiguous cases that look like octal notation.
Changed in version 3.9.5: Leading zeros are no longer tolerated and are treated as an error. IPv4 address strings are now parsed as strict as glibc
inet_pton().- version¶
The appropriate version number:
4for IPv4,6for IPv6.Changed in version 3.14: Made available on the class.
- max_prefixlen¶
The total number of bits in the address representation for this version:
32for IPv4,128for IPv6.The prefix defines the number of leading bits in an address that are compared to determine whether or not an address is part of a network.
Changed in version 3.14: Made available on the class.
- compressed¶
- exploded¶
The string representation in dotted decimal notation. Leading zeroes are never included in the representation.
As IPv4 does not define a shorthand notation for addresses with octets set to zero, these two attributes are always the same as
str(addr)for IPv4 addresses. Exposing these attributes makes it easier to write display code that can handle both IPv4 and IPv6 addresses.
- packed¶
The binary representation of this address - a
bytesobject of the appropriate length (most significant octet first). This is 4 bytes for IPv4 and 16 bytes for IPv6.
- reverse_pointer¶
The name of the reverse DNS PTR record for the IP address, e.g.:
>>> ipaddress.ip_address("127.0.0.1").reverse_pointer '1.0.0.127.in-addr.arpa' >>> ipaddress.ip_address("2001:db8::1").reverse_pointer '1.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.8.b.d.0.1.0.0.2.ip6.arpa'
This is the name that could be used for performing a PTR lookup, not the resolved hostname itself.
Added in version 3.5.
- is_multicast¶
Trueif the address is reserved for multicast use. See RFC 3171 (for IPv4) or RFC 2373 (for IPv6).
- is_private¶
Trueif the address is defined as not globally reachable by iana-ipv4-special-registry (for IPv4) or iana-ipv6-special-registry (for IPv6) with the following exceptions:is_privateisFalsefor the shared address space (100.64.0.0/10)For IPv4-mapped IPv6-addresses the
is_privatevalue is determined by the semantics of the underlying IPv4 addresses and the following condition holds (seeIPv6Address.ipv4_mapped):address.is_private == address.ipv4_mapped.is_private
is_privatehas value opposite tois_global, except for the shared address space (100.64.0.0/10range) where they are bothFalse.Changed in version 3.13: Fixed some false positives and false negatives.
192.0.0.0/24is considered private with the exception of192.0.0.9/32and192.0.0.10/32(previously: only the192.0.0.0/29sub-range was considered private).64:ff9b:1::/48is considered private.2002::/16is considered private.There are exceptions within
2001::/23(otherwise considered private):2001:1::1/128,2001:1::2/128,2001:3::/32,2001:4:112::/48,2001:20::/28,2001:30::/28. The exceptions are not considered private.
- is_global¶
Trueif the address is defined as globally reachable by iana-ipv4-special-registry (for IPv4) or iana-ipv6-special-registry (for IPv6) with the following exception:For IPv4-mapped IPv6-addresses the
is_privatevalue is determined by the semantics of the underlying IPv4 addresses and the following condition holds (seeIPv6Address.ipv4_mapped):address.is_global == address.ipv4_mapped.is_global
is_globalhas value opposite tois_private, except for the shared address space (100.64.0.0/10range) where they are bothFalse.Added in version 3.4.
Changed in version 3.13: Fixed some false positives and false negatives, see
is_privatefor details.