blob: 23526b6c4227f61811463fa467ccfed1b3bad150 [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
27
Eli Bendersky0e497492012-07-31 17:23:11 +030028Convenience factory functions
29-----------------------------
Nick Coghlan9680bdb2012-06-17 17:24:10 +100030
Eli Bendersky0e497492012-07-31 17:23:11 +030031The :mod:`ipaddress` module provides factory functions to conveniently create
32IP addresses, networks and interfaces:
Nick Coghlan9680bdb2012-06-17 17:24:10 +100033
34.. function:: ip_address(address)
35
36 Return an :class:`IPv4Address` or :class:`IPv6Address` object depending on
Eli Bendersky0e497492012-07-31 17:23:11 +030037 the IP address passed as argument. Either IPv4 or IPv6 addresses may be
38 supplied; integers less than 2**32 will be considered to be IPv4 by default.
Eli Bendersky948af232012-10-07 07:23:50 -070039 A :exc:`ValueError` is raised if *address* does not represent a valid IPv4
40 or IPv6 address.
41
42.. testsetup::
43 >>> import ipaddress
44 >>> from ipaddress import (ip_network, IPv4Address, IPv4Interface,
45 ... IPv4Network)
46
47::
Nick Coghlan9680bdb2012-06-17 17:24:10 +100048
49 >>> ipaddress.ip_address('192.168.0.1')
50 IPv4Address('192.168.0.1')
51 >>> ipaddress.ip_address('2001:db8::')
52 IPv6Address('2001:db8::')
53
54
55.. function:: ip_network(address, strict=True)
56
57 Return an :class:`IPv4Network` or :class:`IPv6Network` object depending on
58 the IP address passed as argument. *address* is a string or integer
59 representing the IP network. Either IPv4 or IPv6 networks may be supplied;
60 integers less than 2**32 will be considered to be IPv4 by default. *strict*
61 is passed to :class:`IPv4Network` or :class:`IPv6Network` constructor. A
Eli Bendersky0e497492012-07-31 17:23:11 +030062 :exc:`ValueError` is raised if *address* does not represent a valid IPv4 or
63 IPv6 address, or if the network has host bits set.
Nick Coghlan9680bdb2012-06-17 17:24:10 +100064
65 >>> ipaddress.ip_network('192.168.0.0/28')
66 IPv4Network('192.168.0.0/28')
67
68
69.. function:: ip_interface(address)
70
71 Return an :class:`IPv4Interface` or :class:`IPv6Interface` object depending
72 on the IP address passed as argument. *address* is a string or integer
73 representing the IP address. Either IPv4 or IPv6 addresses may be supplied;
Eli Bendersky0e497492012-07-31 17:23:11 +030074 integers less than 2**32 will be considered to be IPv4 by default. A
75 :exc:`ValueError` is raised if *address* does not represent a valid IPv4 or
Nick Coghlan9680bdb2012-06-17 17:24:10 +100076 IPv6 address.
77
Nick Coghlan730f67f2012-08-05 22:02:18 +100078One downside of these convenience functions is that the need to handle both
79IPv4 and IPv6 formats means that error messages provide minimal
80information on the precise error, as the functions don't know whether the
81IPv4 or IPv6 format was intended. More detailed error reporting can be
82obtained by calling the appropriate version specific class constructors
83directly.
84
85
86IP Addresses
87------------
Nick Coghlan9680bdb2012-06-17 17:24:10 +100088
Eli Bendersky0e497492012-07-31 17:23:11 +030089Address objects
Nick Coghlan730f67f2012-08-05 22:02:18 +100090^^^^^^^^^^^^^^^
Nick Coghlan9680bdb2012-06-17 17:24:10 +100091
Eli Bendersky0e497492012-07-31 17:23:11 +030092The :class:`IPv4Address` and :class:`IPv6Address` objects share a lot of common
93attributes. Some attributes that are only meaningful for IPv6 addresses are
94also implemented by :class:`IPv4Address` objects, in order to make it easier to
Nick Coghlan730f67f2012-08-05 22:02:18 +100095write code that handles both IP versions correctly.
Nick Coghlan9680bdb2012-06-17 17:24:10 +100096
97.. class:: IPv4Address(address)
98
Eli Bendersky0e497492012-07-31 17:23:11 +030099 Construct an IPv4 address. An :exc:`AddressValueError` is raised if
100 *address* is not a valid IPv4 address.
101
102 The following constitutes a valid IPv4 address:
103
104 1. A string in decimal-dot notation, consisting of four decimal integers in
105 the inclusive range 0-255, separated by dots (e.g. ``192.168.0.1``). Each
Nick Coghlan730f67f2012-08-05 22:02:18 +1000106 integer represents an octet (byte) in the address. Leading zeroes are
Donald Stufft8b852f12014-05-20 12:58:38 -0400107 tolerated only for values less than 8 (as there is no ambiguity
Nick Coghlan730f67f2012-08-05 22:02:18 +1000108 between the decimal and octal interpretations of such strings).
Eli Bendersky0e497492012-07-31 17:23:11 +0300109 2. An integer that fits into 32 bits.
Nick Coghlan730f67f2012-08-05 22:02:18 +1000110 3. An integer packed into a :class:`bytes` object of length 4 (most
111 significant octet first).
Nick Coghlan9680bdb2012-06-17 17:24:10 +1000112
113 >>> ipaddress.IPv4Address('192.168.0.1')
114 IPv4Address('192.168.0.1')
Eli Bendersky948af232012-10-07 07:23:50 -0700115 >>> ipaddress.IPv4Address(3232235521)
Nick Coghlan730f67f2012-08-05 22:02:18 +1000116 IPv4Address('192.168.0.1')
117 >>> ipaddress.IPv4Address(b'\xC0\xA8\x00\x01')
118 IPv4Address('192.168.0.1')
Eli Bendersky0e497492012-07-31 17:23:11 +0300119
120 .. attribute:: version
121
Nick Coghlan730f67f2012-08-05 22:02:18 +1000122 The appropriate version number: ``4`` for IPv4, ``6`` for IPv6.
Eli Bendersky0e497492012-07-31 17:23:11 +0300123
124 .. attribute:: max_prefixlen
125
Nick Coghlan730f67f2012-08-05 22:02:18 +1000126 The total number of bits in the address representation for this
127 version: ``32`` for IPv4, ``128`` for IPv6.
128
129 The prefix defines the number of leading bits in an address that
130 are compared to determine whether or not an address is part of a
131 network.
132
133 .. attribute:: compressed
134 .. attribute:: exploded
135
136 The string representation in dotted decimal notation. Leading zeroes
137 are never included in the representation.
138
139 As IPv4 does not define a shorthand notation for addresses with octets
140 set to zero, these two attributes are always the same as ``str(addr)``
141 for IPv4 addresses. Exposing these attributes makes it easier to
142 write display code that can handle both IPv4 and IPv6 addresses.
143
144 .. attribute:: packed
145
146 The binary representation of this address - a :class:`bytes` object of
147 the appropriate length (most significant octet first). This is 4 bytes
148 for IPv4 and 16 bytes for IPv6.
Eli Bendersky0e497492012-07-31 17:23:11 +0300149
Eric V. Smithebdaaf42014-04-14 12:58:07 -0400150 .. attribute:: reverse_pointer
151
152 The name of the reverse DNS PTR record for the IP address, e.g.::
153
154 >>> ipaddress.ip_address("127.0.0.1").reverse_pointer
155 '1.0.0.127.in-addr.arpa'
156 >>> ipaddress.ip_address("2001:db8::1").reverse_pointer
157 '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'
158
159 This is the name that could be used for performing a PTR lookup, not the
160 resolved hostname itself.
161
162 .. versionadded:: 3.5
163
Eli Bendersky0e497492012-07-31 17:23:11 +0300164 .. attribute:: is_multicast
165
Nick Coghlan730f67f2012-08-05 22:02:18 +1000166 ``True`` if the address is reserved for multicast use. See
167 :RFC:`3171` (for IPv4) or :RFC:`2373` (for IPv6).
Eli Bendersky0e497492012-07-31 17:23:11 +0300168
Peter Moodye5019d52013-10-24 09:47:10 -0700169 .. attribute:: is_private
170
171 ``True`` if the address is allocated for private networks. See
R David Murray6674ac02014-03-06 11:51:37 -0500172 iana-ipv4-special-registry_ (for IPv4) or iana-ipv6-special-registry_
Peter Moodye5019d52013-10-24 09:47:10 -0700173 (for IPv6).
174
Peter Moody8ed30c12013-10-21 16:16:51 -0700175 .. attribute:: is_global
Eli Bendersky0e497492012-07-31 17:23:11 +0300176
Peter Moodybe9c1b12013-10-22 12:36:21 -0700177 ``True`` if the address is allocated for public networks. See
R David Murray6674ac02014-03-06 11:51:37 -0500178 iana-ipv4-special-registry_ (for IPv4) or iana-ipv6-special-registry_
Peter Moody8ed30c12013-10-21 16:16:51 -0700179 (for IPv6).
180
R David Murray6674ac02014-03-06 11:51:37 -0500181 .. versionadded:: 3.4
Eli Bendersky0e497492012-07-31 17:23:11 +0300182
183 .. attribute:: is_unspecified
184
Andrew Kuchlinge5235f12014-02-15 17:11:06 -0500185 ``True`` if the address is unspecified. See :RFC:`5735` (for IPv4)
Nick Coghlan730f67f2012-08-05 22:02:18 +1000186 or :RFC:`2373` (for IPv6).
Eli Bendersky0e497492012-07-31 17:23:11 +0300187
188 .. attribute:: is_reserved
189
Nick Coghlan730f67f2012-08-05 22:02:18 +1000190 ``True`` if the address is otherwise IETF reserved.
Eli Bendersky0e497492012-07-31 17:23:11 +0300191
192 .. attribute:: is_loopback
193
Nick Coghlan730f67f2012-08-05 22:02:18 +1000194 ``True`` if this is a loopback address. See :RFC:`3330` (for IPv4)
195 or :RFC:`2373` (for IPv6).
Eli Bendersky0e497492012-07-31 17:23:11 +0300196
197 .. attribute:: is_link_local
198
Nick Coghlan730f67f2012-08-05 22:02:18 +1000199 ``True`` if the address is reserved for link-local usage. See
200 :RFC:`3927`.
201
Serhiy Storchaka6dff0202016-05-07 10:49:07 +0300202.. _iana-ipv4-special-registry: https://www.iana.org/assignments/iana-ipv4-special-registry/iana-ipv4-special-registry.xhtml
203.. _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 -0500204
Eli Bendersky0e497492012-07-31 17:23:11 +0300205
206.. class:: IPv6Address(address)
207
208 Construct an IPv6 address. An :exc:`AddressValueError` is raised if
209 *address* is not a valid IPv6 address.
210
211 The following constitutes a valid IPv6 address:
212
213 1. A string consisting of eight groups of four hexadecimal digits, each
214 group representing 16 bits. The groups are separated by colons.
215 This describes an *exploded* (longhand) notation. The string can
216 also be *compressed* (shorthand notation) by various means. See
217 :RFC:`4291` for details. For example,
218 ``"0000:0000:0000:0000:0000:0abc:0007:0def"`` can be compressed to
219 ``"::abc:7:def"``.
220 2. An integer that fits into 128 bits.
221 3. An integer packed into a :class:`bytes` object of length 16, big-endian.
222
223 >>> ipaddress.IPv6Address('2001:db8::1000')
224 IPv6Address('2001:db8::1000')
225
Nick Coghlan730f67f2012-08-05 22:02:18 +1000226 .. attribute:: compressed
227
228 The short form of the address representation, with leading zeroes in
229 groups omitted and the longest sequence of groups consisting entirely of
230 zeroes collapsed to a single empty group.
231
232 This is also the value returned by ``str(addr)`` for IPv6 addresses.
233
234 .. attribute:: exploded
235
236 The long form of the address representation, with all leading zeroes and
237 groups consisting entirely of zeroes included.
238
R David Murray6674ac02014-03-06 11:51:37 -0500239
240 For the following attributes, see the corresponding documention of the
241 :class:`IPv4Address` class:
242
Nick Coghlan730f67f2012-08-05 22:02:18 +1000243 .. attribute:: packed
Eric V. Smithebdaaf42014-04-14 12:58:07 -0400244 .. attribute:: reverse_pointer
Nick Coghlan730f67f2012-08-05 22:02:18 +1000245 .. attribute:: version
246 .. attribute:: max_prefixlen
247 .. attribute:: is_multicast
248 .. attribute:: is_private
R David Murray6674ac02014-03-06 11:51:37 -0500249 .. attribute:: is_global
Nick Coghlan730f67f2012-08-05 22:02:18 +1000250 .. attribute:: is_unspecified
251 .. attribute:: is_reserved
252 .. attribute:: is_loopback
253 .. attribute:: is_link_local
254
R David Murray6674ac02014-03-06 11:51:37 -0500255 .. versionadded:: 3.4
256 is_global
Eli Bendersky0e497492012-07-31 17:23:11 +0300257
258 .. attribute:: is_site_local
259
Nick Coghlan730f67f2012-08-05 22:02:18 +1000260 ``True`` if the address is reserved for site-local usage. Note that
261 the site-local address space has been deprecated by :RFC:`3879`. Use
262 :attr:`~IPv4Address.is_private` to test if this address is in the
263 space of unique local addresses as defined by :RFC:`4193`.
Eli Bendersky0e497492012-07-31 17:23:11 +0300264
265 .. attribute:: ipv4_mapped
266
Nick Coghlan730f67f2012-08-05 22:02:18 +1000267 For addresses that appear to be IPv4 mapped addresses (starting with
268 ``::FFFF/96``), this property will report the embedded IPv4 address.
269 For any other address, this property will be ``None``.
Eli Bendersky0e497492012-07-31 17:23:11 +0300270
271 .. attribute:: sixtofour
272
Nick Coghlan730f67f2012-08-05 22:02:18 +1000273 For addresses that appear to be 6to4 addresses (starting with
274 ``2002::/16``) as defined by :RFC:`3056`, this property will report
275 the embedded IPv4 address. For any other address, this property will
276 be ``None``.
277
278 .. attribute:: teredo
279
280 For addresses that appear to be Teredo addresses (starting with
281 ``2001::/32``) as defined by :RFC:`4380`, this property will report
282 the embedded ``(server, client)`` IP address pair. For any other
283 address, this property will be ``None``.
284
285
286Conversion to Strings and Integers
287^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
288
289To interoperate with networking interfaces such as the socket module,
290addresses must be converted to strings or integers. This is handled using
291the :func:`str` and :func:`int` builtin functions::
292
293 >>> str(ipaddress.IPv4Address('192.168.0.1'))
294 '192.168.0.1'
295 >>> int(ipaddress.IPv4Address('192.168.0.1'))
296 3232235521
297 >>> str(ipaddress.IPv6Address('::1'))
298 '::1'
299 >>> int(ipaddress.IPv6Address('::1'))
300 1
Eli Bendersky0e497492012-07-31 17:23:11 +0300301
302
303Operators
304^^^^^^^^^
305
306Address objects support some operators. Unless stated otherwise, operators can
307only be applied between compatible objects (i.e. IPv4 with IPv4, IPv6 with
308IPv6).
309
Nick Coghlan730f67f2012-08-05 22:02:18 +1000310
Georg Brandl9ad417e2013-10-06 19:23:57 +0200311Comparison operators
312""""""""""""""""""""
Eli Bendersky0e497492012-07-31 17:23:11 +0300313
Georg Brandl9ad417e2013-10-06 19:23:57 +0200314Address objects can be compared with the usual set of comparison operators. Some
Eli Bendersky0e497492012-07-31 17:23:11 +0300315examples::
316
317 >>> IPv4Address('127.0.0.2') > IPv4Address('127.0.0.1')
318 True
319 >>> IPv4Address('127.0.0.2') == IPv4Address('127.0.0.1')
320 False
321 >>> IPv4Address('127.0.0.2') != IPv4Address('127.0.0.1')
322 True
323
Nick Coghlan730f67f2012-08-05 22:02:18 +1000324
Eli Bendersky0e497492012-07-31 17:23:11 +0300325Arithmetic operators
326""""""""""""""""""""
327
328Integers can be added to or subtracted from address objects. Some examples::
329
330 >>> IPv4Address('127.0.0.2') + 3
331 IPv4Address('127.0.0.5')
332 >>> IPv4Address('127.0.0.2') - 3
333 IPv4Address('126.255.255.255')
334 >>> IPv4Address('255.255.255.255') + 1
335 Traceback (most recent call last):
336 File "<stdin>", line 1, in <module>
337 ipaddress.AddressValueError: 4294967296 (>= 2**32) is not permitted as an IPv4 address
338
339
Nick Coghlan730f67f2012-08-05 22:02:18 +1000340IP Network definitions
341----------------------
342
343The :class:`IPv4Network` and :class:`IPv6Network` objects provide a mechanism
344for defining and inspecting IP network definitions. A network definition
345consists of a *mask* and a *network address*, and as such defines a range of
346IP addresses that equal the network address when masked (binary AND) with the
347mask. For example, a network definition with the mask ``255.255.255.0`` and
348the network address ``192.168.1.0`` consists of IP addresses in the inclusive
349range ``192.168.1.0`` to ``192.168.1.255``.
350
351
352Prefix, net mask and host mask
353^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
354
355There are several equivalent ways to specify IP network masks. A *prefix*
356``/<nbits>`` is a notation that denotes how many high-order bits are set in
357the network mask. A *net mask* is an IP address with some number of
358high-order bits set. Thus the prefix ``/24`` is equivalent to the net mask
359``255.255.255.0`` in IPv4, or ``ffff:ff00::`` in IPv6. In addition, a
360*host mask* is the logical inverse of a *net mask*, and is sometimes used
361(for example in Cisco access control lists) to denote a network mask. The
362host mask equivalent to ``/24`` in IPv4 is ``0.0.0.255``.
363
364
Eli Bendersky0e497492012-07-31 17:23:11 +0300365Network objects
Nick Coghlan730f67f2012-08-05 22:02:18 +1000366^^^^^^^^^^^^^^^
367
368All attributes implemented by address objects are implemented by network
369objects as well. In addition, network objects implement additional attributes.
370All of these are common between :class:`IPv4Network` and :class:`IPv6Network`,
371so to avoid duplication they are only documented for :class:`IPv4Network`.
Nick Coghlan9680bdb2012-06-17 17:24:10 +1000372
373.. class:: IPv4Network(address, strict=True)
374
Nick Coghlan730f67f2012-08-05 22:02:18 +1000375 Construct an IPv4 network definition. *address* can be one of the following:
376
377 1. A string consisting of an IP address and an optional mask, separated by
378 a slash (``/``). The IP address is the network address, and the mask
379 can be either a single number, which means it's a *prefix*, or a string
380 representation of an IPv4 address. If it's the latter, the mask is
381 interpreted as a *net mask* if it starts with a non-zero field, or as
382 a *host mask* if it starts with a zero field. If no mask is provided,
383 it's considered to be ``/32``.
384
385 For example, the following *address* specifications are equivalent:
386 ``192.168.1.0/24``, ``192.168.1.0/255.255.255.0`` and
387 ``192.168.1.0/0.0.0.255``.
388
389 2. An integer that fits into 32 bits. This is equivalent to a
390 single-address network, with the network address being *address* and
391 the mask being ``/32``.
392
393 3. An integer packed into a :class:`bytes` object of length 4, big-endian.
394 The interpretation is similar to an integer *address*.
395
Antoine Pitrou5fb195f2014-05-12 20:36:46 +0200396 4. A two-tuple of an address description and a netmask, where the address
397 description is either a string, a 32-bits integer, a 4-bytes packed
398 integer, or an existing IPv4Address object; and the netmask is either
399 an integer representing the prefix length (e.g. ``24``) or a string
400 representing the prefix mask (e.g. ``255.255.255.0``).
401
Nick Coghlan730f67f2012-08-05 22:02:18 +1000402 An :exc:`AddressValueError` is raised if *address* is not a valid IPv4
403 address. A :exc:`NetmaskValueError` is raised if the mask is not valid for
404 an IPv4 address.
Nick Coghlan9680bdb2012-06-17 17:24:10 +1000405
406 If *strict* is ``True`` and host bits are set in the supplied address,
Nick Coghlan730f67f2012-08-05 22:02:18 +1000407 then :exc:`ValueError` is raised. Otherwise, the host bits are masked out
Nick Coghlan9680bdb2012-06-17 17:24:10 +1000408 to determine the appropriate network address.
409
Nick Coghlan7362c3e2012-08-05 22:32:37 +1000410 Unless stated otherwise, all network methods accepting other network/address
411 objects will raise :exc:`TypeError` if the argument's IP version is
412 incompatible to ``self``
413
Antoine Pitrou5fb195f2014-05-12 20:36:46 +0200414 .. versionchanged:: 3.5
415
416 Added the two-tuple form for the *address* constructor parameter.
417
Nick Coghlan7362c3e2012-08-05 22:32:37 +1000418 .. attribute:: version
419 .. attribute:: max_prefixlen
420
421 Refer to the corresponding attribute documentation in
422 :class:`IPv4Address`
423
424 .. attribute:: is_multicast
425 .. attribute:: is_private
426 .. attribute:: is_unspecified
427 .. attribute:: is_reserved
428 .. attribute:: is_loopback
429 .. attribute:: is_link_local
430
431 These attributes are true for the network as a whole if they are true
Terry Jan Reedy0f847642013-03-11 18:34:00 -0400432 for both the network address and the broadcast address
Nick Coghlan7362c3e2012-08-05 22:32:37 +1000433
434 .. attribute:: network_address
435
Nick Coghlan31096a92012-08-05 22:52:38 +1000436 The network address for the network. The network address and the
437 prefix length together uniquely define a network.
Nick Coghlan730f67f2012-08-05 22:02:18 +1000438
439 .. attribute:: broadcast_address
440
Nick Coghlan31096a92012-08-05 22:52:38 +1000441 The broadcast address for the network. Packets sent to the broadcast
442 address should be received by every host on the network.
Nick Coghlan730f67f2012-08-05 22:02:18 +1000443
Zachary Ware9774ce02014-01-14 09:09:48 -0600444 .. attribute:: hostmask
Nick Coghlan730f67f2012-08-05 22:02:18 +1000445
Nick Coghlan7362c3e2012-08-05 22:32:37 +1000446 The host mask, as a string.
Nick Coghlan730f67f2012-08-05 22:02:18 +1000447
448 .. attribute:: with_prefixlen
Nick Coghlan7362c3e2012-08-05 22:32:37 +1000449 .. attribute:: compressed
450 .. attribute:: exploded
Nick Coghlan730f67f2012-08-05 22:02:18 +1000451
Nick Coghlan7362c3e2012-08-05 22:32:37 +1000452 A string representation of the network, with the mask in prefix
453 notation.
454
455 ``with_prefixlen`` and ``compressed`` are always the same as
456 ``str(network)``.
457 ``exploded`` uses the exploded form the network address.
Nick Coghlan730f67f2012-08-05 22:02:18 +1000458
459 .. attribute:: with_netmask
460
Nick Coghlan7362c3e2012-08-05 22:32:37 +1000461 A string representation of the network, with the mask in net mask
462 notation.
Nick Coghlan730f67f2012-08-05 22:02:18 +1000463
464 .. attribute:: with_hostmask
465
Nick Coghlan7362c3e2012-08-05 22:32:37 +1000466 A string representation of the network, with the mask in host mask
467 notation.
Nick Coghlan730f67f2012-08-05 22:02:18 +1000468
469 .. attribute:: num_addresses
470
Nick Coghlan7362c3e2012-08-05 22:32:37 +1000471 The total number of addresses in the network.
Nick Coghlan730f67f2012-08-05 22:02:18 +1000472
473 .. attribute:: prefixlen
474
Nick Coghlan7362c3e2012-08-05 22:32:37 +1000475 Length of the network prefix, in bits.
Nick Coghlan730f67f2012-08-05 22:02:18 +1000476
477 .. method:: hosts()
478
Nick Coghlan7362c3e2012-08-05 22:32:37 +1000479 Returns an iterator over the usable hosts in the network. The usable
480 hosts are all the IP addresses that belong to the network, except the
481 network address itself and the network broadcast address.
Nick Coghlan730f67f2012-08-05 22:02:18 +1000482
Eli Bendersky948af232012-10-07 07:23:50 -0700483 >>> list(ip_network('192.0.2.0/29').hosts()) #doctest: +NORMALIZE_WHITESPACE
Nick Coghlan7362c3e2012-08-05 22:32:37 +1000484 [IPv4Address('192.0.2.1'), IPv4Address('192.0.2.2'),
485 IPv4Address('192.0.2.3'), IPv4Address('192.0.2.4'),
486 IPv4Address('192.0.2.5'), IPv4Address('192.0.2.6')]
Nick Coghlan730f67f2012-08-05 22:02:18 +1000487
488 .. method:: overlaps(other)
489
Nick Coghlan7362c3e2012-08-05 22:32:37 +1000490 ``True`` if this network is partly or wholly contained in *other* or
Terry Jan Reedy0f847642013-03-11 18:34:00 -0400491 *other* is wholly contained in this network.
Nick Coghlan730f67f2012-08-05 22:02:18 +1000492
493 .. method:: address_exclude(network)
494
Nick Coghlan7362c3e2012-08-05 22:32:37 +1000495 Computes the network definitions resulting from removing the given
496 *network* from this one. Returns an iterator of network objects.
497 Raises :exc:`ValueError` if *network* is not completely contained in
498 this network.
Nick Coghlan730f67f2012-08-05 22:02:18 +1000499
Nick Coghlan7362c3e2012-08-05 22:32:37 +1000500 >>> n1 = ip_network('192.0.2.0/28')
501 >>> n2 = ip_network('192.0.2.1/32')
Eli Bendersky948af232012-10-07 07:23:50 -0700502 >>> list(n1.address_exclude(n2)) #doctest: +NORMALIZE_WHITESPACE
Nick Coghlan7362c3e2012-08-05 22:32:37 +1000503 [IPv4Network('192.0.2.8/29'), IPv4Network('192.0.2.4/30'),
504 IPv4Network('192.0.2.2/31'), IPv4Network('192.0.2.0/32')]
Nick Coghlan730f67f2012-08-05 22:02:18 +1000505
506 .. method:: subnets(prefixlen_diff=1, new_prefix=None)
507
Nick Coghlan7362c3e2012-08-05 22:32:37 +1000508 The subnets that join to make the current network definition, depending
509 on the argument values. *prefixlen_diff* is the amount our prefix
510 length should be increased by. *new_prefix* is the desired new
511 prefix of the subnets; it must be larger than our prefix. One and
512 only one of *prefixlen_diff* and *new_prefix* must be set. Returns an
513 iterator of network objects.
Nick Coghlan730f67f2012-08-05 22:02:18 +1000514
Nick Coghlan7362c3e2012-08-05 22:32:37 +1000515 >>> list(ip_network('192.0.2.0/24').subnets())
516 [IPv4Network('192.0.2.0/25'), IPv4Network('192.0.2.128/25')]
Eli Bendersky948af232012-10-07 07:23:50 -0700517 >>> list(ip_network('192.0.2.0/24').subnets(prefixlen_diff=2)) #doctest: +NORMALIZE_WHITESPACE
Nick Coghlan7362c3e2012-08-05 22:32:37 +1000518 [IPv4Network('192.0.2.0/26'), IPv4Network('192.0.2.64/26'),
519 IPv4Network('192.0.2.128/26'), IPv4Network('192.0.2.192/26')]
Eli Bendersky948af232012-10-07 07:23:50 -0700520 >>> list(ip_network('192.0.2.0/24').subnets(new_prefix=26)) #doctest: +NORMALIZE_WHITESPACE
Nick Coghlan7362c3e2012-08-05 22:32:37 +1000521 [IPv4Network('192.0.2.0/26'), IPv4Network('192.0.2.64/26'),
522 IPv4Network('192.0.2.128/26'), IPv4Network('192.0.2.192/26')]
523 >>> list(ip_network('192.0.2.0/24').subnets(new_prefix=23))
524 Traceback (most recent call last):
525 File "<stdin>", line 1, in <module>
526 raise ValueError('new prefix must be longer')
527 ValueError: new prefix must be longer
528 >>> list(ip_network('192.0.2.0/24').subnets(new_prefix=25))
529 [IPv4Network('192.0.2.0/25'), IPv4Network('192.0.2.128/25')]
Nick Coghlan730f67f2012-08-05 22:02:18 +1000530
531 .. method:: supernet(prefixlen_diff=1, new_prefix=None)
532
Nick Coghlan7362c3e2012-08-05 22:32:37 +1000533 The supernet containing this network definition, depending on the
534 argument values. *prefixlen_diff* is the amount our prefix length
535 should be decreased by. *new_prefix* is the desired new prefix of
536 the supernet; it must be smaller than our prefix. One and only one
537 of *prefixlen_diff* and *new_prefix* must be set. Returns a single
538 network object.
Nick Coghlan730f67f2012-08-05 22:02:18 +1000539
Nick Coghlan7362c3e2012-08-05 22:32:37 +1000540 >>> ip_network('192.0.2.0/24').supernet()
541 IPv4Network('192.0.2.0/23')
542 >>> ip_network('192.0.2.0/24').supernet(prefixlen_diff=2)
543 IPv4Network('192.0.0.0/22')
544 >>> ip_network('192.0.2.0/24').supernet(new_prefix=20)
545 IPv4Network('192.0.0.0/20')
Nick Coghlan730f67f2012-08-05 22:02:18 +1000546
547 .. method:: compare_networks(other)
548
Nick Coghlan7362c3e2012-08-05 22:32:37 +1000549 Compare this network to *other*. In this comparison only the network
550 addresses are considered; host bits aren't. Returns either ``-1``,
551 ``0`` or ``1``.
Nick Coghlan730f67f2012-08-05 22:02:18 +1000552
Nick Coghlan7362c3e2012-08-05 22:32:37 +1000553 >>> ip_network('192.0.2.1/32').compare_networks(ip_network('192.0.2.2/32'))
554 -1
555 >>> ip_network('192.0.2.1/32').compare_networks(ip_network('192.0.2.0/32'))
556 1
557 >>> ip_network('192.0.2.1/32').compare_networks(ip_network('192.0.2.1/32'))
558 0
Nick Coghlan9680bdb2012-06-17 17:24:10 +1000559
560
Nick Coghlan9680bdb2012-06-17 17:24:10 +1000561.. class:: IPv6Network(address, strict=True)
562
Nick Coghlan730f67f2012-08-05 22:02:18 +1000563 Construct an IPv6 network definition. *address* can be one of the following:
564
565 1. A string consisting of an IP address and an optional mask, separated by
Nick Coghlan7362c3e2012-08-05 22:32:37 +1000566 a slash (``/``). The IP address is the network address, and the mask
Nick Coghlan730f67f2012-08-05 22:02:18 +1000567 can be either a single number, which means it's a *prefix*, or a string
568 representation of an IPv6 address. If it's the latter, the mask is
569 interpreted as a *net mask*. If no mask is provided, it's considered to
570 be ``/128``.
571
572 For example, the following *address* specifications are equivalent:
573 ``2001:db00::0/24`` and ``2001:db00::0/ffff:ff00::``.
574
575 2. An integer that fits into 128 bits. This is equivalent to a
576 single-address network, with the network address being *address* and
577 the mask being ``/128``.
578
Benjamin Peterson0612ffe2015-08-30 14:42:38 -0700579 3. An integer packed into a :class:`bytes` object of length 16, big-endian.
Nick Coghlan730f67f2012-08-05 22:02:18 +1000580 The interpretation is similar to an integer *address*.
581
Antoine Pitrou5fb195f2014-05-12 20:36:46 +0200582 4. A two-tuple of an address description and a netmask, where the address
583 description is either a string, a 128-bits integer, a 16-bytes packed
Berker Peksag420e4d82016-06-10 14:26:07 +0300584 integer, or an existing IPv6Address object; and the netmask is an
Antoine Pitrou5fb195f2014-05-12 20:36:46 +0200585 integer representing the prefix length.
586
Nick Coghlan730f67f2012-08-05 22:02:18 +1000587 An :exc:`AddressValueError` is raised if *address* is not a valid IPv6
588 address. A :exc:`NetmaskValueError` is raised if the mask is not valid for
589 an IPv6 address.
Nick Coghlan9680bdb2012-06-17 17:24:10 +1000590
591 If *strict* is ``True`` and host bits are set in the supplied address,
Nick Coghlan730f67f2012-08-05 22:02:18 +1000592 then :exc:`ValueError` is raised. Otherwise, the host bits are masked out
Nick Coghlan9680bdb2012-06-17 17:24:10 +1000593 to determine the appropriate network address.
594
Antoine Pitrou5fb195f2014-05-12 20:36:46 +0200595 .. versionchanged:: 3.5
596
597 Added the two-tuple form for the *address* constructor parameter.
598
Nick Coghlan7362c3e2012-08-05 22:32:37 +1000599 .. attribute:: version
600 .. attribute:: max_prefixlen
601 .. attribute:: is_multicast
602 .. attribute:: is_private
603 .. attribute:: is_unspecified
604 .. attribute:: is_reserved
605 .. attribute:: is_loopback
606 .. attribute:: is_link_local
607 .. attribute:: network_address
608 .. attribute:: broadcast_address
Zachary Ware9774ce02014-01-14 09:09:48 -0600609 .. attribute:: hostmask
Nick Coghlan7362c3e2012-08-05 22:32:37 +1000610 .. attribute:: with_prefixlen
611 .. attribute:: compressed
612 .. attribute:: exploded
613 .. attribute:: with_netmask
614 .. attribute:: with_hostmask
615 .. attribute:: num_addresses
616 .. attribute:: prefixlen
617 .. method:: hosts()
618 .. method:: overlaps(other)
619 .. method:: address_exclude(network)
620 .. method:: subnets(prefixlen_diff=1, new_prefix=None)
621 .. method:: supernet(prefixlen_diff=1, new_prefix=None)
622 .. method:: compare_networks(other)
Nick Coghlan730f67f2012-08-05 22:02:18 +1000623
Nick Coghlan7362c3e2012-08-05 22:32:37 +1000624 Refer to the corresponding attribute documentation in
625 :class:`IPv4Network`
626
627 .. attribute:: is_site_local
628
629 These attribute is true for the network as a whole if it is true
Terry Jan Reedy0f847642013-03-11 18:34:00 -0400630 for both the network address and the broadcast address
Nick Coghlan730f67f2012-08-05 22:02:18 +1000631
632
633Operators
634^^^^^^^^^
635
636Network objects support some operators. Unless stated otherwise, operators can
637only be applied between compatible objects (i.e. IPv4 with IPv4, IPv6 with
638IPv6).
639
Nick Coghlan7362c3e2012-08-05 22:32:37 +1000640
Nick Coghlan730f67f2012-08-05 22:02:18 +1000641Logical operators
642"""""""""""""""""
643
644Network objects can be compared with the usual set of logical operators,
645similarly to address objects.
646
Nick Coghlan7362c3e2012-08-05 22:32:37 +1000647
Nick Coghlan730f67f2012-08-05 22:02:18 +1000648Iteration
649"""""""""
650
651Network objects can be iterated to list all the addresses belonging to the
652network. For iteration, *all* hosts are returned, including unusable hosts
653(for usable hosts, use the :meth:`~IPv4Network.hosts` method). An
654example::
655
656 >>> for addr in IPv4Network('192.0.2.0/28'):
Serhiy Storchakadba90392016-05-10 12:01:23 +0300657 ... addr
Nick Coghlan730f67f2012-08-05 22:02:18 +1000658 ...
659 IPv4Address('192.0.2.0')
660 IPv4Address('192.0.2.1')
661 IPv4Address('192.0.2.2')
662 IPv4Address('192.0.2.3')
663 IPv4Address('192.0.2.4')
664 IPv4Address('192.0.2.5')
665 IPv4Address('192.0.2.6')
666 IPv4Address('192.0.2.7')
667 IPv4Address('192.0.2.8')
668 IPv4Address('192.0.2.9')
669 IPv4Address('192.0.2.10')
670 IPv4Address('192.0.2.11')
671 IPv4Address('192.0.2.12')
672 IPv4Address('192.0.2.13')
673 IPv4Address('192.0.2.14')
674 IPv4Address('192.0.2.15')
675
Nick Coghlan7362c3e2012-08-05 22:32:37 +1000676
Nick Coghlan730f67f2012-08-05 22:02:18 +1000677Networks as containers of addresses
678"""""""""""""""""""""""""""""""""""
679
680Network objects can act as containers of addresses. Some examples::
681
682 >>> IPv4Network('192.0.2.0/28')[0]
683 IPv4Address('192.0.2.0')
684 >>> IPv4Network('192.0.2.0/28')[15]
685 IPv4Address('192.0.2.15')
686 >>> IPv4Address('192.0.2.6') in IPv4Network('192.0.2.0/28')
687 True
688 >>> IPv4Address('192.0.3.6') in IPv4Network('192.0.2.0/28')
689 False
Nick Coghlan9680bdb2012-06-17 17:24:10 +1000690
691
Eli Bendersky0e497492012-07-31 17:23:11 +0300692Interface objects
693-----------------
694
695.. class:: IPv4Interface(address)
696
Nick Coghlana8517ad2012-08-20 10:04:26 +1000697 Construct an IPv4 interface. The meaning of *address* is as in the
698 constructor of :class:`IPv4Network`, except that arbitrary host addresses
699 are always accepted.
Eli Bendersky0e497492012-07-31 17:23:11 +0300700
Nick Coghlana8517ad2012-08-20 10:04:26 +1000701 :class:`IPv4Interface` is a subclass of :class:`IPv4Address`, so it inherits
702 all the attributes from that class. In addition, the following attributes
703 are available:
Eli Bendersky0e497492012-07-31 17:23:11 +0300704
Nick Coghlana8517ad2012-08-20 10:04:26 +1000705 .. attribute:: ip
706
707 The address (:class:`IPv4Address`) without network information.
708
709 >>> interface = IPv4Interface('192.0.2.5/24')
710 >>> interface.ip
711 IPv4Address('192.0.2.5')
712
713 .. attribute:: network
714
715 The network (:class:`IPv4Network`) this interface belongs to.
716
717 >>> interface = IPv4Interface('192.0.2.5/24')
718 >>> interface.network
719 IPv4Network('192.0.2.0/24')
720
721 .. attribute:: with_prefixlen
722
723 A string representation of the interface with the mask in prefix notation.
724
725 >>> interface = IPv4Interface('192.0.2.5/24')
726 >>> interface.with_prefixlen
727 '192.0.2.5/24'
728
729 .. attribute:: with_netmask
730
731 A string representation of the interface with the network as a net mask.
732
733 >>> interface = IPv4Interface('192.0.2.5/24')
734 >>> interface.with_netmask
735 '192.0.2.5/255.255.255.0'
736
737 .. attribute:: with_hostmask
738
739 A string representation of the interface with the network as a host mask.
740
741 >>> interface = IPv4Interface('192.0.2.5/24')
742 >>> interface.with_hostmask
743 '192.0.2.5/0.0.0.255'
Eli Bendersky0e497492012-07-31 17:23:11 +0300744
745
746.. class:: IPv6Interface(address)
747
Nick Coghlana8517ad2012-08-20 10:04:26 +1000748 Construct an IPv6 interface. The meaning of *address* is as in the
749 constructor of :class:`IPv6Network`, except that arbitrary host addresses
750 are always accepted.
Eli Bendersky0e497492012-07-31 17:23:11 +0300751
Nick Coghlana8517ad2012-08-20 10:04:26 +1000752 :class:`IPv6Interface` is a subclass of :class:`IPv6Address`, so it inherits
753 all the attributes from that class. In addition, the following attributes
754 are available:
Eli Bendersky0e497492012-07-31 17:23:11 +0300755
Nick Coghlana8517ad2012-08-20 10:04:26 +1000756 .. attribute:: ip
757 .. attribute:: network
758 .. attribute:: with_prefixlen
759 .. attribute:: with_netmask
760 .. attribute:: with_hostmask
761
762 Refer to the corresponding attribute documentation in
763 :class:`IPv4Interface`.
Eli Bendersky0e497492012-07-31 17:23:11 +0300764
765
Nick Coghlan9680bdb2012-06-17 17:24:10 +1000766Other Module Level Functions
767----------------------------
768
769The module also provides the following module level functions:
770
771.. function:: v4_int_to_packed(address)
772
773 Represent an address as 4 packed bytes in network (big-endian) order.
774 *address* is an integer representation of an IPv4 IP address. A
775 :exc:`ValueError` is raised if the integer is negative or too large to be an
776 IPv4 IP address.
777
778 >>> ipaddress.ip_address(3221225985)
779 IPv4Address('192.0.2.1')
780 >>> ipaddress.v4_int_to_packed(3221225985)
781 b'\xc0\x00\x02\x01'
782
783
784.. function:: v6_int_to_packed(address)
785
786 Represent an address as 16 packed bytes in network (big-endian) order.
787 *address* is an integer representation of an IPv6 IP address. A
788 :exc:`ValueError` is raised if the integer is negative or too large to be an
789 IPv6 IP address.
790
791
792.. function:: summarize_address_range(first, last)
793
794 Return an iterator of the summarized network range given the first and last
795 IP addresses. *first* is the first :class:`IPv4Address` or
796 :class:`IPv6Address` in the range and *last* is the last :class:`IPv4Address`
797 or :class:`IPv6Address` in the range. A :exc:`TypeError` is raised if
798 *first* or *last* are not IP addresses or are not of the same version. A
799 :exc:`ValueError` is raised if *last* is not greater than *first* or if
800 *first* address version is not 4 or 6.
801
802 >>> [ipaddr for ipaddr in ipaddress.summarize_address_range(
803 ... ipaddress.IPv4Address('192.0.2.0'),
804 ... ipaddress.IPv4Address('192.0.2.130'))]
805 [IPv4Network('192.0.2.0/25'), IPv4Network('192.0.2.128/31'), IPv4Network('192.0.2.130/32')]
806
807
808.. function:: collapse_addresses(addresses)
809
810 Return an iterator of the collapsed :class:`IPv4Network` or
811 :class:`IPv6Network` objects. *addresses* is an iterator of
812 :class:`IPv4Network` or :class:`IPv6Network` objects. A :exc:`TypeError` is
813 raised if *addresses* contains mixed version objects.
814
815 >>> [ipaddr for ipaddr in
816 ... ipaddress.collapse_addresses([ipaddress.IPv4Network('192.0.2.0/25'),
817 ... ipaddress.IPv4Network('192.0.2.128/25')])]
818 [IPv4Network('192.0.2.0/24')]
819
820
821.. function:: get_mixed_type_key(obj)
822
823 Return a key suitable for sorting between networks and addresses. Address
824 and Network objects are not sortable by default; they're fundamentally
825 different, so the expression::
826
827 IPv4Address('192.0.2.0') <= IPv4Network('192.0.2.0/24')
828
829 doesn't make sense. There are some times however, where you may wish to
830 have :mod:`ipaddress` sort these anyway. If you need to do this, you can use
831 this function as the ``key`` argument to :func:`sorted()`.
832
833 *obj* is either a network or address object.
834
835
836Custom Exceptions
837-----------------
838
839To support more specific error reporting from class constructors, the
840module defines the following exceptions:
841
842.. exception:: AddressValueError(ValueError)
843
844 Any value error related to the address.
845
846
847.. exception:: NetmaskValueError(ValueError)
848
849 Any value error related to the netmask.