blob: 75c9107bd5eb311bf1405a41345b1e5632ee61c5 [file] [log] [blame]
Nick Coghlan9680bdb2012-06-17 17:24:10 +10001:mod:`ipaddress` --- IPv4/IPv6 manipulation library
2===================================================
3
4.. module:: ipaddress
5 :synopsis: IPv4/IPv6 manipulation library.
Terry Jan Reedyfa089b92016-06-11 15:02:54 -04006
Nick Coghlan9680bdb2012-06-17 17:24:10 +10007.. moduleauthor:: Peter Moody
8
9**Source code:** :source:`Lib/ipaddress.py`
10
11--------------
12
Nick Coghlan730f67f2012-08-05 22:02:18 +100013:mod:`ipaddress` provides the capabilities to create, manipulate and
14operate on IPv4 and IPv6 addresses and networks.
Nick Coghlan9680bdb2012-06-17 17:24:10 +100015
16The functions and classes in this module make it straightforward to handle
17various tasks related to IP addresses, including checking whether or not two
18hosts are on the same subnet, iterating over all hosts in a particular
Nick Coghlan730f67f2012-08-05 22:02:18 +100019subnet, checking whether or not a string represents a valid IP address or
20network definition, and so on.
21
Benjamin Peterson5feeeba2014-12-28 22:14:15 -060022This is the full module API reference—for an overview and introduction, see
23:ref:`ipaddress-howto`.
Nick Coghlan730f67f2012-08-05 22:02:18 +100024
25.. versionadded:: 3.3
Nick Coghlan9680bdb2012-06-17 17:24:10 +100026
Berker Peksag6bbc8392016-08-04 17:21:46 +030027.. testsetup::
Marco Buttue65fcde2017-04-27 14:23:34 +020028
29 import ipaddress
30 from ipaddress import (
31 ip_network, IPv4Address, IPv4Interface, IPv4Network,
32 )
Nick Coghlan9680bdb2012-06-17 17:24:10 +100033
Eli Bendersky0e497492012-07-31 17:23:11 +030034Convenience factory functions
35-----------------------------
Nick Coghlan9680bdb2012-06-17 17:24:10 +100036
Eli Bendersky0e497492012-07-31 17:23:11 +030037The :mod:`ipaddress` module provides factory functions to conveniently create
38IP addresses, networks and interfaces:
Nick Coghlan9680bdb2012-06-17 17:24:10 +100039
40.. function:: ip_address(address)
41
42 Return an :class:`IPv4Address` or :class:`IPv6Address` object depending on
Eli Bendersky0e497492012-07-31 17:23:11 +030043 the IP address passed as argument. Either IPv4 or IPv6 addresses may be
44 supplied; integers less than 2**32 will be considered to be IPv4 by default.
Eli Bendersky948af232012-10-07 07:23:50 -070045 A :exc:`ValueError` is raised if *address* does not represent a valid IPv4
46 or IPv6 address.
47
Nick Coghlan9680bdb2012-06-17 17:24:10 +100048 >>> ipaddress.ip_address('192.168.0.1')
49 IPv4Address('192.168.0.1')
50 >>> ipaddress.ip_address('2001:db8::')
51 IPv6Address('2001:db8::')
52
53
54.. function:: ip_network(address, strict=True)
55
56 Return an :class:`IPv4Network` or :class:`IPv6Network` object depending on
57 the IP address passed as argument. *address* is a string or integer
58 representing the IP network. Either IPv4 or IPv6 networks may be supplied;
59 integers less than 2**32 will be considered to be IPv4 by default. *strict*
60 is passed to :class:`IPv4Network` or :class:`IPv6Network` constructor. A
Eli Bendersky0e497492012-07-31 17:23:11 +030061 :exc:`ValueError` is raised if *address* does not represent a valid IPv4 or
62 IPv6 address, or if the network has host bits set.
Nick Coghlan9680bdb2012-06-17 17:24:10 +100063
64 >>> ipaddress.ip_network('192.168.0.0/28')
65 IPv4Network('192.168.0.0/28')
66
67
68.. function:: ip_interface(address)
69
70 Return an :class:`IPv4Interface` or :class:`IPv6Interface` object depending
71 on the IP address passed as argument. *address* is a string or integer
72 representing the IP address. Either IPv4 or IPv6 addresses may be supplied;
Eli Bendersky0e497492012-07-31 17:23:11 +030073 integers less than 2**32 will be considered to be IPv4 by default. A
74 :exc:`ValueError` is raised if *address* does not represent a valid IPv4 or
Nick Coghlan9680bdb2012-06-17 17:24:10 +100075 IPv6 address.
76
Nick Coghlan730f67f2012-08-05 22:02:18 +100077One downside of these convenience functions is that the need to handle both
78IPv4 and IPv6 formats means that error messages provide minimal
79information on the precise error, as the functions don't know whether the
80IPv4 or IPv6 format was intended. More detailed error reporting can be
81obtained by calling the appropriate version specific class constructors
82directly.
83
84
85IP Addresses
86------------
Nick Coghlan9680bdb2012-06-17 17:24:10 +100087
Eli Bendersky0e497492012-07-31 17:23:11 +030088Address objects
Nick Coghlan730f67f2012-08-05 22:02:18 +100089^^^^^^^^^^^^^^^
Nick Coghlan9680bdb2012-06-17 17:24:10 +100090
Eli Bendersky0e497492012-07-31 17:23:11 +030091The :class:`IPv4Address` and :class:`IPv6Address` objects share a lot of common
92attributes. Some attributes that are only meaningful for IPv6 addresses are
93also implemented by :class:`IPv4Address` objects, in order to make it easier to
Nick Coghlan730f67f2012-08-05 22:02:18 +100094write code that handles both IP versions correctly.
Nick Coghlan9680bdb2012-06-17 17:24:10 +100095
96.. class:: IPv4Address(address)
97
Eli Bendersky0e497492012-07-31 17:23:11 +030098 Construct an IPv4 address. An :exc:`AddressValueError` is raised if
99 *address* is not a valid IPv4 address.
100
101 The following constitutes a valid IPv4 address:
102
103 1. A string in decimal-dot notation, consisting of four decimal integers in
Serhiy Storchakac7b1a0b2016-11-26 13:43:28 +0200104 the inclusive range 0--255, separated by dots (e.g. ``192.168.0.1``). Each
Nick Coghlan730f67f2012-08-05 22:02:18 +1000105 integer represents an octet (byte) in the address. Leading zeroes are
Donald Stufft8b852f12014-05-20 12:58:38 -0400106 tolerated only for values less than 8 (as there is no ambiguity
Nick Coghlan730f67f2012-08-05 22:02:18 +1000107 between the decimal and octal interpretations of such strings).
Eli Bendersky0e497492012-07-31 17:23:11 +0300108 2. An integer that fits into 32 bits.
Nick Coghlan730f67f2012-08-05 22:02:18 +1000109 3. An integer packed into a :class:`bytes` object of length 4 (most
110 significant octet first).
Nick Coghlan9680bdb2012-06-17 17:24:10 +1000111
112 >>> ipaddress.IPv4Address('192.168.0.1')
113 IPv4Address('192.168.0.1')
Eli Bendersky948af232012-10-07 07:23:50 -0700114 >>> ipaddress.IPv4Address(3232235521)
Nick Coghlan730f67f2012-08-05 22:02:18 +1000115 IPv4Address('192.168.0.1')
116 >>> ipaddress.IPv4Address(b'\xC0\xA8\x00\x01')
117 IPv4Address('192.168.0.1')
Eli Bendersky0e497492012-07-31 17:23:11 +0300118
119 .. attribute:: version
120
Nick Coghlan730f67f2012-08-05 22:02:18 +1000121 The appropriate version number: ``4`` for IPv4, ``6`` for IPv6.
Eli Bendersky0e497492012-07-31 17:23:11 +0300122
123 .. attribute:: max_prefixlen
124
Nick Coghlan730f67f2012-08-05 22:02:18 +1000125 The total number of bits in the address representation for this
126 version: ``32`` for IPv4, ``128`` for IPv6.
127
128 The prefix defines the number of leading bits in an address that
129 are compared to determine whether or not an address is part of a
130 network.
131
132 .. attribute:: compressed
133 .. attribute:: exploded
134
135 The string representation in dotted decimal notation. Leading zeroes
136 are never included in the representation.
137
138 As IPv4 does not define a shorthand notation for addresses with octets
139 set to zero, these two attributes are always the same as ``str(addr)``
140 for IPv4 addresses. Exposing these attributes makes it easier to
141 write display code that can handle both IPv4 and IPv6 addresses.
142
143 .. attribute:: packed
144
145 The binary representation of this address - a :class:`bytes` object of
146 the appropriate length (most significant octet first). This is 4 bytes
147 for IPv4 and 16 bytes for IPv6.
Eli Bendersky0e497492012-07-31 17:23:11 +0300148
Eric V. Smithebdaaf42014-04-14 12:58:07 -0400149 .. attribute:: reverse_pointer
150
151 The name of the reverse DNS PTR record for the IP address, e.g.::
152
153 >>> ipaddress.ip_address("127.0.0.1").reverse_pointer
154 '1.0.0.127.in-addr.arpa'
155 >>> ipaddress.ip_address("2001:db8::1").reverse_pointer
156 '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'
157
158 This is the name that could be used for performing a PTR lookup, not the
159 resolved hostname itself.
160
Berker Peksag85b60902016-08-04 17:25:40 +0300161 .. versionadded:: 3.5
Eric V. Smithebdaaf42014-04-14 12:58:07 -0400162
Eli Bendersky0e497492012-07-31 17:23:11 +0300163 .. attribute:: is_multicast
164
Nick Coghlan730f67f2012-08-05 22:02:18 +1000165 ``True`` if the address is reserved for multicast use. See
166 :RFC:`3171` (for IPv4) or :RFC:`2373` (for IPv6).
Eli Bendersky0e497492012-07-31 17:23:11 +0300167
Peter Moodye5019d52013-10-24 09:47:10 -0700168 .. attribute:: is_private
169
170 ``True`` if the address is allocated for private networks. See
R David Murray6674ac02014-03-06 11:51:37 -0500171 iana-ipv4-special-registry_ (for IPv4) or iana-ipv6-special-registry_
Peter Moodye5019d52013-10-24 09:47:10 -0700172 (for IPv6).
173
Peter Moody8ed30c12013-10-21 16:16:51 -0700174 .. attribute:: is_global
Eli Bendersky0e497492012-07-31 17:23:11 +0300175
Peter Moodybe9c1b12013-10-22 12:36:21 -0700176 ``True`` if the address is allocated for public networks. See
R David Murray6674ac02014-03-06 11:51:37 -0500177 iana-ipv4-special-registry_ (for IPv4) or iana-ipv6-special-registry_
Peter Moody8ed30c12013-10-21 16:16:51 -0700178 (for IPv6).
179
R David Murray6674ac02014-03-06 11:51:37 -0500180 .. versionadded:: 3.4
Eli Bendersky0e497492012-07-31 17:23:11 +0300181
182 .. attribute:: is_unspecified
183
Andrew Kuchlinge5235f12014-02-15 17:11:06 -0500184 ``True`` if the address is unspecified. See :RFC:`5735` (for IPv4)
Nick Coghlan730f67f2012-08-05 22:02:18 +1000185 or :RFC:`2373` (for IPv6).
Eli Bendersky0e497492012-07-31 17:23:11 +0300186
187 .. attribute:: is_reserved
188
Nick Coghlan730f67f2012-08-05 22:02:18 +1000189 ``True`` if the address is otherwise IETF reserved.
Eli Bendersky0e497492012-07-31 17:23:11 +0300190
191 .. attribute:: is_loopback
192
Nick Coghlan730f67f2012-08-05 22:02:18 +1000193 ``True`` if this is a loopback address. See :RFC:`3330` (for IPv4)
194 or :RFC:`2373` (for IPv6).
Eli Bendersky0e497492012-07-31 17:23:11 +0300195
196 .. attribute:: is_link_local
197
Nick Coghlan730f67f2012-08-05 22:02:18 +1000198 ``True`` if the address is reserved for link-local usage. See
199 :RFC:`3927`.
200
Serhiy Storchaka6dff0202016-05-07 10:49:07 +0300201.. _iana-ipv4-special-registry: https://www.iana.org/assignments/iana-ipv4-special-registry/iana-ipv4-special-registry.xhtml
202.. _iana-ipv6-special-registry: https://www.iana.org/assignments/iana-ipv6-special-registry/iana-ipv6-special-registry.xhtml
R David Murray6674ac02014-03-06 11:51:37 -0500203
Eli Bendersky0e497492012-07-31 17:23:11 +0300204
205.. class:: IPv6Address(address)
206
207 Construct an IPv6 address. An :exc:`AddressValueError` is raised if
208 *address* is not a valid IPv6 address.
209
210 The following constitutes a valid IPv6 address:
211
212 1. A string consisting of eight groups of four hexadecimal digits, each
213 group representing 16 bits. The groups are separated by colons.
214 This describes an *exploded* (longhand) notation. The string can
215 also be *compressed* (shorthand notation) by various means. See
216 :RFC:`4291` for details. For example,
217 ``"0000:0000:0000:0000:0000:0abc:0007:0def"`` can be compressed to
218 ``"::abc:7:def"``.
219 2. An integer that fits into 128 bits.
220 3. An integer packed into a :class:`bytes` object of length 16, big-endian.
221
222 >>> ipaddress.IPv6Address('2001:db8::1000')
223 IPv6Address('2001:db8::1000')
224
Nick Coghlan730f67f2012-08-05 22:02:18 +1000225 .. attribute:: compressed
226
227 The short form of the address representation, with leading zeroes in
228 groups omitted and the longest sequence of groups consisting entirely of
229 zeroes collapsed to a single empty group.
230
231 This is also the value returned by ``str(addr)`` for IPv6 addresses.
232
233 .. attribute:: exploded
234
235 The long form of the address representation, with all leading zeroes and
236 groups consisting entirely of zeroes included.
237
R David Murray6674ac02014-03-06 11:51:37 -0500238
239 For the following attributes, see the corresponding documention of the
240 :class:`IPv4Address` class:
241
Nick Coghlan730f67f2012-08-05 22:02:18 +1000242 .. attribute:: packed
Eric V. Smithebdaaf42014-04-14 12:58:07 -0400243 .. attribute:: reverse_pointer
Nick Coghlan730f67f2012-08-05 22:02:18 +1000244 .. attribute:: version
245 .. attribute:: max_prefixlen
246 .. attribute:: is_multicast
247 .. attribute:: is_private
R David Murray6674ac02014-03-06 11:51:37 -0500248 .. attribute:: is_global
Nick Coghlan730f67f2012-08-05 22:02:18 +1000249 .. attribute:: is_unspecified
250 .. attribute:: is_reserved
251 .. attribute:: is_loopback
252 .. attribute:: is_link_local
253
R David Murray6674ac02014-03-06 11:51:37 -0500254 .. versionadded:: 3.4
255 is_global
Eli Bendersky0e497492012-07-31 17:23:11 +0300256
257 .. attribute:: is_site_local
258
Nick Coghlan730f67f2012-08-05 22:02:18 +1000259 ``True`` if the address is reserved for site-local usage. Note that
260 the site-local address space has been deprecated by :RFC:`3879`. Use
261 :attr:`~IPv4Address.is_private` to test if this address is in the
262 space of unique local addresses as defined by :RFC:`4193`.
Eli Bendersky0e497492012-07-31 17:23:11 +0300263
264 .. attribute:: ipv4_mapped
265
Nick Coghlan730f67f2012-08-05 22:02:18 +1000266 For addresses that appear to be IPv4 mapped addresses (starting with
267 ``::FFFF/96``), this property will report the embedded IPv4 address.
268 For any other address, this property will be ``None``.
Eli Bendersky0e497492012-07-31 17:23:11 +0300269
270 .. attribute:: sixtofour
271
Nick Coghlan730f67f2012-08-05 22:02:18 +1000272 For addresses that appear to be 6to4 addresses (starting with
273 ``2002::/16``) as defined by :RFC:`3056`, this property will report
274 the embedded IPv4 address. For any other address, this property will
275 be ``None``.
276
277 .. attribute:: teredo
278
279 For addresses that appear to be Teredo addresses (starting with
280 ``2001::/32``) as defined by :RFC:`4380`, this property will report
281 the embedded ``(server, client)`` IP address pair. For any other
282 address, this property will be ``None``.
283
284
285Conversion to Strings and Integers
286^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
287
288To interoperate with networking interfaces such as the socket module,
289addresses must be converted to strings or integers. This is handled using
290the :func:`str` and :func:`int` builtin functions::
291
292 >>> str(ipaddress.IPv4Address('192.168.0.1'))
293 '192.168.0.1'
294 >>> int(ipaddress.IPv4Address('192.168.0.1'))
295 3232235521
296 >>> str(ipaddress.IPv6Address('::1'))
297 '::1'
298 >>> int(ipaddress.IPv6Address('::1'))
299 1
Eli Bendersky0e497492012-07-31 17:23:11 +0300300
301
302Operators
303^^^^^^^^^
304
305Address objects support some operators. Unless stated otherwise, operators can
306only be applied between compatible objects (i.e. IPv4 with IPv4, IPv6 with
307IPv6).
308
Nick Coghlan730f67f2012-08-05 22:02:18 +1000309
Georg Brandl9ad417e2013-10-06 19:23:57 +0200310Comparison operators
311""""""""""""""""""""
Eli Bendersky0e497492012-07-31 17:23:11 +0300312
Georg Brandl9ad417e2013-10-06 19:23:57 +0200313Address objects can be compared with the usual set of comparison operators. Some
Eli Bendersky0e497492012-07-31 17:23:11 +0300314examples::
315
316 >>> IPv4Address('127.0.0.2') > IPv4Address('127.0.0.1')
317 True
318 >>> IPv4Address('127.0.0.2') == IPv4Address('127.0.0.1')
319 False
320 >>> IPv4Address('127.0.0.2') != IPv4Address('127.0.0.1')
321 True
322
Nick Coghlan730f67f2012-08-05 22:02:18 +1000323
Eli Bendersky0e497492012-07-31 17:23:11 +0300324Arithmetic operators
325""""""""""""""""""""
326
327Integers can be added to or subtracted from address objects. Some examples::
328
329 >>> IPv4Address('127.0.0.2') + 3
330 IPv4Address('127.0.0.5')
331 >>> IPv4Address('127.0.0.2') - 3
332 IPv4Address('126.255.255.255')
333 >>> IPv4Address('255.255.255.255') + 1
334 Traceback (most recent call last):
335 File "<stdin>", line 1, in <module>
336 ipaddress.AddressValueError: 4294967296 (>= 2**32) is not permitted as an IPv4 address
337
338
Nick Coghlan730f67f2012-08-05 22:02:18 +1000339IP Network definitions
340----------------------
341
342The :class:`IPv4Network` and :class:`IPv6Network` objects provide a mechanism
343for defining and inspecting IP network definitions. A network definition
344consists of a *mask* and a *network address*, and as such defines a range of
345IP addresses that equal the network address when masked (binary AND) with the
346mask. For example, a network definition with the mask ``255.255.255.0`` and
347the network address ``192.168.1.0`` consists of IP addresses in the inclusive
348range ``192.168.1.0`` to ``192.168.1.255``.
349
350
351Prefix, net mask and host mask
352^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
353
354There are several equivalent ways to specify IP network masks. A *prefix*
355``/<nbits>`` is a notation that denotes how many high-order bits are set in
356the network mask. A *net mask* is an IP address with some number of
357high-order bits set. Thus the prefix ``/24`` is equivalent to the net mask
358``255.255.255.0`` in IPv4, or ``ffff:ff00::`` in IPv6. In addition, a
359*host mask* is the logical inverse of a *net mask*, and is sometimes used
360(for example in Cisco access control lists) to denote a network mask. The
361host mask equivalent to ``/24`` in IPv4 is ``0.0.0.255``.
362
363
Eli Bendersky0e497492012-07-31 17:23:11 +0300364Network objects
Nick Coghlan730f67f2012-08-05 22:02:18 +1000365^^^^^^^^^^^^^^^
366
367All attributes implemented by address objects are implemented by network
368objects as well. In addition, network objects implement additional attributes.
369All of these are common between :class:`IPv4Network` and :class:`IPv6Network`,
370so to avoid duplication they are only documented for :class:`IPv4Network`.
Nick Coghlan9680bdb2012-06-17 17:24:10 +1000371
372.. class:: IPv4Network(address, strict=True)
373
Nick Coghlan730f67f2012-08-05 22:02:18 +1000374 Construct an IPv4 network definition. *address* can be one of the following:
375
376 1. A string consisting of an IP address and an optional mask, separated by
377 a slash (``/``). The IP address is the network address, and the mask
378 can be either a single number, which means it's a *prefix*, or a string
379 representation of an IPv4 address. If it's the latter, the mask is
380 interpreted as a *net mask* if it starts with a non-zero field, or as
381 a *host mask* if it starts with a zero field. If no mask is provided,
382 it's considered to be ``/32``.
383
384 For example, the following *address* specifications are equivalent:
385 ``192.168.1.0/24``, ``192.168.1.0/255.255.255.0`` and
386 ``192.168.1.0/0.0.0.255``.
387
388 2. An integer that fits into 32 bits. This is equivalent to a
389 single-address network, with the network address being *address* and
390 the mask being ``/32``.
391
392 3. An integer packed into a :class:`bytes` object of length 4, big-endian.
393 The interpretation is similar to an integer *address*.
394
Antoine Pitrou5fb195f2014-05-12 20:36:46 +0200395 4. A two-tuple of an address description and a netmask, where the address
396 description is either a string, a 32-bits integer, a 4-bytes packed
397 integer, or an existing IPv4Address object; and the netmask is either
398 an integer representing the prefix length (e.g. ``24``) or a string
399 representing the prefix mask (e.g. ``255.255.255.0``).
400
Nick Coghlan730f67f2012-08-05 22:02:18 +1000401 An :exc:`AddressValueError` is raised if *address* is not a valid IPv4
402 address. A :exc:`NetmaskValueError` is raised if the mask is not valid for
403 an IPv4 address.
Nick Coghlan9680bdb2012-06-17 17:24:10 +1000404
405 If *strict* is ``True`` and host bits are set in the supplied address,
Nick Coghlan730f67f2012-08-05 22:02:18 +1000406 then :exc:`ValueError` is raised. Otherwise, the host bits are masked out
Nick Coghlan9680bdb2012-06-17 17:24:10 +1000407 to determine the appropriate network address.
408
Nick Coghlan7362c3e2012-08-05 22:32:37 +1000409 Unless stated otherwise, all network methods accepting other network/address
410 objects will raise :exc:`TypeError` if the argument's IP version is
411 incompatible to ``self``
412
Antoine Pitrou5fb195f2014-05-12 20:36:46 +0200413 .. versionchanged:: 3.5
414
415 Added the two-tuple form for the *address* constructor parameter.
416
Nick Coghlan7362c3e2012-08-05 22:32:37 +1000417 .. attribute:: version
418 .. attribute:: max_prefixlen
419
420 Refer to the corresponding attribute documentation in
421 :class:`IPv4Address`
422
423 .. attribute:: is_multicast
424 .. attribute:: is_private
425 .. attribute:: is_unspecified
426 .. attribute:: is_reserved
427 .. attribute:: is_loopback
428 .. attribute:: is_link_local
429
430 These attributes are true for the network as a whole if they are true
Terry Jan Reedy0f847642013-03-11 18:34:00 -0400431 for both the network address and the broadcast address
Nick Coghlan7362c3e2012-08-05 22:32:37 +1000432
433 .. attribute:: network_address
434
Nick Coghlan31096a92012-08-05 22:52:38 +1000435 The network address for the network. The network address and the
436 prefix length together uniquely define a network.
Nick Coghlan730f67f2012-08-05 22:02:18 +1000437
438 .. attribute:: broadcast_address
439
Nick Coghlan31096a92012-08-05 22:52:38 +1000440 The broadcast address for the network. Packets sent to the broadcast
441 address should be received by every host on the network.
Nick Coghlan730f67f2012-08-05 22:02:18 +1000442
Zachary Ware9774ce02014-01-14 09:09:48 -0600443 .. attribute:: hostmask
Nick Coghlan730f67f2012-08-05 22:02:18 +1000444
Nick Coghlan7362c3e2012-08-05 22:32:37 +1000445 The host mask, as a string.
Nick Coghlan730f67f2012-08-05 22:02:18 +1000446
447 .. attribute:: with_prefixlen
Nick Coghlan7362c3e2012-08-05 22:32:37 +1000448 .. attribute:: compressed
449 .. attribute:: exploded
Nick Coghlan730f67f2012-08-05 22:02:18 +1000450
Nick Coghlan7362c3e2012-08-05 22:32:37 +1000451 A string representation of the network, with the mask in prefix
452 notation.
453
454 ``with_prefixlen`` and ``compressed`` are always the same as
455 ``str(network)``.
456 ``exploded`` uses the exploded form the network address.
Nick Coghlan730f67f2012-08-05 22:02:18 +1000457
458 .. attribute:: with_netmask
459
Nick Coghlan7362c3e2012-08-05 22:32:37 +1000460 A string representation of the network, with the mask in net mask
461 notation.
Nick Coghlan730f67f2012-08-05 22:02:18 +1000462
463 .. attribute:: with_hostmask
464
Nick Coghlan7362c3e2012-08-05 22:32:37 +1000465 A string representation of the network, with the mask in host mask
466 notation.
Nick Coghlan730f67f2012-08-05 22:02:18 +1000467
468 .. attribute:: num_addresses
469
Nick Coghlan7362c3e2012-08-05 22:32:37 +1000470 The total number of addresses in the network.
Nick Coghlan730f67f2012-08-05 22:02:18 +1000471
472 .. attribute:: prefixlen
473
Nick Coghlan7362c3e2012-08-05 22:32:37 +1000474 Length of the network prefix, in bits.
Nick Coghlan730f67f2012-08-05 22:02:18 +1000475
476 .. method:: hosts()
477
Nick Coghlan7362c3e2012-08-05 22:32:37 +1000478 Returns an iterator over the usable hosts in the network. The usable
479 hosts are all the IP addresses that belong to the network, except the
480 network address itself and the network broadcast address.
Nick Coghlan730f67f2012-08-05 22:02:18 +1000481
Eli Bendersky948af232012-10-07 07:23:50 -0700482 >>> list(ip_network('192.0.2.0/29').hosts()) #doctest: +NORMALIZE_WHITESPACE
Nick Coghlan7362c3e2012-08-05 22:32:37 +1000483 [IPv4Address('192.0.2.1'), IPv4Address('192.0.2.2'),
484 IPv4Address('192.0.2.3'), IPv4Address('192.0.2.4'),
485 IPv4Address('192.0.2.5'), IPv4Address('192.0.2.6')]
Nick Coghlan730f67f2012-08-05 22:02:18 +1000486
487 .. method:: overlaps(other)
488
Nick Coghlan7362c3e2012-08-05 22:32:37 +1000489 ``True`` if this network is partly or wholly contained in *other* or
Terry Jan Reedy0f847642013-03-11 18:34:00 -0400490 *other* is wholly contained in this network.
Nick Coghlan730f67f2012-08-05 22:02:18 +1000491
492 .. method:: address_exclude(network)
493
Nick Coghlan7362c3e2012-08-05 22:32:37 +1000494 Computes the network definitions resulting from removing the given
495 *network* from this one. Returns an iterator of network objects.
496 Raises :exc:`ValueError` if *network* is not completely contained in
497 this network.
Nick Coghlan730f67f2012-08-05 22:02:18 +1000498
Nick Coghlan7362c3e2012-08-05 22:32:37 +1000499 >>> n1 = ip_network('192.0.2.0/28')
500 >>> n2 = ip_network('192.0.2.1/32')
Eli Bendersky948af232012-10-07 07:23:50 -0700501 >>> list(n1.address_exclude(n2)) #doctest: +NORMALIZE_WHITESPACE
Nick Coghlan7362c3e2012-08-05 22:32:37 +1000502 [IPv4Network('192.0.2.8/29'), IPv4Network('192.0.2.4/30'),
503 IPv4Network('192.0.2.2/31'), IPv4Network('192.0.2.0/32')]
Nick Coghlan730f67f2012-08-05 22:02:18 +1000504
505 .. method:: subnets(prefixlen_diff=1, new_prefix=None)
506
Nick Coghlan7362c3e2012-08-05 22:32:37 +1000507 The subnets that join to make the current network definition, depending
508 on the argument values. *prefixlen_diff* is the amount our prefix
509 length should be increased by. *new_prefix* is the desired new
510 prefix of the subnets; it must be larger than our prefix. One and
511 only one of *prefixlen_diff* and *new_prefix* must be set. Returns an
512 iterator of network objects.
Nick Coghlan730f67f2012-08-05 22:02:18 +1000513
Nick Coghlan7362c3e2012-08-05 22:32:37 +1000514 >>> list(ip_network('192.0.2.0/24').subnets())
515 [IPv4Network('192.0.2.0/25'), IPv4Network('192.0.2.128/25')]
Eli Bendersky948af232012-10-07 07:23:50 -0700516 >>> list(ip_network('192.0.2.0/24').subnets(prefixlen_diff=2)) #doctest: +NORMALIZE_WHITESPACE
Nick Coghlan7362c3e2012-08-05 22:32:37 +1000517 [IPv4Network('192.0.2.0/26'), IPv4Network('192.0.2.64/26'),
518 IPv4Network('192.0.2.128/26'), IPv4Network('192.0.2.192/26')]
Eli Bendersky948af232012-10-07 07:23:50 -0700519 >>> list(ip_network('192.0.2.0/24').subnets(new_prefix=26)) #doctest: +NORMALIZE_WHITESPACE
Nick Coghlan7362c3e2012-08-05 22:32:37 +1000520 [IPv4Network('192.0.2.0/26'), IPv4Network('192.0.2.64/26'),
521 IPv4Network('192.0.2.128/26'), IPv4Network('192.0.2.192/26')]
522 >>> list(ip_network('192.0.2.0/24').subnets(new_prefix=23))
523 Traceback (most recent call last):
524 File "<stdin>", line 1, in <module>
525 raise ValueError('new prefix must be longer')
526 ValueError: new prefix must be longer
527 >>> list(ip_network('192.0.2.0/24').subnets(new_prefix=25))
528 [IPv4Network('192.0.2.0/25'), IPv4Network('192.0.2.128/25')]
Nick Coghlan730f67f2012-08-05 22:02:18 +1000529
530 .. method:: supernet(prefixlen_diff=1, new_prefix=None)
531
Nick Coghlan7362c3e2012-08-05 22:32:37 +1000532 The supernet containing this network definition, depending on the
533 argument values. *prefixlen_diff* is the amount our prefix length
534 should be decreased by. *new_prefix* is the desired new prefix of
535 the supernet; it must be smaller than our prefix. One and only one
536 of *prefixlen_diff* and *new_prefix* must be set. Returns a single
537 network object.
Nick Coghlan730f67f2012-08-05 22:02:18 +1000538
Nick Coghlan7362c3e2012-08-05 22:32:37 +1000539 >>> ip_network('192.0.2.0/24').supernet()
540 IPv4Network('192.0.2.0/23')
541 >>> ip_network('192.0.2.0/24').supernet(prefixlen_diff=2)
542 IPv4Network('192.0.0.0/22')
543 >>> ip_network('192.0.2.0/24').supernet(new_prefix=20)
544 IPv4Network('192.0.0.0/20')
Nick Coghlan730f67f2012-08-05 22:02:18 +1000545
Cheryl Sabella91dc64b2017-10-22 17:39:49 -0400546 .. method:: subnet_of(other)
547
548 Returns *True* if this network is a subnet of *other*.
549
550 >>> a = ip_network('192.168.1.0/24')
551 >>> b = ip_network('192.168.1.128/30')
552 >>> b.subnet_of(a)
553 True
554
555 .. versionadded:: 3.7
556
557 .. method:: supernet_of(other)
558
559 Returns *True* if this network is a supernet of *other*.
560
561 >>> a = ip_network('192.168.1.0/24')
562 >>> b = ip_network('192.168.1.128/30')
563 >>> a.supernet_of(b)
564 True
565
566 .. versionadded:: 3.7
567
Nick Coghlan730f67f2012-08-05 22:02:18 +1000568 .. method:: compare_networks(other)
569
Nick Coghlan7362c3e2012-08-05 22:32:37 +1000570 Compare this network to *other*. In this comparison only the network
571 addresses are considered; host bits aren't. Returns either ``-1``,
572 ``0`` or ``1``.
Nick Coghlan730f67f2012-08-05 22:02:18 +1000573
Nick Coghlan7362c3e2012-08-05 22:32:37 +1000574 >>> ip_network('192.0.2.1/32').compare_networks(ip_network('192.0.2.2/32'))
575 -1
576 >>> ip_network('192.0.2.1/32').compare_networks(ip_network('192.0.2.0/32'))
577 1
578 >>> ip_network('192.0.2.1/32').compare_networks(ip_network('192.0.2.1/32'))
579 0
Nick Coghlan9680bdb2012-06-17 17:24:10 +1000580
s-sanjay16f85232017-03-30 00:44:29 -0700581 .. deprecated:: 3.7
582 It uses the same ordering and comparison algorithm as "<", "==", and ">"
583
Nick Coghlan9680bdb2012-06-17 17:24:10 +1000584
Nick Coghlan9680bdb2012-06-17 17:24:10 +1000585.. class:: IPv6Network(address, strict=True)
586
Nick Coghlan730f67f2012-08-05 22:02:18 +1000587 Construct an IPv6 network definition. *address* can be one of the following:
588
589 1. A string consisting of an IP address and an optional mask, separated by
Nick Coghlan7362c3e2012-08-05 22:32:37 +1000590 a slash (``/``). The IP address is the network address, and the mask
Nick Coghlan730f67f2012-08-05 22:02:18 +1000591 can be either a single number, which means it's a *prefix*, or a string
592 representation of an IPv6 address. If it's the latter, the mask is
593 interpreted as a *net mask*. If no mask is provided, it's considered to
594 be ``/128``.
595
596 For example, the following *address* specifications are equivalent:
597 ``2001:db00::0/24`` and ``2001:db00::0/ffff:ff00::``.
598
599 2. An integer that fits into 128 bits. This is equivalent to a
600 single-address network, with the network address being *address* and
601 the mask being ``/128``.
602
Benjamin Peterson0612ffe2015-08-30 14:42:38 -0700603 3. An integer packed into a :class:`bytes` object of length 16, big-endian.
Nick Coghlan730f67f2012-08-05 22:02:18 +1000604 The interpretation is similar to an integer *address*.
605
Antoine Pitrou5fb195f2014-05-12 20:36:46 +0200606 4. A two-tuple of an address description and a netmask, where the address
607 description is either a string, a 128-bits integer, a 16-bytes packed
Berker Peksag420e4d82016-06-10 14:26:07 +0300608 integer, or an existing IPv6Address object; and the netmask is an
Antoine Pitrou5fb195f2014-05-12 20:36:46 +0200609 integer representing the prefix length.
610
Nick Coghlan730f67f2012-08-05 22:02:18 +1000611 An :exc:`AddressValueError` is raised if *address* is not a valid IPv6
612 address. A :exc:`NetmaskValueError` is raised if the mask is not valid for
613 an IPv6 address.
Nick Coghlan9680bdb2012-06-17 17:24:10 +1000614
615 If *strict* is ``True`` and host bits are set in the supplied address,
Nick Coghlan730f67f2012-08-05 22:02:18 +1000616 then :exc:`ValueError` is raised. Otherwise, the host bits are masked out
Nick Coghlan9680bdb2012-06-17 17:24:10 +1000617 to determine the appropriate network address.
618
Antoine Pitrou5fb195f2014-05-12 20:36:46 +0200619 .. versionchanged:: 3.5
620
621 Added the two-tuple form for the *address* constructor parameter.
622
Nick Coghlan7362c3e2012-08-05 22:32:37 +1000623 .. attribute:: version
624 .. attribute:: max_prefixlen
625 .. attribute:: is_multicast
626 .. attribute:: is_private
627 .. attribute:: is_unspecified
628 .. attribute:: is_reserved
629 .. attribute:: is_loopback
630 .. attribute:: is_link_local
631 .. attribute:: network_address
632 .. attribute:: broadcast_address
Zachary Ware9774ce02014-01-14 09:09:48 -0600633 .. attribute:: hostmask
Nick Coghlan7362c3e2012-08-05 22:32:37 +1000634 .. attribute:: with_prefixlen
635 .. attribute:: compressed
636 .. attribute:: exploded
637 .. attribute:: with_netmask
638 .. attribute:: with_hostmask
639 .. attribute:: num_addresses
640 .. attribute:: prefixlen
641 .. method:: hosts()
642 .. method:: overlaps(other)
643 .. method:: address_exclude(network)
644 .. method:: subnets(prefixlen_diff=1, new_prefix=None)
645 .. method:: supernet(prefixlen_diff=1, new_prefix=None)
Cheryl Sabella91dc64b2017-10-22 17:39:49 -0400646 .. method:: subnet_of(other)
647 .. method:: supernet_of(other)
Nick Coghlan7362c3e2012-08-05 22:32:37 +1000648 .. method:: compare_networks(other)
Nick Coghlan730f67f2012-08-05 22:02:18 +1000649
Nick Coghlan7362c3e2012-08-05 22:32:37 +1000650 Refer to the corresponding attribute documentation in
651 :class:`IPv4Network`
652
653 .. attribute:: is_site_local
654
655 These attribute is true for the network as a whole if it is true
Terry Jan Reedy0f847642013-03-11 18:34:00 -0400656 for both the network address and the broadcast address
Nick Coghlan730f67f2012-08-05 22:02:18 +1000657
658
659Operators
660^^^^^^^^^
661
662Network objects support some operators. Unless stated otherwise, operators can
663only be applied between compatible objects (i.e. IPv4 with IPv4, IPv6 with
664IPv6).
665
Nick Coghlan7362c3e2012-08-05 22:32:37 +1000666
Nick Coghlan730f67f2012-08-05 22:02:18 +1000667Logical operators
668"""""""""""""""""
669
670Network objects can be compared with the usual set of logical operators,
671similarly to address objects.
672
Nick Coghlan7362c3e2012-08-05 22:32:37 +1000673
Nick Coghlan730f67f2012-08-05 22:02:18 +1000674Iteration
675"""""""""
676
677Network objects can be iterated to list all the addresses belonging to the
678network. For iteration, *all* hosts are returned, including unusable hosts
679(for usable hosts, use the :meth:`~IPv4Network.hosts` method). An
680example::
681
682 >>> for addr in IPv4Network('192.0.2.0/28'):
Serhiy Storchakadba90392016-05-10 12:01:23 +0300683 ... addr
Nick Coghlan730f67f2012-08-05 22:02:18 +1000684 ...
685 IPv4Address('192.0.2.0')
686 IPv4Address('192.0.2.1')
687 IPv4Address('192.0.2.2')
688 IPv4Address('192.0.2.3')
689 IPv4Address('192.0.2.4')
690 IPv4Address('192.0.2.5')
691 IPv4Address('192.0.2.6')
692 IPv4Address('192.0.2.7')
693 IPv4Address('192.0.2.8')
694 IPv4Address('192.0.2.9')
695 IPv4Address('192.0.2.10')
696 IPv4Address('192.0.2.11')
697 IPv4Address('192.0.2.12')
698 IPv4Address('192.0.2.13')
699 IPv4Address('192.0.2.14')
700 IPv4Address('192.0.2.15')
701
Nick Coghlan7362c3e2012-08-05 22:32:37 +1000702
Nick Coghlan730f67f2012-08-05 22:02:18 +1000703Networks as containers of addresses
704"""""""""""""""""""""""""""""""""""
705
706Network objects can act as containers of addresses. Some examples::
707
708 >>> IPv4Network('192.0.2.0/28')[0]
709 IPv4Address('192.0.2.0')
710 >>> IPv4Network('192.0.2.0/28')[15]
711 IPv4Address('192.0.2.15')
712 >>> IPv4Address('192.0.2.6') in IPv4Network('192.0.2.0/28')
713 True
714 >>> IPv4Address('192.0.3.6') in IPv4Network('192.0.2.0/28')
715 False
Nick Coghlan9680bdb2012-06-17 17:24:10 +1000716
717
Eli Bendersky0e497492012-07-31 17:23:11 +0300718Interface objects
719-----------------
720
721.. class:: IPv4Interface(address)
722
Nick Coghlana8517ad2012-08-20 10:04:26 +1000723 Construct an IPv4 interface. The meaning of *address* is as in the
724 constructor of :class:`IPv4Network`, except that arbitrary host addresses
725 are always accepted.
Eli Bendersky0e497492012-07-31 17:23:11 +0300726
Nick Coghlana8517ad2012-08-20 10:04:26 +1000727 :class:`IPv4Interface` is a subclass of :class:`IPv4Address`, so it inherits
728 all the attributes from that class. In addition, the following attributes
729 are available:
Eli Bendersky0e497492012-07-31 17:23:11 +0300730
Nick Coghlana8517ad2012-08-20 10:04:26 +1000731 .. attribute:: ip
732
733 The address (:class:`IPv4Address`) without network information.
734
735 >>> interface = IPv4Interface('192.0.2.5/24')
736 >>> interface.ip
737 IPv4Address('192.0.2.5')
738
739 .. attribute:: network
740
741 The network (:class:`IPv4Network`) this interface belongs to.
742
743 >>> interface = IPv4Interface('192.0.2.5/24')
744 >>> interface.network
745 IPv4Network('192.0.2.0/24')
746
747 .. attribute:: with_prefixlen
748
749 A string representation of the interface with the mask in prefix notation.
750
751 >>> interface = IPv4Interface('192.0.2.5/24')
752 >>> interface.with_prefixlen
753 '192.0.2.5/24'
754
755 .. attribute:: with_netmask
756
757 A string representation of the interface with the network as a net mask.
758
759 >>> interface = IPv4Interface('192.0.2.5/24')
760 >>> interface.with_netmask
761 '192.0.2.5/255.255.255.0'
762
763 .. attribute:: with_hostmask
764
765 A string representation of the interface with the network as a host mask.
766
767 >>> interface = IPv4Interface('192.0.2.5/24')
768 >>> interface.with_hostmask
769 '192.0.2.5/0.0.0.255'
Eli Bendersky0e497492012-07-31 17:23:11 +0300770
771
772.. class:: IPv6Interface(address)
773
Nick Coghlana8517ad2012-08-20 10:04:26 +1000774 Construct an IPv6 interface. The meaning of *address* is as in the
775 constructor of :class:`IPv6Network`, except that arbitrary host addresses
776 are always accepted.
Eli Bendersky0e497492012-07-31 17:23:11 +0300777
Nick Coghlana8517ad2012-08-20 10:04:26 +1000778 :class:`IPv6Interface` is a subclass of :class:`IPv6Address`, so it inherits
779 all the attributes from that class. In addition, the following attributes
780 are available:
Eli Bendersky0e497492012-07-31 17:23:11 +0300781
Nick Coghlana8517ad2012-08-20 10:04:26 +1000782 .. attribute:: ip
783 .. attribute:: network
784 .. attribute:: with_prefixlen
785 .. attribute:: with_netmask
786 .. attribute:: with_hostmask
787
788 Refer to the corresponding attribute documentation in
789 :class:`IPv4Interface`.
Eli Bendersky0e497492012-07-31 17:23:11 +0300790
791
Nick Coghlan9680bdb2012-06-17 17:24:10 +1000792Other Module Level Functions
793----------------------------
794
795The module also provides the following module level functions:
796
797.. function:: v4_int_to_packed(address)
798
799 Represent an address as 4 packed bytes in network (big-endian) order.
800 *address* is an integer representation of an IPv4 IP address. A
801 :exc:`ValueError` is raised if the integer is negative or too large to be an
802 IPv4 IP address.
803
804 >>> ipaddress.ip_address(3221225985)
805 IPv4Address('192.0.2.1')
806 >>> ipaddress.v4_int_to_packed(3221225985)
807 b'\xc0\x00\x02\x01'
808
809
810.. function:: v6_int_to_packed(address)
811
812 Represent an address as 16 packed bytes in network (big-endian) order.
813 *address* is an integer representation of an IPv6 IP address. A
814 :exc:`ValueError` is raised if the integer is negative or too large to be an
815 IPv6 IP address.
816
817
818.. function:: summarize_address_range(first, last)
819
820 Return an iterator of the summarized network range given the first and last
821 IP addresses. *first* is the first :class:`IPv4Address` or
822 :class:`IPv6Address` in the range and *last* is the last :class:`IPv4Address`
823 or :class:`IPv6Address` in the range. A :exc:`TypeError` is raised if
824 *first* or *last* are not IP addresses or are not of the same version. A
825 :exc:`ValueError` is raised if *last* is not greater than *first* or if
826 *first* address version is not 4 or 6.
827
828 >>> [ipaddr for ipaddr in ipaddress.summarize_address_range(
829 ... ipaddress.IPv4Address('192.0.2.0'),
830 ... ipaddress.IPv4Address('192.0.2.130'))]
831 [IPv4Network('192.0.2.0/25'), IPv4Network('192.0.2.128/31'), IPv4Network('192.0.2.130/32')]
832
833
834.. function:: collapse_addresses(addresses)
835
836 Return an iterator of the collapsed :class:`IPv4Network` or
837 :class:`IPv6Network` objects. *addresses* is an iterator of
838 :class:`IPv4Network` or :class:`IPv6Network` objects. A :exc:`TypeError` is
839 raised if *addresses* contains mixed version objects.
840
841 >>> [ipaddr for ipaddr in
842 ... ipaddress.collapse_addresses([ipaddress.IPv4Network('192.0.2.0/25'),
843 ... ipaddress.IPv4Network('192.0.2.128/25')])]
844 [IPv4Network('192.0.2.0/24')]
845
846
847.. function:: get_mixed_type_key(obj)
848
849 Return a key suitable for sorting between networks and addresses. Address
850 and Network objects are not sortable by default; they're fundamentally
851 different, so the expression::
852
853 IPv4Address('192.0.2.0') <= IPv4Network('192.0.2.0/24')
854
855 doesn't make sense. There are some times however, where you may wish to
856 have :mod:`ipaddress` sort these anyway. If you need to do this, you can use
857 this function as the ``key`` argument to :func:`sorted()`.
858
859 *obj* is either a network or address object.
860
861
862Custom Exceptions
863-----------------
864
865To support more specific error reporting from class constructors, the
866module defines the following exceptions:
867
868.. exception:: AddressValueError(ValueError)
869
870 Any value error related to the address.
871
872
873.. exception:: NetmaskValueError(ValueError)
874
875 Any value error related to the netmask.