blob: 826e4aa948f5433b9fb83fc41cf439c3098192cb [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.
6.. moduleauthor:: Peter Moody
7
8**Source code:** :source:`Lib/ipaddress.py`
9
10--------------
11
Nick Coghlan730f67f2012-08-05 22:02:18 +100012.. note::
Nick Coghlan9680bdb2012-06-17 17:24:10 +100013
Nick Coghlan730f67f2012-08-05 22:02:18 +100014 The ``ipaddress`` module has been included in the standard library on a
15 :term:`provisional basis <provisional package>`. Backwards incompatible
16 changes (up to and including removal of the package) may occur if deemed
17 necessary by the core developers.
18
19:mod:`ipaddress` provides the capabilities to create, manipulate and
20operate on IPv4 and IPv6 addresses and networks.
Nick Coghlan9680bdb2012-06-17 17:24:10 +100021
22The functions and classes in this module make it straightforward to handle
23various tasks related to IP addresses, including checking whether or not two
24hosts are on the same subnet, iterating over all hosts in a particular
Nick Coghlan730f67f2012-08-05 22:02:18 +100025subnet, checking whether or not a string represents a valid IP address or
26network definition, and so on.
27
28This is the full module API reference - for an overview and introduction,
29see :ref:`ipaddress-howto`.
30
31.. versionadded:: 3.3
Nick Coghlan9680bdb2012-06-17 17:24:10 +100032
33
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
48.. testsetup::
49 >>> import ipaddress
50 >>> from ipaddress import (ip_network, IPv4Address, IPv4Interface,
51 ... IPv4Network)
52
53::
Nick Coghlan9680bdb2012-06-17 17:24:10 +100054
55 >>> ipaddress.ip_address('192.168.0.1')
56 IPv4Address('192.168.0.1')
57 >>> ipaddress.ip_address('2001:db8::')
58 IPv6Address('2001:db8::')
59
60
61.. function:: ip_network(address, strict=True)
62
63 Return an :class:`IPv4Network` or :class:`IPv6Network` object depending on
64 the IP address passed as argument. *address* is a string or integer
65 representing the IP network. Either IPv4 or IPv6 networks may be supplied;
66 integers less than 2**32 will be considered to be IPv4 by default. *strict*
67 is passed to :class:`IPv4Network` or :class:`IPv6Network` constructor. A
Eli Bendersky0e497492012-07-31 17:23:11 +030068 :exc:`ValueError` is raised if *address* does not represent a valid IPv4 or
69 IPv6 address, or if the network has host bits set.
Nick Coghlan9680bdb2012-06-17 17:24:10 +100070
71 >>> ipaddress.ip_network('192.168.0.0/28')
72 IPv4Network('192.168.0.0/28')
73
74
75.. function:: ip_interface(address)
76
77 Return an :class:`IPv4Interface` or :class:`IPv6Interface` object depending
78 on the IP address passed as argument. *address* is a string or integer
79 representing the IP address. Either IPv4 or IPv6 addresses may be supplied;
Eli Bendersky0e497492012-07-31 17:23:11 +030080 integers less than 2**32 will be considered to be IPv4 by default. A
81 :exc:`ValueError` is raised if *address* does not represent a valid IPv4 or
Nick Coghlan9680bdb2012-06-17 17:24:10 +100082 IPv6 address.
83
Nick Coghlan730f67f2012-08-05 22:02:18 +100084One downside of these convenience functions is that the need to handle both
85IPv4 and IPv6 formats means that error messages provide minimal
86information on the precise error, as the functions don't know whether the
87IPv4 or IPv6 format was intended. More detailed error reporting can be
88obtained by calling the appropriate version specific class constructors
89directly.
90
91
92IP Addresses
93------------
Nick Coghlan9680bdb2012-06-17 17:24:10 +100094
Eli Bendersky0e497492012-07-31 17:23:11 +030095Address objects
Nick Coghlan730f67f2012-08-05 22:02:18 +100096^^^^^^^^^^^^^^^
Nick Coghlan9680bdb2012-06-17 17:24:10 +100097
Eli Bendersky0e497492012-07-31 17:23:11 +030098The :class:`IPv4Address` and :class:`IPv6Address` objects share a lot of common
99attributes. Some attributes that are only meaningful for IPv6 addresses are
100also implemented by :class:`IPv4Address` objects, in order to make it easier to
Nick Coghlan730f67f2012-08-05 22:02:18 +1000101write code that handles both IP versions correctly.
Nick Coghlan9680bdb2012-06-17 17:24:10 +1000102
103.. class:: IPv4Address(address)
104
Eli Bendersky0e497492012-07-31 17:23:11 +0300105 Construct an IPv4 address. An :exc:`AddressValueError` is raised if
106 *address* is not a valid IPv4 address.
107
108 The following constitutes a valid IPv4 address:
109
110 1. A string in decimal-dot notation, consisting of four decimal integers in
111 the inclusive range 0-255, separated by dots (e.g. ``192.168.0.1``). Each
Nick Coghlan730f67f2012-08-05 22:02:18 +1000112 integer represents an octet (byte) in the address. Leading zeroes are
113 tolerated only for values less then 8 (as there is no ambiguity
114 between the decimal and octal interpretations of such strings).
Eli Bendersky0e497492012-07-31 17:23:11 +0300115 2. An integer that fits into 32 bits.
Nick Coghlan730f67f2012-08-05 22:02:18 +1000116 3. An integer packed into a :class:`bytes` object of length 4 (most
117 significant octet first).
Nick Coghlan9680bdb2012-06-17 17:24:10 +1000118
119 >>> ipaddress.IPv4Address('192.168.0.1')
120 IPv4Address('192.168.0.1')
Eli Bendersky948af232012-10-07 07:23:50 -0700121 >>> ipaddress.IPv4Address(3232235521)
Nick Coghlan730f67f2012-08-05 22:02:18 +1000122 IPv4Address('192.168.0.1')
123 >>> ipaddress.IPv4Address(b'\xC0\xA8\x00\x01')
124 IPv4Address('192.168.0.1')
Eli Bendersky0e497492012-07-31 17:23:11 +0300125
126 .. attribute:: version
127
Nick Coghlan730f67f2012-08-05 22:02:18 +1000128 The appropriate version number: ``4`` for IPv4, ``6`` for IPv6.
Eli Bendersky0e497492012-07-31 17:23:11 +0300129
130 .. attribute:: max_prefixlen
131
Nick Coghlan730f67f2012-08-05 22:02:18 +1000132 The total number of bits in the address representation for this
133 version: ``32`` for IPv4, ``128`` for IPv6.
134
135 The prefix defines the number of leading bits in an address that
136 are compared to determine whether or not an address is part of a
137 network.
138
139 .. attribute:: compressed
140 .. attribute:: exploded
141
142 The string representation in dotted decimal notation. Leading zeroes
143 are never included in the representation.
144
145 As IPv4 does not define a shorthand notation for addresses with octets
146 set to zero, these two attributes are always the same as ``str(addr)``
147 for IPv4 addresses. Exposing these attributes makes it easier to
148 write display code that can handle both IPv4 and IPv6 addresses.
149
150 .. attribute:: packed
151
152 The binary representation of this address - a :class:`bytes` object of
153 the appropriate length (most significant octet first). This is 4 bytes
154 for IPv4 and 16 bytes for IPv6.
Eli Bendersky0e497492012-07-31 17:23:11 +0300155
156 .. attribute:: is_multicast
157
Nick Coghlan730f67f2012-08-05 22:02:18 +1000158 ``True`` if the address is reserved for multicast use. See
159 :RFC:`3171` (for IPv4) or :RFC:`2373` (for IPv6).
Eli Bendersky0e497492012-07-31 17:23:11 +0300160
Peter Moodye5019d52013-10-24 09:47:10 -0700161 .. attribute:: is_private
162
163 ``True`` if the address is allocated for private networks. See
164 iana-ipv4-special-registry (for IPv4) or iana-ipv6-special-registry
165 (for IPv6).
166
Peter Moody8ed30c12013-10-21 16:16:51 -0700167 .. attribute:: is_global
Eli Bendersky0e497492012-07-31 17:23:11 +0300168
Peter Moodybe9c1b12013-10-22 12:36:21 -0700169 ``True`` if the address is allocated for public networks. See
Peter Moody8ed30c12013-10-21 16:16:51 -0700170 iana-ipv4-special-registry (for IPv4) or iana-ipv6-special-registry
171 (for IPv6).
172
173 .. versionadded:: 3.4
Eli Bendersky0e497492012-07-31 17:23:11 +0300174
175 .. attribute:: is_unspecified
176
Nick Coghlan730f67f2012-08-05 22:02:18 +1000177 ``True`` if the address is unspecified. See :RFC:`5375` (for IPv4)
178 or :RFC:`2373` (for IPv6).
Eli Bendersky0e497492012-07-31 17:23:11 +0300179
180 .. attribute:: is_reserved
181
Nick Coghlan730f67f2012-08-05 22:02:18 +1000182 ``True`` if the address is otherwise IETF reserved.
Eli Bendersky0e497492012-07-31 17:23:11 +0300183
184 .. attribute:: is_loopback
185
Nick Coghlan730f67f2012-08-05 22:02:18 +1000186 ``True`` if this is a loopback address. See :RFC:`3330` (for IPv4)
187 or :RFC:`2373` (for IPv6).
Eli Bendersky0e497492012-07-31 17:23:11 +0300188
189 .. attribute:: is_link_local
190
Nick Coghlan730f67f2012-08-05 22:02:18 +1000191 ``True`` if the address is reserved for link-local usage. See
192 :RFC:`3927`.
193
Eli Bendersky0e497492012-07-31 17:23:11 +0300194
195.. class:: IPv6Address(address)
196
197 Construct an IPv6 address. An :exc:`AddressValueError` is raised if
198 *address* is not a valid IPv6 address.
199
200 The following constitutes a valid IPv6 address:
201
202 1. A string consisting of eight groups of four hexadecimal digits, each
203 group representing 16 bits. The groups are separated by colons.
204 This describes an *exploded* (longhand) notation. The string can
205 also be *compressed* (shorthand notation) by various means. See
206 :RFC:`4291` for details. For example,
207 ``"0000:0000:0000:0000:0000:0abc:0007:0def"`` can be compressed to
208 ``"::abc:7:def"``.
209 2. An integer that fits into 128 bits.
210 3. An integer packed into a :class:`bytes` object of length 16, big-endian.
211
212 >>> ipaddress.IPv6Address('2001:db8::1000')
213 IPv6Address('2001:db8::1000')
214
Nick Coghlan730f67f2012-08-05 22:02:18 +1000215 .. attribute:: compressed
216
217 The short form of the address representation, with leading zeroes in
218 groups omitted and the longest sequence of groups consisting entirely of
219 zeroes collapsed to a single empty group.
220
221 This is also the value returned by ``str(addr)`` for IPv6 addresses.
222
223 .. attribute:: exploded
224
225 The long form of the address representation, with all leading zeroes and
226 groups consisting entirely of zeroes included.
227
228 .. attribute:: packed
229 .. attribute:: version
230 .. attribute:: max_prefixlen
231 .. attribute:: is_multicast
232 .. attribute:: is_private
233 .. attribute:: is_unspecified
234 .. attribute:: is_reserved
235 .. attribute:: is_loopback
236 .. attribute:: is_link_local
237
238 Refer to the corresponding attribute documentation in
239 :class:`IPv4Address`
Eli Bendersky0e497492012-07-31 17:23:11 +0300240
241 .. attribute:: is_site_local
242
Nick Coghlan730f67f2012-08-05 22:02:18 +1000243 ``True`` if the address is reserved for site-local usage. Note that
244 the site-local address space has been deprecated by :RFC:`3879`. Use
245 :attr:`~IPv4Address.is_private` to test if this address is in the
246 space of unique local addresses as defined by :RFC:`4193`.
Eli Bendersky0e497492012-07-31 17:23:11 +0300247
248 .. attribute:: ipv4_mapped
249
Nick Coghlan730f67f2012-08-05 22:02:18 +1000250 For addresses that appear to be IPv4 mapped addresses (starting with
251 ``::FFFF/96``), this property will report the embedded IPv4 address.
252 For any other address, this property will be ``None``.
Eli Bendersky0e497492012-07-31 17:23:11 +0300253
254 .. attribute:: sixtofour
255
Nick Coghlan730f67f2012-08-05 22:02:18 +1000256 For addresses that appear to be 6to4 addresses (starting with
257 ``2002::/16``) as defined by :RFC:`3056`, this property will report
258 the embedded IPv4 address. For any other address, this property will
259 be ``None``.
260
261 .. attribute:: teredo
262
263 For addresses that appear to be Teredo addresses (starting with
264 ``2001::/32``) as defined by :RFC:`4380`, this property will report
265 the embedded ``(server, client)`` IP address pair. For any other
266 address, this property will be ``None``.
267
268
269Conversion to Strings and Integers
270^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
271
272To interoperate with networking interfaces such as the socket module,
273addresses must be converted to strings or integers. This is handled using
274the :func:`str` and :func:`int` builtin functions::
275
276 >>> str(ipaddress.IPv4Address('192.168.0.1'))
277 '192.168.0.1'
278 >>> int(ipaddress.IPv4Address('192.168.0.1'))
279 3232235521
280 >>> str(ipaddress.IPv6Address('::1'))
281 '::1'
282 >>> int(ipaddress.IPv6Address('::1'))
283 1
Eli Bendersky0e497492012-07-31 17:23:11 +0300284
285
286Operators
287^^^^^^^^^
288
289Address objects support some operators. Unless stated otherwise, operators can
290only be applied between compatible objects (i.e. IPv4 with IPv4, IPv6 with
291IPv6).
292
Nick Coghlan730f67f2012-08-05 22:02:18 +1000293
Georg Brandl9ad417e2013-10-06 19:23:57 +0200294Comparison operators
295""""""""""""""""""""
Eli Bendersky0e497492012-07-31 17:23:11 +0300296
Georg Brandl9ad417e2013-10-06 19:23:57 +0200297Address objects can be compared with the usual set of comparison operators. Some
Eli Bendersky0e497492012-07-31 17:23:11 +0300298examples::
299
300 >>> IPv4Address('127.0.0.2') > IPv4Address('127.0.0.1')
301 True
302 >>> IPv4Address('127.0.0.2') == IPv4Address('127.0.0.1')
303 False
304 >>> IPv4Address('127.0.0.2') != IPv4Address('127.0.0.1')
305 True
306
Nick Coghlan730f67f2012-08-05 22:02:18 +1000307
Eli Bendersky0e497492012-07-31 17:23:11 +0300308Arithmetic operators
309""""""""""""""""""""
310
311Integers can be added to or subtracted from address objects. Some examples::
312
313 >>> IPv4Address('127.0.0.2') + 3
314 IPv4Address('127.0.0.5')
315 >>> IPv4Address('127.0.0.2') - 3
316 IPv4Address('126.255.255.255')
317 >>> IPv4Address('255.255.255.255') + 1
318 Traceback (most recent call last):
319 File "<stdin>", line 1, in <module>
320 ipaddress.AddressValueError: 4294967296 (>= 2**32) is not permitted as an IPv4 address
321
322
Nick Coghlan730f67f2012-08-05 22:02:18 +1000323IP Network definitions
324----------------------
325
326The :class:`IPv4Network` and :class:`IPv6Network` objects provide a mechanism
327for defining and inspecting IP network definitions. A network definition
328consists of a *mask* and a *network address*, and as such defines a range of
329IP addresses that equal the network address when masked (binary AND) with the
330mask. For example, a network definition with the mask ``255.255.255.0`` and
331the network address ``192.168.1.0`` consists of IP addresses in the inclusive
332range ``192.168.1.0`` to ``192.168.1.255``.
333
334
335Prefix, net mask and host mask
336^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
337
338There are several equivalent ways to specify IP network masks. A *prefix*
339``/<nbits>`` is a notation that denotes how many high-order bits are set in
340the network mask. A *net mask* is an IP address with some number of
341high-order bits set. Thus the prefix ``/24`` is equivalent to the net mask
342``255.255.255.0`` in IPv4, or ``ffff:ff00::`` in IPv6. In addition, a
343*host mask* is the logical inverse of a *net mask*, and is sometimes used
344(for example in Cisco access control lists) to denote a network mask. The
345host mask equivalent to ``/24`` in IPv4 is ``0.0.0.255``.
346
347
Eli Bendersky0e497492012-07-31 17:23:11 +0300348Network objects
Nick Coghlan730f67f2012-08-05 22:02:18 +1000349^^^^^^^^^^^^^^^
350
351All attributes implemented by address objects are implemented by network
352objects as well. In addition, network objects implement additional attributes.
353All of these are common between :class:`IPv4Network` and :class:`IPv6Network`,
354so to avoid duplication they are only documented for :class:`IPv4Network`.
Nick Coghlan9680bdb2012-06-17 17:24:10 +1000355
356.. class:: IPv4Network(address, strict=True)
357
Nick Coghlan730f67f2012-08-05 22:02:18 +1000358 Construct an IPv4 network definition. *address* can be one of the following:
359
360 1. A string consisting of an IP address and an optional mask, separated by
361 a slash (``/``). The IP address is the network address, and the mask
362 can be either a single number, which means it's a *prefix*, or a string
363 representation of an IPv4 address. If it's the latter, the mask is
364 interpreted as a *net mask* if it starts with a non-zero field, or as
365 a *host mask* if it starts with a zero field. If no mask is provided,
366 it's considered to be ``/32``.
367
368 For example, the following *address* specifications are equivalent:
369 ``192.168.1.0/24``, ``192.168.1.0/255.255.255.0`` and
370 ``192.168.1.0/0.0.0.255``.
371
372 2. An integer that fits into 32 bits. This is equivalent to a
373 single-address network, with the network address being *address* and
374 the mask being ``/32``.
375
376 3. An integer packed into a :class:`bytes` object of length 4, big-endian.
377 The interpretation is similar to an integer *address*.
378
379 An :exc:`AddressValueError` is raised if *address* is not a valid IPv4
380 address. A :exc:`NetmaskValueError` is raised if the mask is not valid for
381 an IPv4 address.
Nick Coghlan9680bdb2012-06-17 17:24:10 +1000382
383 If *strict* is ``True`` and host bits are set in the supplied address,
Nick Coghlan730f67f2012-08-05 22:02:18 +1000384 then :exc:`ValueError` is raised. Otherwise, the host bits are masked out
Nick Coghlan9680bdb2012-06-17 17:24:10 +1000385 to determine the appropriate network address.
386
Nick Coghlan7362c3e2012-08-05 22:32:37 +1000387 Unless stated otherwise, all network methods accepting other network/address
388 objects will raise :exc:`TypeError` if the argument's IP version is
389 incompatible to ``self``
390
391 .. attribute:: version
392 .. attribute:: max_prefixlen
393
394 Refer to the corresponding attribute documentation in
395 :class:`IPv4Address`
396
397 .. attribute:: is_multicast
398 .. attribute:: is_private
399 .. attribute:: is_unspecified
400 .. attribute:: is_reserved
401 .. attribute:: is_loopback
402 .. attribute:: is_link_local
403
404 These attributes are true for the network as a whole if they are true
Terry Jan Reedy0f847642013-03-11 18:34:00 -0400405 for both the network address and the broadcast address
Nick Coghlan7362c3e2012-08-05 22:32:37 +1000406
407 .. attribute:: network_address
408
Nick Coghlan31096a92012-08-05 22:52:38 +1000409 The network address for the network. The network address and the
410 prefix length together uniquely define a network.
Nick Coghlan730f67f2012-08-05 22:02:18 +1000411
412 .. attribute:: broadcast_address
413
Nick Coghlan31096a92012-08-05 22:52:38 +1000414 The broadcast address for the network. Packets sent to the broadcast
415 address should be received by every host on the network.
Nick Coghlan730f67f2012-08-05 22:02:18 +1000416
417 .. attribute:: host mask
418
Nick Coghlan7362c3e2012-08-05 22:32:37 +1000419 The host mask, as a string.
Nick Coghlan730f67f2012-08-05 22:02:18 +1000420
421 .. attribute:: with_prefixlen
Nick Coghlan7362c3e2012-08-05 22:32:37 +1000422 .. attribute:: compressed
423 .. attribute:: exploded
Nick Coghlan730f67f2012-08-05 22:02:18 +1000424
Nick Coghlan7362c3e2012-08-05 22:32:37 +1000425 A string representation of the network, with the mask in prefix
426 notation.
427
428 ``with_prefixlen`` and ``compressed`` are always the same as
429 ``str(network)``.
430 ``exploded`` uses the exploded form the network address.
Nick Coghlan730f67f2012-08-05 22:02:18 +1000431
432 .. attribute:: with_netmask
433
Nick Coghlan7362c3e2012-08-05 22:32:37 +1000434 A string representation of the network, with the mask in net mask
435 notation.
Nick Coghlan730f67f2012-08-05 22:02:18 +1000436
437 .. attribute:: with_hostmask
438
Nick Coghlan7362c3e2012-08-05 22:32:37 +1000439 A string representation of the network, with the mask in host mask
440 notation.
Nick Coghlan730f67f2012-08-05 22:02:18 +1000441
442 .. attribute:: num_addresses
443
Nick Coghlan7362c3e2012-08-05 22:32:37 +1000444 The total number of addresses in the network.
Nick Coghlan730f67f2012-08-05 22:02:18 +1000445
446 .. attribute:: prefixlen
447
Nick Coghlan7362c3e2012-08-05 22:32:37 +1000448 Length of the network prefix, in bits.
Nick Coghlan730f67f2012-08-05 22:02:18 +1000449
450 .. method:: hosts()
451
Nick Coghlan7362c3e2012-08-05 22:32:37 +1000452 Returns an iterator over the usable hosts in the network. The usable
453 hosts are all the IP addresses that belong to the network, except the
454 network address itself and the network broadcast address.
Nick Coghlan730f67f2012-08-05 22:02:18 +1000455
Eli Bendersky948af232012-10-07 07:23:50 -0700456 >>> list(ip_network('192.0.2.0/29').hosts()) #doctest: +NORMALIZE_WHITESPACE
Nick Coghlan7362c3e2012-08-05 22:32:37 +1000457 [IPv4Address('192.0.2.1'), IPv4Address('192.0.2.2'),
458 IPv4Address('192.0.2.3'), IPv4Address('192.0.2.4'),
459 IPv4Address('192.0.2.5'), IPv4Address('192.0.2.6')]
Nick Coghlan730f67f2012-08-05 22:02:18 +1000460
461 .. method:: overlaps(other)
462
Nick Coghlan7362c3e2012-08-05 22:32:37 +1000463 ``True`` if this network is partly or wholly contained in *other* or
Terry Jan Reedy0f847642013-03-11 18:34:00 -0400464 *other* is wholly contained in this network.
Nick Coghlan730f67f2012-08-05 22:02:18 +1000465
466 .. method:: address_exclude(network)
467
Nick Coghlan7362c3e2012-08-05 22:32:37 +1000468 Computes the network definitions resulting from removing the given
469 *network* from this one. Returns an iterator of network objects.
470 Raises :exc:`ValueError` if *network* is not completely contained in
471 this network.
Nick Coghlan730f67f2012-08-05 22:02:18 +1000472
Nick Coghlan7362c3e2012-08-05 22:32:37 +1000473 >>> n1 = ip_network('192.0.2.0/28')
474 >>> n2 = ip_network('192.0.2.1/32')
Eli Bendersky948af232012-10-07 07:23:50 -0700475 >>> list(n1.address_exclude(n2)) #doctest: +NORMALIZE_WHITESPACE
Nick Coghlan7362c3e2012-08-05 22:32:37 +1000476 [IPv4Network('192.0.2.8/29'), IPv4Network('192.0.2.4/30'),
477 IPv4Network('192.0.2.2/31'), IPv4Network('192.0.2.0/32')]
Nick Coghlan730f67f2012-08-05 22:02:18 +1000478
479 .. method:: subnets(prefixlen_diff=1, new_prefix=None)
480
Nick Coghlan7362c3e2012-08-05 22:32:37 +1000481 The subnets that join to make the current network definition, depending
482 on the argument values. *prefixlen_diff* is the amount our prefix
483 length should be increased by. *new_prefix* is the desired new
484 prefix of the subnets; it must be larger than our prefix. One and
485 only one of *prefixlen_diff* and *new_prefix* must be set. Returns an
486 iterator of network objects.
Nick Coghlan730f67f2012-08-05 22:02:18 +1000487
Nick Coghlan7362c3e2012-08-05 22:32:37 +1000488 >>> list(ip_network('192.0.2.0/24').subnets())
489 [IPv4Network('192.0.2.0/25'), IPv4Network('192.0.2.128/25')]
Eli Bendersky948af232012-10-07 07:23:50 -0700490 >>> list(ip_network('192.0.2.0/24').subnets(prefixlen_diff=2)) #doctest: +NORMALIZE_WHITESPACE
Nick Coghlan7362c3e2012-08-05 22:32:37 +1000491 [IPv4Network('192.0.2.0/26'), IPv4Network('192.0.2.64/26'),
492 IPv4Network('192.0.2.128/26'), IPv4Network('192.0.2.192/26')]
Eli Bendersky948af232012-10-07 07:23:50 -0700493 >>> list(ip_network('192.0.2.0/24').subnets(new_prefix=26)) #doctest: +NORMALIZE_WHITESPACE
Nick Coghlan7362c3e2012-08-05 22:32:37 +1000494 [IPv4Network('192.0.2.0/26'), IPv4Network('192.0.2.64/26'),
495 IPv4Network('192.0.2.128/26'), IPv4Network('192.0.2.192/26')]
496 >>> list(ip_network('192.0.2.0/24').subnets(new_prefix=23))
497 Traceback (most recent call last):
498 File "<stdin>", line 1, in <module>
499 raise ValueError('new prefix must be longer')
500 ValueError: new prefix must be longer
501 >>> list(ip_network('192.0.2.0/24').subnets(new_prefix=25))
502 [IPv4Network('192.0.2.0/25'), IPv4Network('192.0.2.128/25')]
Nick Coghlan730f67f2012-08-05 22:02:18 +1000503
504 .. method:: supernet(prefixlen_diff=1, new_prefix=None)
505
Nick Coghlan7362c3e2012-08-05 22:32:37 +1000506 The supernet containing this network definition, depending on the
507 argument values. *prefixlen_diff* is the amount our prefix length
508 should be decreased by. *new_prefix* is the desired new prefix of
509 the supernet; it must be smaller than our prefix. One and only one
510 of *prefixlen_diff* and *new_prefix* must be set. Returns a single
511 network object.
Nick Coghlan730f67f2012-08-05 22:02:18 +1000512
Nick Coghlan7362c3e2012-08-05 22:32:37 +1000513 >>> ip_network('192.0.2.0/24').supernet()
514 IPv4Network('192.0.2.0/23')
515 >>> ip_network('192.0.2.0/24').supernet(prefixlen_diff=2)
516 IPv4Network('192.0.0.0/22')
517 >>> ip_network('192.0.2.0/24').supernet(new_prefix=20)
518 IPv4Network('192.0.0.0/20')
Nick Coghlan730f67f2012-08-05 22:02:18 +1000519
520 .. method:: compare_networks(other)
521
Nick Coghlan7362c3e2012-08-05 22:32:37 +1000522 Compare this network to *other*. In this comparison only the network
523 addresses are considered; host bits aren't. Returns either ``-1``,
524 ``0`` or ``1``.
Nick Coghlan730f67f2012-08-05 22:02:18 +1000525
Nick Coghlan7362c3e2012-08-05 22:32:37 +1000526 >>> ip_network('192.0.2.1/32').compare_networks(ip_network('192.0.2.2/32'))
527 -1
528 >>> ip_network('192.0.2.1/32').compare_networks(ip_network('192.0.2.0/32'))
529 1
530 >>> ip_network('192.0.2.1/32').compare_networks(ip_network('192.0.2.1/32'))
531 0
Nick Coghlan9680bdb2012-06-17 17:24:10 +1000532
533
Nick Coghlan9680bdb2012-06-17 17:24:10 +1000534.. class:: IPv6Network(address, strict=True)
535
Nick Coghlan730f67f2012-08-05 22:02:18 +1000536 Construct an IPv6 network definition. *address* can be one of the following:
537
538 1. A string consisting of an IP address and an optional mask, separated by
Nick Coghlan7362c3e2012-08-05 22:32:37 +1000539 a slash (``/``). The IP address is the network address, and the mask
Nick Coghlan730f67f2012-08-05 22:02:18 +1000540 can be either a single number, which means it's a *prefix*, or a string
541 representation of an IPv6 address. If it's the latter, the mask is
542 interpreted as a *net mask*. If no mask is provided, it's considered to
543 be ``/128``.
544
545 For example, the following *address* specifications are equivalent:
546 ``2001:db00::0/24`` and ``2001:db00::0/ffff:ff00::``.
547
548 2. An integer that fits into 128 bits. This is equivalent to a
549 single-address network, with the network address being *address* and
550 the mask being ``/128``.
551
552 3. An integer packed into a :class:`bytes` object of length 16, bit-endian.
553 The interpretation is similar to an integer *address*.
554
555 An :exc:`AddressValueError` is raised if *address* is not a valid IPv6
556 address. A :exc:`NetmaskValueError` is raised if the mask is not valid for
557 an IPv6 address.
Nick Coghlan9680bdb2012-06-17 17:24:10 +1000558
559 If *strict* is ``True`` and host bits are set in the supplied address,
Nick Coghlan730f67f2012-08-05 22:02:18 +1000560 then :exc:`ValueError` is raised. Otherwise, the host bits are masked out
Nick Coghlan9680bdb2012-06-17 17:24:10 +1000561 to determine the appropriate network address.
562
Nick Coghlan7362c3e2012-08-05 22:32:37 +1000563 .. attribute:: version
564 .. attribute:: max_prefixlen
565 .. attribute:: is_multicast
566 .. attribute:: is_private
567 .. attribute:: is_unspecified
568 .. attribute:: is_reserved
569 .. attribute:: is_loopback
570 .. attribute:: is_link_local
571 .. attribute:: network_address
572 .. attribute:: broadcast_address
573 .. attribute:: host mask
574 .. attribute:: with_prefixlen
575 .. attribute:: compressed
576 .. attribute:: exploded
577 .. attribute:: with_netmask
578 .. attribute:: with_hostmask
579 .. attribute:: num_addresses
580 .. attribute:: prefixlen
581 .. method:: hosts()
582 .. method:: overlaps(other)
583 .. method:: address_exclude(network)
584 .. method:: subnets(prefixlen_diff=1, new_prefix=None)
585 .. method:: supernet(prefixlen_diff=1, new_prefix=None)
586 .. method:: compare_networks(other)
Nick Coghlan730f67f2012-08-05 22:02:18 +1000587
Nick Coghlan7362c3e2012-08-05 22:32:37 +1000588 Refer to the corresponding attribute documentation in
589 :class:`IPv4Network`
590
591 .. attribute:: is_site_local
592
593 These attribute is true for the network as a whole if it is true
Terry Jan Reedy0f847642013-03-11 18:34:00 -0400594 for both the network address and the broadcast address
Nick Coghlan730f67f2012-08-05 22:02:18 +1000595
596
597Operators
598^^^^^^^^^
599
600Network objects support some operators. Unless stated otherwise, operators can
601only be applied between compatible objects (i.e. IPv4 with IPv4, IPv6 with
602IPv6).
603
Nick Coghlan7362c3e2012-08-05 22:32:37 +1000604
Nick Coghlan730f67f2012-08-05 22:02:18 +1000605Logical operators
606"""""""""""""""""
607
608Network objects can be compared with the usual set of logical operators,
609similarly to address objects.
610
Nick Coghlan7362c3e2012-08-05 22:32:37 +1000611
Nick Coghlan730f67f2012-08-05 22:02:18 +1000612Iteration
613"""""""""
614
615Network objects can be iterated to list all the addresses belonging to the
616network. For iteration, *all* hosts are returned, including unusable hosts
617(for usable hosts, use the :meth:`~IPv4Network.hosts` method). An
618example::
619
620 >>> for addr in IPv4Network('192.0.2.0/28'):
621 ... addr
622 ...
623 IPv4Address('192.0.2.0')
624 IPv4Address('192.0.2.1')
625 IPv4Address('192.0.2.2')
626 IPv4Address('192.0.2.3')
627 IPv4Address('192.0.2.4')
628 IPv4Address('192.0.2.5')
629 IPv4Address('192.0.2.6')
630 IPv4Address('192.0.2.7')
631 IPv4Address('192.0.2.8')
632 IPv4Address('192.0.2.9')
633 IPv4Address('192.0.2.10')
634 IPv4Address('192.0.2.11')
635 IPv4Address('192.0.2.12')
636 IPv4Address('192.0.2.13')
637 IPv4Address('192.0.2.14')
638 IPv4Address('192.0.2.15')
639
Nick Coghlan7362c3e2012-08-05 22:32:37 +1000640
Nick Coghlan730f67f2012-08-05 22:02:18 +1000641Networks as containers of addresses
642"""""""""""""""""""""""""""""""""""
643
644Network objects can act as containers of addresses. Some examples::
645
646 >>> IPv4Network('192.0.2.0/28')[0]
647 IPv4Address('192.0.2.0')
648 >>> IPv4Network('192.0.2.0/28')[15]
649 IPv4Address('192.0.2.15')
650 >>> IPv4Address('192.0.2.6') in IPv4Network('192.0.2.0/28')
651 True
652 >>> IPv4Address('192.0.3.6') in IPv4Network('192.0.2.0/28')
653 False
Nick Coghlan9680bdb2012-06-17 17:24:10 +1000654
655
Eli Bendersky0e497492012-07-31 17:23:11 +0300656Interface objects
657-----------------
658
659.. class:: IPv4Interface(address)
660
Nick Coghlana8517ad2012-08-20 10:04:26 +1000661 Construct an IPv4 interface. The meaning of *address* is as in the
662 constructor of :class:`IPv4Network`, except that arbitrary host addresses
663 are always accepted.
Eli Bendersky0e497492012-07-31 17:23:11 +0300664
Nick Coghlana8517ad2012-08-20 10:04:26 +1000665 :class:`IPv4Interface` is a subclass of :class:`IPv4Address`, so it inherits
666 all the attributes from that class. In addition, the following attributes
667 are available:
Eli Bendersky0e497492012-07-31 17:23:11 +0300668
Nick Coghlana8517ad2012-08-20 10:04:26 +1000669 .. attribute:: ip
670
671 The address (:class:`IPv4Address`) without network information.
672
673 >>> interface = IPv4Interface('192.0.2.5/24')
674 >>> interface.ip
675 IPv4Address('192.0.2.5')
676
677 .. attribute:: network
678
679 The network (:class:`IPv4Network`) this interface belongs to.
680
681 >>> interface = IPv4Interface('192.0.2.5/24')
682 >>> interface.network
683 IPv4Network('192.0.2.0/24')
684
685 .. attribute:: with_prefixlen
686
687 A string representation of the interface with the mask in prefix notation.
688
689 >>> interface = IPv4Interface('192.0.2.5/24')
690 >>> interface.with_prefixlen
691 '192.0.2.5/24'
692
693 .. attribute:: with_netmask
694
695 A string representation of the interface with the network as a net mask.
696
697 >>> interface = IPv4Interface('192.0.2.5/24')
698 >>> interface.with_netmask
699 '192.0.2.5/255.255.255.0'
700
701 .. attribute:: with_hostmask
702
703 A string representation of the interface with the network as a host mask.
704
705 >>> interface = IPv4Interface('192.0.2.5/24')
706 >>> interface.with_hostmask
707 '192.0.2.5/0.0.0.255'
Eli Bendersky0e497492012-07-31 17:23:11 +0300708
709
710.. class:: IPv6Interface(address)
711
Nick Coghlana8517ad2012-08-20 10:04:26 +1000712 Construct an IPv6 interface. The meaning of *address* is as in the
713 constructor of :class:`IPv6Network`, except that arbitrary host addresses
714 are always accepted.
Eli Bendersky0e497492012-07-31 17:23:11 +0300715
Nick Coghlana8517ad2012-08-20 10:04:26 +1000716 :class:`IPv6Interface` is a subclass of :class:`IPv6Address`, so it inherits
717 all the attributes from that class. In addition, the following attributes
718 are available:
Eli Bendersky0e497492012-07-31 17:23:11 +0300719
Nick Coghlana8517ad2012-08-20 10:04:26 +1000720 .. attribute:: ip
721 .. attribute:: network
722 .. attribute:: with_prefixlen
723 .. attribute:: with_netmask
724 .. attribute:: with_hostmask
725
726 Refer to the corresponding attribute documentation in
727 :class:`IPv4Interface`.
Eli Bendersky0e497492012-07-31 17:23:11 +0300728
729
Nick Coghlan9680bdb2012-06-17 17:24:10 +1000730Other Module Level Functions
731----------------------------
732
733The module also provides the following module level functions:
734
735.. function:: v4_int_to_packed(address)
736
737 Represent an address as 4 packed bytes in network (big-endian) order.
738 *address* is an integer representation of an IPv4 IP address. A
739 :exc:`ValueError` is raised if the integer is negative or too large to be an
740 IPv4 IP address.
741
742 >>> ipaddress.ip_address(3221225985)
743 IPv4Address('192.0.2.1')
744 >>> ipaddress.v4_int_to_packed(3221225985)
745 b'\xc0\x00\x02\x01'
746
747
748.. function:: v6_int_to_packed(address)
749
750 Represent an address as 16 packed bytes in network (big-endian) order.
751 *address* is an integer representation of an IPv6 IP address. A
752 :exc:`ValueError` is raised if the integer is negative or too large to be an
753 IPv6 IP address.
754
755
756.. function:: summarize_address_range(first, last)
757
758 Return an iterator of the summarized network range given the first and last
759 IP addresses. *first* is the first :class:`IPv4Address` or
760 :class:`IPv6Address` in the range and *last* is the last :class:`IPv4Address`
761 or :class:`IPv6Address` in the range. A :exc:`TypeError` is raised if
762 *first* or *last* are not IP addresses or are not of the same version. A
763 :exc:`ValueError` is raised if *last* is not greater than *first* or if
764 *first* address version is not 4 or 6.
765
766 >>> [ipaddr for ipaddr in ipaddress.summarize_address_range(
767 ... ipaddress.IPv4Address('192.0.2.0'),
768 ... ipaddress.IPv4Address('192.0.2.130'))]
769 [IPv4Network('192.0.2.0/25'), IPv4Network('192.0.2.128/31'), IPv4Network('192.0.2.130/32')]
770
771
772.. function:: collapse_addresses(addresses)
773
774 Return an iterator of the collapsed :class:`IPv4Network` or
775 :class:`IPv6Network` objects. *addresses* is an iterator of
776 :class:`IPv4Network` or :class:`IPv6Network` objects. A :exc:`TypeError` is
777 raised if *addresses* contains mixed version objects.
778
779 >>> [ipaddr for ipaddr in
780 ... ipaddress.collapse_addresses([ipaddress.IPv4Network('192.0.2.0/25'),
781 ... ipaddress.IPv4Network('192.0.2.128/25')])]
782 [IPv4Network('192.0.2.0/24')]
783
784
785.. function:: get_mixed_type_key(obj)
786
787 Return a key suitable for sorting between networks and addresses. Address
788 and Network objects are not sortable by default; they're fundamentally
789 different, so the expression::
790
791 IPv4Address('192.0.2.0') <= IPv4Network('192.0.2.0/24')
792
793 doesn't make sense. There are some times however, where you may wish to
794 have :mod:`ipaddress` sort these anyway. If you need to do this, you can use
795 this function as the ``key`` argument to :func:`sorted()`.
796
797 *obj* is either a network or address object.
798
799
800Custom Exceptions
801-----------------
802
803To support more specific error reporting from class constructors, the
804module defines the following exceptions:
805
806.. exception:: AddressValueError(ValueError)
807
808 Any value error related to the address.
809
810
811.. exception:: NetmaskValueError(ValueError)
812
813 Any value error related to the netmask.