ipaddress
— IPv4/IPv6 manipulation libraryIPv4/IPv6操作库¶
Source code: Lib/ipaddress.py
ipaddress
provides the capabilities to create, manipulate and operate on IPv4 and IPv6 addresses and networks.提供了在IPv4和IPv6地址和网络上创建、操作和操作的功能。
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.该模块中的函数和类使处理与IP地址相关的各种任务变得简单明了,包括检查两个主机是否在同一子网上,在特定子网上的所有主机上迭代,检查字符串是否代表有效的IP地址或网络定义,等等。
This is the full module API reference—for an overview and introduction, see An introduction to the ipaddress module.这是完整的模块API参考资料,用于概述和介绍,请参阅ipaddress模块简介。
New in version 3.3.版本3.3中新增。
Convenience factory functions便利工厂功能¶
The ipaddress
module provides factory functions to conveniently create IP addresses, networks and interfaces:ipaddress
模块提供了工厂功能,可以方便地创建IP地址、网络和接口:
-
ipaddress.
ip_address
(address)¶ Return an根据作为参数传递的IP地址,返回一个IPv4Address
orIPv6Address
object depending on the IP address passed as argument.IPv4Address
或IPv6Address
对象。Either IPv4 or IPv6 addresses may be supplied; integers less than可以提供IPv4或IPv6地址;默认情况下,小于2**32
will be considered to be IPv4 by default.2**32
的整数将被视为IPv4。A如果address不表示有效的IPv4或IPv6地址,则会引发ValueError
is raised if address does not represent a valid IPv4 or IPv6 address.ValueError
。>>> 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根据作为参数传递的IP地址,返回一个IPv4Network
orIPv6Network
object depending on the IP address passed as argument.IPv4Network
或IPv6Network
对象。addressis a string or integer representing the IP network.是表示IP网络的字符串或整数。Either IPv4 or IPv6 networks may be supplied; integers less than可以提供IPv4或IPv6网络;默认情况下,小于2**32
will be considered to be IPv4 by default.2**32
的整数将被视为IPv4。strict is passed tostrict被传递给IPv4Network
orIPv6Network
constructor.IPv4Network
或IPv6Network
构造函数。A如果address不表示有效的IPv4或IPv6地址,或者网络设置了主机位,则会引发ValueError
is raised if address does not represent a valid IPv4 or IPv6 address, or if the network has host bits set.ValueError
。>>> ipaddress.ip_network('192.168.0.0/28')
IPv4Network('192.168.0.0/28')
-
ipaddress.
ip_interface
(address)¶ Return an根据作为参数传递的IP地址,返回一个IPv4Interface
orIPv6Interface
object depending on the IP address passed as argument.IPv4Interface
或IPv6Interface
对象。address is a string or integer representing the IP address.address是表示IP地址的字符串或整数。Either IPv4 or IPv6 addresses may be supplied; integers less than可以提供IPv4或IPv6地址;默认情况下,小于2**32
will be considered to be IPv4 by default.2**32
的整数将被视为IPv4。A如果地址不表示有效的IPv4或IPv6地址,则会引发ValueError
is raised if address does not represent a valid IPv4 or IPv6 address.ValueError
。
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. 这些便利功能的一个缺点是,需要同时处理IPv4和IPv6格式,这意味着错误消息提供的关于精确错误的信息很少,因为这些功能不知道是IPv4格式还是IPv6格式。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. IPv4Address
和IPv6Address
对象共享许多公共属性。Some attributes that are only meaningful for IPv6 addresses are also implemented by 一些只对IPv6地址有意义的属性也由IPv4Address
objects, in order to make it easier to write code that handles both IP versions correctly. IPv4Address
对象实现,以便更容易编写正确处理两个IP版本的代码。Address objects are hashable, so they can be used as keys in dictionaries.地址对象是可散列的,因此它们可以用作字典中的键。
-
class
ipaddress.
IPv4Address
(address)¶ Construct an IPv4 address.构造一个IPv4地址。An如果address不是有效的IPv4地址,则会引发AddressValueError
is raised if address is not a valid IPv4 address.AddressValueError
。The following constitutes a valid IPv4 address:以下内容构成有效的IPv4地址:A string in decimal-dot notation, consisting of four decimal integers in the inclusive range 0–255, separated by dots (e.g.十进制点表示法中的一种字符串,由四个十进制整数组成,包括0255,用点分隔(例如192.168.0.1
).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.一个可容纳32位的整数。An integer packed into a一个整数,被压缩到长度为4的bytes
object of length 4 (most significant octet first).bytes
对象中(最高有效的八位字节优先)。
>>> 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:版本3.8中更改:Leading zeros are tolerated, even in ambiguous cases that look like octal notation.前导零是可以容忍的,即使在看起来像八进制表示法的模糊情况下也是如此。Changed in version 3.10:版本3.10中更改:Leading zeros are no longer tolerated and are treated as an error.前导零不再被容忍,并被视为错误。IPv4 address strings are now parsed as strict as glibcIPv4地址字符串现在被解析为与glibcinet_pton()
.inet_pton()
一样严格。Changed in version 3.9.5:版本3.9.5中更改:The above change was also included in Python 3.9 starting with version 3.9.5.从3.9.5版本开始,Python 3.9中也包含了上述更改。Changed in version 3.8.12:版本3.8.12中更改:The above change was also included in Python 3.8 starting with version 3.8.12.从3.8.12版本开始,Python 3.8中也包含了上述更改。-
version
¶ The appropriate version number:适当的版本号:IPv4为4
for IPv4,6
for IPv6.4
,IPv6为6
。
-
max_prefixlen
¶ The total number of bits in the address representation for this version:此版本的地址表示中的总位数:IPv4为32
for IPv4,128
for IPv6.32
,IPv6为128
。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由于IPv4没有为八位字节设置为零的地址定义缩写符号,因此这两个属性始终与IPv4地址的str(addr)
for IPv4 addresses.str(addr)
相同。Exposing these attributes makes it easier to write display code that can handle both IPv4 and IPv6 addresses.公开这些属性可以更容易地编写既可以处理IPv4地址又可以处理IPv6地址的显示代码。
-
packed
¶ The binary representation of this address - a这个地址的二进制表示形式-一个适当长度的bytes
object of the appropriate length (most significant octet first).bytes
对象(最高有效的八位字节优先)。This is 4 bytes for IPv4 and 16 bytes for IPv6.IPv4为4个字节,IPv6为16个字节。
-
reverse_pointer
¶ The name of the reverse DNS PTR record for the IP address, e.g.:IP地址的反向DNS PTR记录的名称,例如:>>> 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.这是可用于执行PTR查找的名称,而不是解析的主机名本身。New in version 3.5.版本3.5中新增。
-
is_multicast
¶ 如果地址被保留用于多播使用,则为True
if the address is reserved for multicast use.True
。See RFC 3171 (for IPv4) or RFC 2373 (for IPv6).请参阅RFC 3171(针对IPv4)或RFC 2373(针对IPv6)。
-
is_private
¶ 如果地址是为专用网络分配的,则为True
if the address is allocated for private networks.True
。See iana-ipv4-special-registry (for IPv4) or iana-ipv6-special-registry (for IPv6).请参阅iana-ipv4-special-registry(适用于ipv4)或iana-ipv6-special-registry(适用于ipv6)。
-
is_global
¶ 如果地址是为公共网络分配的,则为True
if the address is allocated for public networks.True
。See iana-ipv4-special-registry (for IPv4) or iana-ipv6-special-registry (for IPv6).请参阅iana-ipv4-special-registry(适用于ipv4)或iana-ipv6-special-registery(适用于ipv6)。New in version 3.4.版本3.4中新增。
-
is_unspecified
¶ 如果未指定地址,则为True
if the address is unspecified.True
。See RFC 5735 (for IPv4) or RFC 2373 (for IPv6).请参阅RFC 5735(针对IPv4)或RFC 2373(针对IPv6)。
-
is_reserved
¶ 如果地址是IETF保留的,则为True
if the address is otherwise IETF reserved.True
。
-
IPv4Address.
__format__
(fmt)¶ Returns a string representation of the IP address, controlled by an explicit format string.返回由显式格式字符串控制的IP地址的字符串表示形式。fmt can be one of the following:fmt可以是以下选项之一:'s'
, the default option, equivalent tostr()
,'b'
for a zero-padded binary string,'X'
or'x'
for an uppercase or lowercase hexadecimal representation, or'n'
, which is equivalent to'b'
for IPv4 addresses and'x'
for IPv6.'s'
,默认选项,等效于str()
,'b'
表示零填充的二进制字符串,'X'
或'x'
表示大写或小写的十六进制表示,或“n”,等效于IPv4地址的“b”和IPv6的“X”。For binary and hexadecimal representations, the form specifier对于二进制和十六进制表示,可以使用形式说明符'#'
and the grouping option'_'
are available.'#'
和分组选项'_'
。__format__
is used byformat
,str.format
and f-strings.__format__
由format
、str.format
和f-string使用。>>> 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.版本3.9中新增。
-
class
ipaddress.
IPv6Address
(address)¶ Construct an IPv6 address.构造一个IPv6地址。An如果地址不是有效的IPv6地址,则会引发AddressValueError
is raised if address is not a valid IPv6 address.AddressValueError
。The following constitutes a valid IPv6 address:以下内容构成有效的IPv6地址:A string consisting of eight groups of four hexadecimal digits, each group representing 16 bits.由八组四个十六进制数字组成的字符串,每组代表16位。The groups are separated by colons.这些组用冒号分隔。This describes an exploded (longhand) notation.这描述了一种分解(非缩写)表示法。The string can also be compressed (shorthand notation) by various means.字符串也可以通过各种方式进行压缩(简写表示法)。See RFC 4291 for details.有关详细信息,请参阅RFC 4291。For example,例如,"0000:0000:0000:0000:0000:0abc:0007:0def"
can be compressed to"::abc:7:def"
."0000:0000:0000:0000:0000:0abc:0007:0def"
可以压缩为"::abc:7:def"
。Optionally, the string may also have a scope zone ID, expressed with a suffix可选地,该字符串还可以具有作用域区域ID,用后缀%scope_id
.%scope_id
表示。If present, the scope ID must be non-empty, and may not contain如果存在,则作用域ID必须非空,并且不能包含%
.%
。See RFC 4007 for details.有关详细信息,请参阅RFC 4007。For example,例如,fe80::1234%1
might identify addressfe80::1234
on the first link of the node.fe80::1234%1
可能会在节点的第一个链接上标识地址fe80::1234
。An integer that fits into 128 bits.一个可容纳128位的整数。An integer packed into a一个整数,压缩到一个长度为16的bytes
object of length 16, big-endian.bytes
对象中,big-endian。
>>> 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这也是str(addr)
for IPv6 addresses.str(addr)
为IPv6地址返回的值。-
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有关以下属性和方法,请参阅IPv4Address
class:IPv4Address
类的相应文档:-
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.版本3.4中新增。is_global
-
is_site_local
¶ 如果地址是为站点本地使用而保留的,则为True
if the address is reserved for site-local usage.True
。Note that the site-local address space has been deprecated by RFC 3879.请注意,站点本地地址空间已被RFC 3879否决。Use使用is_private
to test if this address is in the space of unique local addresses as defined by RFC 4193.is_private
测试此地址是否在RFC 4193定义的唯一本地地址空间中。
-
ipv4_mapped
¶ For addresses that appear to be IPv4 mapped addresses (starting with对于看起来是IPv4映射地址(以::FFFF/96
), this property will report the embedded IPv4 address.::FFFF/96
开头)的地址,此属性将报告嵌入的IPv4地址。For any other address, this property will be对于任何其他地址,此属性将为None
.None
。
-
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.对于RFC 4007定义的作用域地址,此属性将地址所属的地址作用域的特定区域标识为字符串。When no scope zone is specified, this property will be如果未指定作用域区域,则此属性将为None
.None
。
-
sixtofour
¶ For addresses that appear to be 6to4 addresses (starting with对于RFC 3056定义的6to4地址(从2002::/16
) as defined by RFC 3056, this property will report the embedded IPv4 address.2002::/16
开始),此属性将报告嵌入的IPv4地址。For any other address, this property will be对于任何其他地址,此属性将为None
.None
。
-
teredo
¶ For addresses that appear to be Teredo addresses (starting with对于如RFC 4380所定义的看起来是Teredo地址(从2001::/32
) as defined by RFC 4380, this property will report the embedded(server, client)
IP address pair.2001::/32
开始)的地址,,此属性将报告嵌入的(server, client)
IP地址对。For any other address, this property will be对于任何其他地址,此属性将为None
.None
。
-
IPv6Address.
__format__
(fmt)¶ Refer to the corresponding method documentation in请参阅IPv4Address
.IPv4Address
中的相应方法文档。New in version 3.9.版本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()
and int()
builtin functions:str()
和int()
内置函数处理的:
>>> 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.请注意,IPv6作用域地址被转换为没有作用域区域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).除非另有说明,否则运算符只能应用于兼容对象之间(即IPv4与IPv4、IPv6与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. 具有不同作用域区域ID的相同IPv6地址不相等。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 definitionsIP网络定义¶
The IPv4Network
and IPv6Network
objects provide a mechanism for defining and inspecting IP network definitions. IPv4Network
和IPv6Network
对象提供了一种用于定义和检查IP网络定义的机制。A network definition consists of a mask and a network address, and as such defines a range of IP addresses that equal the network address when masked (binary AND) with the mask. 网络定义由掩码和network address组成,因此定义了一系列IP地址,这些IP地址与掩码掩码掩码(二进制and)时的网络地址相等。For example, a network definition with the mask 例如,掩码为255.255.255.0
and the network address 192.168.1.0
consists of IP addresses in the inclusive range 192.168.1.0
to 192.168.1.255
.255.255.255.0
、网络地址为192.168.1.0
的网络定义由192.168.1.0
到192.168.1.255
范围内的IP地址组成。
Prefix, net mask and host mask前缀、网络掩码和主机掩码¶
There are several equivalent ways to specify IP network masks. 有几种等效的方法可以指定IP网络掩码。A prefix prefix /<nbits>
is a notation that denotes how many high-order bits are set in the network mask. /<nbits>
是一种表示在网络掩码中设置了多少高位的符号。A net mask is an IP address with some number of high-order bits set. 网络掩码是一个设置了一定数量的高阶位的IP地址。Thus the prefix 因此,前缀/24
is equivalent to the net mask 255.255.255.0
in IPv4, or ffff:ff00::
in IPv6. /24
相当于IPv4中的网络掩码255.255.255.0
,或IPv6中的ffff:ff00::
。In addition, a host mask is the logical inverse of a net mask, and is sometimes used (for example in Cisco access control lists) to denote a network mask. 此外,主机掩码是网络掩码的逻辑逆,有时(例如在Cisco访问控制列表中)用于表示网络掩码。The host mask equivalent to IPv4中等效于/24
in IPv4 is 0.0.0.255
./24
的主机掩码为0.0.0.255
。
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 所有这些在IPv4Network
and IPv6Network
, so to avoid duplication they are only documented for IPv4Network
. IPv4Network
和IPv6Network
之间都很常见,因此为了避免重复,它们只针对IPv4Network
进行了记录。Network objects are hashable, so they can be used as keys in dictionaries.网络对象是可散列的,因此它们可以用作字典中的关键字。
-
class
ipaddress.
IPv4Network
(address, strict=True)¶ Construct an IPv4 network definition.构造IPv4网络定义。address can be one of the following:address可以是以下地址之一:A string consisting of an IP address and an optional mask, separated by a slash (一个由IP地址和可选掩码组成的字符串,由斜线(/
)./
)分隔。The IP address is the network address, and the mask can be either a single number, which means it’s a prefix, or a string representation of an IPv4 address.IP地址是网络地址,掩码可以是单个数字,也就是说它是前缀,也可以是IPv4地址的字符串表示。If it’s the latter, the mask is interpreted as a net mask if it starts with a non-zero field, or as a host mask if it starts with a zero field, with the single exception of an all-zero mask which is treated as a net mask.如果是后者,则如果掩码以非零字段开始,则将其解释为网络掩码,如果掩码以零字段开始则将其理解为主机掩码,但被视为网络掩码的全零掩码除外。If no mask is provided, it’s considered to be如果没有提供口罩,则认为是/32
./32
。For example, the following address specifications are equivalent:例如,以下address规范等效:192.168.1.0/24
,192.168.1.0/255.255.255.0
and192.168.1.0/0.0.0.255
.192.168.1.0/24
、192.168.1.0/255.255.255.0
和192.168.1.0/0.0.0.255
。An integer that fits into 32 bits.一个可容纳32位的整数。This is equivalent to a single-address network, with the network address being address and the mask being这相当于单个地址网络,网络地址为address,掩码为/32
./32
。An integer packed into a一个整数,压缩到长度为4的bytes
object of length 4, big-endian.bytes
对象中,big-endian。The interpretation is similar to an integer address.这种解释类似于整数address。A two-tuple of an address description and a netmask, where the address description is either a string, a 32-bits integer, a 4-bytes packed integer, or an existing IPv4Address object; and the netmask is either an integer representing the prefix length (e.g.地址描述和网掩码的二元组,其中地址描述是字符串、32位整数、4字节压缩整数或现有的IPv4Address对象;并且网络掩码是表示前缀长度的整数(例如24
) or a string representing the prefix mask (e.g.255.255.255.0
).24
)或表示前缀掩码的字符串(例如255.255.255.0
)。
An如果address不是有效的IPv4地址,则会引发AddressValueError
is raised if address is not a valid IPv4 address.AddressValueError
。A如果掩码对IPv4地址无效,则会引发NetmaskValueError
is raised if the mask is not valid for an IPv4 address.NetmaskValueError
。If strict is如果strict为True
and host bits are set in the supplied address, thenValueError
is raised.True
,并且在提供的地址中设置了主机位,则会引发ValueError
。Otherwise, the host bits are masked out to determine the appropriate network address.否则,主机位被屏蔽掉以确定适当的网络地址。Unless stated otherwise, all network methods accepting other network/address objects will raise除非另有说明,否则如果参数的IP版本与TypeError
if the argument’s IP version is incompatible toself
.self
不兼容,则所有接受其他网络/地址对象的网络方法都将引发TypeError
。Changed in version 3.5:版本3.5中更改:Added the two-tuple form for the address constructor parameter.为address构造函数参数添加了两元组形式。-
version
¶
-
max_prefixlen
¶ Refer to the corresponding attribute documentation in请参阅IPv4Address
.IPv4Address
中相应的属性文档。
-
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主机掩码,作为IPv4Address
object.IPv4Address
对象。
-
netmask
¶ The net mask, as an作为IPv4Address
object.IPv4Address
对象的网络掩码。
-
with_prefixlen
¶
-
compressed
¶
-
exploded
¶ A string representation of the network, with the mask in prefix notation.网络的字符串表示形式,掩码以前缀表示。with_prefixlen
andcompressed
are always the same asstr(network)
.with_prefixlen
和compressed
始终与str(network)
相同。exploded
uses the exploded form the network address.使用网络地址的分解形式。
-
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.可用主机是属于网络的所有IP地址,除了网络地址本身和网络广播地址。For networks with a mask length of 31, the network address and network broadcast address are also included in the result.对于掩码长度为31的网络,网络地址和网络广播地址也包括在结果中。Networks with a mask of 32 will return a list containing the single host address.掩码为32的网络将返回一个包含单个主机地址的列表。>>> 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)¶ 如果此网络部分或全部包含在other网络中,或者other网络完全包含在该网络中,则为True
if this network is partly or wholly contained in other or other is wholly contained in this network.True
。
-
address_exclude
(network)¶ Computes the network definitions resulting from removing the given network from this one.计算从此network中删除给定网络所产生的网络定义。Returns an iterator of network objects.返回网络对象的迭代器。Raises如果network未完全包含在此网络中,则引发ValueError
if network is not completely contained in this network.ValueError
。>>> 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.prefixlen_diff是前缀长度应该增加的数量。new_prefix is the desired new prefix of the subnets; it must be larger than our prefix.new_prefix是子网所需的新前缀;它必须大于前缀。One and only one of prefixlen_diff and new_prefix must be set.必须设置prefixlen_diff和new_prefix中的一个,并且只能设置其中一个。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.prefixlen_diff是前缀长度应该减少的数量。new_prefix is the desired new prefix of the supernet; it must be smaller than our prefix.new_prefix是超网的期望的新前缀;它必须比前缀小。One and only one of prefixlen_diff and new_prefix must be set.必须设置prefixlen_diff和new_prefix中的一个,并且只能设置其中一个。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如果此网络是other网络的子网,则返回True
if this network is a subnet of other.True
。>>> a = ip_network('192.168.1.0/24')
>>> b = ip_network('192.168.1.128/30')
>>> b.subnet_of(a)
TrueNew in version 3.7.版本3.7中新增。
-
supernet_of
(other)¶ Return如果此网络是other网络的超级网络,则返回True
if this network is a supernet of other.True
。>>> a = ip_network('192.168.1.0/24')
>>> b = ip_network('192.168.1.128/30')
>>> a.supernet_of(b)
TrueNew in version 3.7.版本3.7中新增。
-
compare_networks
(other)¶ Compare this network to other.将此网络与other网络进行比较。In this comparison only the network addresses are considered; host bits aren’t.在这种比较中,只考虑网络地址;主机位不是。Returns either返回-1
,0
or1
.-1
、0
或1
。>>> 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'))
0Deprecated since version 3.7:
It uses the same ordering and comparison algorithm as “<”, “==”, and “>”它使用与“<”、“==”和“>”相同的排序和比较算法
-
class
ipaddress.
IPv6Network
(address, strict=True)¶ Construct an IPv6 network definition.构建IPv6网络定义。address can be one of the following:address可以是以下地址之一:A string consisting of an IP address and an optional prefix length, separated by a slash (一个由IP地址和可选前缀长度组成的字符串,由斜线(/
)./
)分隔。The IP address is the network address, and the prefix length must be a single number, the prefix.IP地址是网络地址,前缀长度必须是单个数字,即前缀。If no prefix length is provided, it’s considered to be如果没有提供前缀长度,则认为它是/128
./128
。Note that currently expanded netmasks are not supported.请注意,不支持当前扩展的网络掩码。That means这意味着2001:db00::0/24
is a valid argument while2001:db00::0/ffff:ff00::
is not.2001:db00::0/24
是一个有效的参数,而2001:db00::0/ffff:ff00::
是。An integer that fits into 128 bits. This is equivalent to a single-address network, with the network address being address and the mask being一个可容纳128位的整数。这相当于单个地址网络,网络地址为address,掩码为/128
./128
。An integer packed into a一个整数,压缩到一个长度为16的bytes
object of length 16, big-endian.bytes
对象中,big-endian。The interpretation is similar to an integer address.这种解释类似于整数address。A two-tuple of an address description and a netmask, where the address description is either a string, a 128-bits integer, a 16-bytes packed integer, or an existing IPv6Address object; and the netmask is an integer representing the prefix length.地址描述和网掩码的二元组,其中地址描述是字符串、128位整数、16字节压缩整数或现有的IPv6Address对象;并且网络掩码是表示前缀长度的整数。
An如果address不是有效的IPv6地址,则会引发AddressValueError
is raised if address is not a valid IPv6 address.AddressValueError
。A如果掩码对IPv6地址无效,则会引发NetmaskValueError
is raised if the mask is not valid for an IPv6 address.NetmaskValueError
。If strict is如果strict为True
and host bits are set in the supplied address, thenValueError
is raised.True
,并且在提供的地址中设置了主机位,则会引发ValueError
。Otherwise, the host bits are masked out to determine the appropriate network address.否则,主机位被屏蔽掉以确定适当的网络地址。Changed in version 3.5:版本3.5中更改:Added the two-tuple form for the address constructor parameter.为address构造函数参数添加了两元组形式。-
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.可用主机是属于网络的所有IP地址,但子网路由器选播地址除外。For networks with a mask length of 127, the Subnet-Router anycast address is also included in the result.对于掩码长度为127的网络,子网路由器选播地址也包含在结果中。Networks with a mask of 128 will return a list containing the single host address.掩码为128的网络将返回一个包含单个主机地址的列表。
-
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请参阅IPv4Network
.IPv4Network
中相应的属性文档。
-
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).除非另有说明,否则运算符只能应用于兼容对象之间(即IPv4与IPv4、IPv6与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 对于迭代,将返回所有主机,包括不可用的主机(对于可用的主机,请使用hosts()
method). hosts()
方法)。An example:例如:
>>> 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.接口对象是可散列的,因此它们可以用作字典中的键。
-
class
ipaddress.
IPv4Interface
(address)¶ Construct an IPv4 interface.构造IPv4接口。The meaning of address is as in the constructor ofaddress的含义与IPv4Network
, except that arbitrary host addresses are always accepted.IPv4Network
的构造函数相同,只是总是接受任意主机地址。IPv4Interface
is a subclass of是IPv4Address
, so it inherits all the attributes from that class.IPv4Address
的子类,因此它继承了该类的所有属性。In addition, the following attributes are available:此外,还提供了以下属性:-
ip
¶ The address (没有网络信息的地址(IPv4Address
) without network information.IPv4Address
)。>>> interface = IPv4Interface('192.0.2.5/24')
>>> interface.ip
IPv4Address('192.0.2.5')
-
network
¶ The network (此接口所属的网络(IPv4Network
) this interface belongs to.IPv4Network
)。>>> 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.构建一个IPv6接口。The meaning of address is as in the constructor ofaddress的含义与IPv6Network
, except that arbitrary host addresses are always accepted.IPv6Network
的构造函数相同,只是总是接受任意主机地址。IPv6Interface
is a subclass ofIPv6Address
, so it inherits all the attributes from that class.IPv6Interface
是IPv6Address
的一个子类,因此它继承了该类的所有属性。In addition, the following attributes are available:此外,还提供了以下属性:-
ip
¶
-
network
¶
-
with_prefixlen
¶
-
with_netmask
¶
-
with_hostmask
¶ Refer to the corresponding attribute documentation in请参阅IPv4Interface
.IPv4Interface
中相应的属性文档。
-
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).除非另有说明,否则运算符只能应用于兼容对象之间(即IPv4与IPv4、IPv6与IPv6)。
Logical operators逻辑运算符¶
Interface objects can be compared with the usual set of logical operators.接口对象可以与通常的一组逻辑运算符进行比较。
For equality comparison (对于相等比较(==
and !=
), both the IP address and network must be the same for the objects to be equal. ==
和!=
),IP地址和网络必须相同,对象才能相等。An interface will not compare equal to any address or network object.接口不会与任何地址或网络对象进行比较。
For ordering (对于排序(<
, >
, etc) the rules are different. <
、>
等),规则是不同的。Interface and address objects with the same IP version can be compared, and the address objects will always sort before the interface objects. 可以比较具有相同IP版本的接口和地址对象,并且地址对象总是在接口对象之前排序。Two interface objects are first compared by their networks and, if those are the same, then by their IP addresses.两个接口对象首先通过它们的网络进行比较,如果它们相同,则通过它们的IP地址进行比较。
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.将地址表示为网络(big-endian)顺序中的4个压缩字节。addressis an integer representation of an IPv4 IP address.是IPv4 IP地址的整数表示。A如果整数为负数或太大而不能作为IPv4 IP地址,则会引发ValueError
is raised if the integer is negative or too large to be an IPv4 IP address.ValueError
。>>> 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.将地址表示为网络(big-endian)顺序中的16个压缩字节。address is an integer representation of an IPv6 IP address.address是IPv6 IP地址的整数表示。A如果整数为负数或太大而不能作为IPv6 IP地址,则会引发ValueError
is raised if the integer is negative or too large to be an IPv6 IP address.ValueError
。
-
ipaddress.
summarize_address_range
(first, last)¶ Return an iterator of the summarized network range given the first and last IP addresses.返回给定第一个和最后一个IP地址的汇总网络范围的迭代器。first is the firstfirst是该范围中的第一个IPv4Address
orIPv6Address
in the range and last is the lastIPv4Address
orIPv6Address
in the range.IPv4Address
或IPv6Address
,last是该区域中的最后一个IPv4A地址或IPv6地址。A如果first或last不是IP地址或不是同一版本,则会引发TypeError
is raised if first or last are not IP addresses or are not of the same version.TypeError
。A如果last不大于first,或者如果first地址版本不是4或6,则会引发ValueError
is raised if last is not greater than first or if first address version is not 4 or 6.ValueError
。>>> [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返回折叠的IPv4Network
orIPv6Network
objects.IPv4Network
或IPv6Network
对象的迭代器。addresses is an iterator ofaddresses是IPv4Network
orIPv6Network
objects.IPv4Network
或IPv6Network
对象的迭代器。A如果addresses包含混合版本的对象,则会引发TypeError
is raised if addresses contains mixed version objects.TypeError
。>>> [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然而,有些时候,你可能希望让ipaddress
sort these anyway.ipaddress
对这些东西进行排序。If you need to do this, you can use this function as the key argument to如果需要这样做,可以使用此函数作为sorted()
.sorted()
的key参数。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:为了支持类构造函数中更具体的错误报告,该模块定义了以下异常:
-
exception
ipaddress.
AddressValueError
(ValueError)¶ Any value error related to the address.任何与地址相关的值错误。
-
exception
ipaddress.
NetmaskValueError
(ValueError)¶ Any value error related to the net mask.任何与网络掩码相关的值错误。