Source code: Lib/ipaddress.py Show
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. New in version 3.3. Convenience factory functions¶The
ipaddress. ip_address (address)¶Return an >>> 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
>>> ipaddress.ip_network('192.168.0.0/28') IPv4Network('192.168.0.0/28') ipaddress. ip_interface (address)¶Return an
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
ipaddress. IPv4Address (address)¶Construct an IPv4 address. An The following constitutes a valid IPv4 address:
>>> 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.10: Leading zeros are no longer tolerated and are treated as an error. IPv4 address strings are now parsed as strict as glibc
Changed in version 3.9.5: The above change was also included in Python 3.9 starting with version 3.9.5. Changed in version 3.8.12: The above change was also included in Python 3.8 starting with version 3.8.12. version ¶The appropriate version number: max_prefixlen ¶The total
number of bits in the address representation for this version: 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. 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 packed ¶The binary representation of this address - a 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. New in version 3.5. is_multicast ¶
is_private ¶
is_global ¶
New in version 3.4. is_unspecified ¶
is_reserved ¶
is_loopback ¶
is_link_local ¶
IPv4Address. __format__ (fmt)¶Returns a string representation of the IP address, controlled by an explicit format string. fmt can be one of the following: >>> format(ipaddress.IPv4Address('192.168.0.1')) '192.168.0.1' >>> '{:#b}'.format(ipaddress.IPv4Address('192.168.0.1')) '0b11000000101010000000000000000001' >>> f'{ipaddress.IPv6Address("2001:db8::1000"):s}' '2001:db8::1000' >>> format(ipaddress.IPv6Address('2001:db8::1000'), '_X') '2001_0DB8_0000_0000_0000_0000_0000_1000' >>> '{:#_n}'.format(ipaddress.IPv6Address('2001:db8::1000')) '0x2001_0db8_0000_0000_0000_0000_0000_1000' New in version 3.9. classipaddress. IPv6Address (address)¶Construct an IPv6 address. An
The following constitutes a valid IPv6 address:
>>> ipaddress.IPv6Address('2001:db8::1000') IPv6Address('2001:db8::1000') >>> ipaddress.IPv6Address('ff02::5678%1') IPv6Address('ff02::5678%1') compressed ¶The short form of the address representation, with leading zeroes in groups omitted and the longest sequence of groups consisting entirely of zeroes collapsed to a single empty group. This is also the value returned by exploded ¶The long form of the address representation, with all leading zeroes and groups consisting entirely of zeroes included. For the following attributes and methods, see the corresponding documentation of the
packed ¶
reverse_pointer ¶ version ¶
max_prefixlen ¶ is_multicast ¶ is_private ¶ is_global ¶
is_unspecified ¶ is_reserved ¶
is_loopback ¶ is_link_local ¶New in version 3.4: is_global is_site_local ¶
ipv4_mapped ¶For addresses that appear to be IPv4 mapped addresses (starting with scope_id ¶For scoped addresses as defined by RFC 4007, this property identifies the particular zone of the address’s scope that the address belongs to, as a string. When no scope zone is specified, this
property will be sixtofour ¶For addresses that appear to be 6to4 addresses (starting with teredo ¶For addresses that appear to be Teredo addresses (starting with IPv6Address. __format__ (fmt)¶Refer to the corresponding method documentation in New in version 3.9. Conversion to Strings and Integers¶To interoperate with networking interfaces such as the socket module, addresses must be converted to strings or integers. This is handled using the
>>> str(ipaddress.IPv4Address('192.168.0.1')) '192.168.0.1' >>> int(ipaddress.IPv4Address('192.168.0.1')) 3232235521 >>> str(ipaddress.IPv6Address('::1')) '::1' >>> int(ipaddress.IPv6Address('::1')) 1 Note that IPv6 scoped addresses are converted to integers without scope zone ID. Operators¶Address objects support some operators. Unless stated otherwise, operators can only be applied between compatible objects (i.e. IPv4 with IPv4, IPv6 with IPv6). Comparison operators¶Address objects can be compared with the usual set of comparison operators. Same IPv6 addresses with different scope zone IDs are not equal. Some examples: >>> IPv4Address('127.0.0.2') > IPv4Address('127.0.0.1') True >>> IPv4Address('127.0.0.2') == IPv4Address('127.0.0.1') False >>> IPv4Address('127.0.0.2') != IPv4Address('127.0.0.1') True >>> IPv6Address('fe80::1234') == IPv6Address('fe80::1234%1') False >>> IPv6Address('fe80::1234%1') != IPv6Address('fe80::1234%2') True Arithmetic operators¶Integers can be added to or subtracted from address objects. Some examples: >>> IPv4Address('127.0.0.2') + 3 IPv4Address('127.0.0.5') >>> IPv4Address('127.0.0.2') - 3 IPv4Address('126.255.255.255') >>> IPv4Address('255.255.255.255') + 1 Traceback (most recent call last): File "<stdin>", line 1, in <module> ipaddress.AddressValueError: 4294967296 (>= 2**32) is not permitted as an IPv4 address IP Network definitions¶The Prefix, net mask and host mask¶There are several equivalent ways to specify IP network masks. A prefix Network objects¶All attributes implemented by address objects are implemented by network objects as well. In addition, network objects implement additional attributes. All of these are common between ipaddress. IPv4Network (address, strict=True)¶Construct an IPv4 network definition. address can be one of the following:
An
If strict is Unless stated otherwise, all network methods accepting other network/address objects will raise Changed in version 3.5: Added the two-tuple form for the address constructor parameter. version ¶
max_prefixlen ¶Refer to the corresponding attribute documentation in is_multicast ¶ is_private ¶
is_unspecified ¶ is_reserved ¶
is_loopback ¶ is_link_local ¶These attributes are true for the network as a whole if they are true for both the network address and the broadcast address. network_address ¶The network address for the network. The network address and the prefix length together uniquely define a network. broadcast_address ¶The broadcast address for the network. Packets sent to the broadcast address should be received by every host on the network. hostmask ¶The host mask, as an netmask ¶The net mask, as an with_prefixlen ¶ compressed ¶
exploded ¶A string representation of the network, with the mask in prefix notation.
with_netmask ¶A string representation of the network, with the mask in net mask notation. with_hostmask ¶
A string representation of the network, with the mask in host mask notation. num_addresses ¶The total number of addresses in the network. prefixlen ¶Length of the network prefix, in bits. hosts ()¶Returns an iterator over the usable hosts in the network. The usable hosts are all the IP addresses that belong to the network, except the network address itself and the network broadcast address. For networks with a mask length of 31, the network address and network broadcast address are also included in the result. Networks with a mask of 32 will return a list containing the single host address. >>> list(ip_network('192.0.2.0/29').hosts()) [IPv4Address('192.0.2.1'), IPv4Address('192.0.2.2'), IPv4Address('192.0.2.3'), IPv4Address('192.0.2.4'), IPv4Address('192.0.2.5'), IPv4Address('192.0.2.6')] >>> list(ip_network('192.0.2.0/31').hosts()) [IPv4Address('192.0.2.0'), IPv4Address('192.0.2.1')] >>> list(ip_network('192.0.2.1/32').hosts()) [IPv4Address('192.0.2.1')] overlaps (other)¶
address_exclude (network)¶Computes the network definitions resulting from removing the given network from this one. Returns an iterator of network objects. Raises
>>> n1 = ip_network('192.0.2.0/28') >>> n2 = ip_network('192.0.2.1/32') >>> list(n1.address_exclude(n2)) [IPv4Network('192.0.2.8/29'), IPv4Network('192.0.2.4/30'), IPv4Network('192.0.2.2/31'), IPv4Network('192.0.2.0/32')] subnets (prefixlen_diff=1,
new_prefix=None)¶The subnets that join to make the current network definition, depending on the argument values. prefixlen_diff is the amount our prefix length should be increased by. new_prefix is the desired new prefix of the subnets; it must be larger than our prefix. One and only one of prefixlen_diff and new_prefix must be set. Returns an iterator of network objects. >>> list(ip_network('192.0.2.0/24').subnets()) [IPv4Network('192.0.2.0/25'), IPv4Network('192.0.2.128/25')] >>> list(ip_network('192.0.2.0/24').subnets(prefixlen_diff=2)) [IPv4Network('192.0.2.0/26'), IPv4Network('192.0.2.64/26'), IPv4Network('192.0.2.128/26'), IPv4Network('192.0.2.192/26')] >>> list(ip_network('192.0.2.0/24').subnets(new_prefix=26)) [IPv4Network('192.0.2.0/26'), IPv4Network('192.0.2.64/26'), IPv4Network('192.0.2.128/26'), IPv4Network('192.0.2.192/26')] >>> list(ip_network('192.0.2.0/24').subnets(new_prefix=23)) Traceback (most recent call last): File "<stdin>", line 1, in <module> raise ValueError('new prefix must be longer') ValueError: new prefix must be longer >>> list(ip_network('192.0.2.0/24').subnets(new_prefix=25)) [IPv4Network('192.0.2.0/25'), IPv4Network('192.0.2.128/25')] supernet (prefixlen_diff=1,
new_prefix=None)¶The supernet containing this network definition, depending on the argument values. prefixlen_diff is the amount our prefix length should be decreased by. new_prefix is the desired new prefix of the supernet; it must be smaller than our prefix. One and only one of prefixlen_diff and new_prefix must be set. Returns a single network object. >>> ip_network('192.0.2.0/24').supernet() IPv4Network('192.0.2.0/23') >>> ip_network('192.0.2.0/24').supernet(prefixlen_diff=2) IPv4Network('192.0.0.0/22') >>> ip_network('192.0.2.0/24').supernet(new_prefix=20) IPv4Network('192.0.0.0/20') subnet_of (other)¶Return >>> a = ip_network('192.168.1.0/24') >>> b = ip_network('192.168.1.128/30') >>> b.subnet_of(a) True New in version 3.7. supernet_of (other)¶Return >>> a = ip_network('192.168.1.0/24') >>> b = ip_network('192.168.1.128/30') >>> a.supernet_of(b) True New in version 3.7. compare_networks (other)¶Compare this network to other. In this comparison only the network addresses are considered; host bits aren’t. Returns either >>> ip_network('192.0.2.1/32').compare_networks(ip_network('192.0.2.2/32')) -1 >>> ip_network('192.0.2.1/32').compare_networks(ip_network('192.0.2.0/32')) 1 >>> ip_network('192.0.2.1/32').compare_networks(ip_network('192.0.2.1/32')) 0 Deprecated since version 3.7: It uses the same ordering and comparison algorithm as “<”, “==”, and “>” classipaddress. IPv6Network (address, strict=True)¶Construct an IPv6 network definition. address can be one of the following:
An If strict is Changed in version 3.5: Added the two-tuple form for the address constructor parameter. version ¶
max_prefixlen ¶ is_multicast ¶ is_private ¶ is_unspecified ¶
is_reserved ¶ is_loopback ¶
is_link_local ¶ network_address ¶
broadcast_address ¶ hostmask ¶
netmask ¶ with_prefixlen ¶
compressed ¶ exploded ¶
with_netmask ¶ with_hostmask ¶
num_addresses ¶ prefixlen ¶
hosts ()¶Returns an iterator over the usable hosts in the network. The usable hosts are all the IP addresses that belong to the network, except the Subnet-Router anycast address. For networks with a mask length of 127, the Subnet-Router anycast address is also included in the result. Networks with a mask of 128 will return a list containing the single host address. overlaps (other)¶
address_exclude (network)¶ subnets (prefixlen_diff=1,
new_prefix=None)¶ supernet (prefixlen_diff=1,
new_prefix=None)¶
subnet_of (other)¶
supernet_of (other)¶ compare_networks (other)¶Refer to the corresponding attribute documentation in is_site_local ¶These attribute is true for the network as a whole if it is true for both the network address and the broadcast address. Operators¶Network objects support some operators. Unless stated otherwise, operators can only be applied between compatible objects (i.e. IPv4 with IPv4, IPv6 with IPv6). Logical operators¶Network objects can be compared with the usual set of logical operators. Network objects are ordered first by network address, then by net mask. Iteration¶Network objects can be iterated to
list all the addresses belonging to the network. For iteration, all hosts are returned, including unusable hosts (for usable hosts, use the >>> for addr in IPv4Network('192.0.2.0/28'): ... addr ... IPv4Address('192.0.2.0') IPv4Address('192.0.2.1') IPv4Address('192.0.2.2') IPv4Address('192.0.2.3') IPv4Address('192.0.2.4') IPv4Address('192.0.2.5') IPv4Address('192.0.2.6') IPv4Address('192.0.2.7') IPv4Address('192.0.2.8') IPv4Address('192.0.2.9') IPv4Address('192.0.2.10') IPv4Address('192.0.2.11') IPv4Address('192.0.2.12') IPv4Address('192.0.2.13') IPv4Address('192.0.2.14') IPv4Address('192.0.2.15') Networks as containers of addresses¶Network objects can act as containers of addresses. Some examples: >>> IPv4Network('192.0.2.0/28')[0] IPv4Address('192.0.2.0') >>> IPv4Network('192.0.2.0/28')[15] IPv4Address('192.0.2.15') >>> IPv4Address('192.0.2.6') in IPv4Network('192.0.2.0/28') True >>> IPv4Address('192.0.3.6') in IPv4Network('192.0.2.0/28') False Interface objects¶Interface objects are hashable, so they can be used as keys in dictionaries. classipaddress. IPv4Interface (address)¶Construct an IPv4 interface. The meaning of address
is as in the constructor of
ip ¶The address
( >>> interface = IPv4Interface('192.0.2.5/24') >>> interface.ip IPv4Address('192.0.2.5') network ¶The network
( >>> interface = IPv4Interface('192.0.2.5/24') >>> interface.network IPv4Network('192.0.2.0/24') with_prefixlen ¶A string representation of the interface with the mask in prefix notation. >>> interface = IPv4Interface('192.0.2.5/24') >>> interface.with_prefixlen '192.0.2.5/24' with_netmask ¶A string representation of the interface with the network as a net mask. >>> interface = IPv4Interface('192.0.2.5/24') >>> interface.with_netmask '192.0.2.5/255.255.255.0' with_hostmask ¶A string representation of the interface with the network as a host mask. >>> interface = IPv4Interface('192.0.2.5/24') >>> interface.with_hostmask '192.0.2.5/0.0.0.255'class ipaddress. IPv6Interface (address)¶Construct an IPv6 interface. The meaning of address is as in the constructor of
ip ¶ network ¶
with_prefixlen ¶ with_netmask ¶
with_hostmask ¶Refer to the corresponding attribute documentation in Operators¶Interface objects support some operators. Unless stated otherwise, operators can only be applied between compatible objects (i.e. IPv4 with IPv4, IPv6 with IPv6). Logical operators¶Interface objects can be compared with the usual set of logical operators. For equality comparison ( For ordering ( Other Module Level Functions¶The module also provides the following module level functions: ipaddress. v4_int_to_packed (address)¶Represent an address as 4 packed bytes in network (big-endian) order. address is an integer representation of an IPv4 IP address. A
>>> ipaddress.ip_address(3221225985) IPv4Address('192.0.2.1') >>> ipaddress.v4_int_to_packed(3221225985) b'\xc0\x00\x02\x01' ipaddress. v6_int_to_packed (address)¶Represent
an address as 16 packed bytes in network (big-endian) order. address is an integer representation of an IPv6 IP address. A ipaddress. summarize_address_range (first,
last)¶Return an iterator of the summarized network range given the first and last IP addresses. first is the first >>> [ipaddr for ipaddr in ipaddress.summarize_address_range( ... ipaddress.IPv4Address('192.0.2.0'), ... ipaddress.IPv4Address('192.0.2.130'))] [IPv4Network('192.0.2.0/25'), IPv4Network('192.0.2.128/31'), IPv4Network('192.0.2.130/32')] ipaddress. collapse_addresses (addresses)¶Return an iterator of the collapsed >>> [ipaddr for ipaddr in ... ipaddress.collapse_addresses([ipaddress.IPv4Network('192.0.2.0/25'), ... ipaddress.IPv4Network('192.0.2.128/25')])] [IPv4Network('192.0.2.0/24')] ipaddress. get_mixed_type_key (obj)¶Return a key suitable for sorting between networks and addresses. Address and Network objects are not sortable by default; they’re fundamentally different, so the expression: IPv4Address('192.0.2.0') <= IPv4Network('192.0.2.0/24') doesn’t make sense. There are some times however, where you may wish to have obj is either a network or address object. Custom Exceptions¶To support more specific error reporting from class constructors, the module defines the following exceptions: exceptionipaddress. AddressValueError (ValueError)¶Any value error related to the address. exceptionipaddress. NetmaskValueError (ValueError)¶Any value error related to the net mask. Which is a valid IP address quizlet?A valid IPv4 address consists of 4 8-bit (1 byte) numbers separated by periods. For example, 10.0. 0.65. Because they are 8 bits long, these numbers are frequently called octets.
Which of the following terms describes a computer or a software application that requests services from a server on the Internet group of answer choices?Used in home and corporate networks, a client is any computer hardware or software device that requests access to a service provided by a server. Clients are typically seen as the requesting program or user in a client-server architecture.
Which part of a computers IP address configuration indicates the portion of the address that specifies the network ID?IPv4 addresses are composed of two parts. The first numbers in the address specify the network, while the latter numbers specify the specific host. A subnet mask specifies which part of an address is the network part, and which part addresses the specific host.
Which of the following is a good practice to prevent your password from being vulnerable to attacks?7. Which of the following is a good practice to prevent your password from being vulnerable to attacks? Always use the same passwords across different sites so that you can easily remember them. Make sure to follow the links provided in emails to help you reset your passwords.
|