blob: d55511611e541256f01b817a43de4cbb2257c690 [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.
45 A :exc:`ValueError` is raised if *address* does not represent a valid IPv4 or
Nick Coghlan9680bdb2012-06-17 17:24:10 +100046 IPv6 address.
47
48 >>> 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
104 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
106 tolerated only for values less then 8 (as there is no ambiguity
107 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')
Nick Coghlan730f67f2012-08-05 22:02:18 +1000114 >>> ipaddress.IPv4Address(3221225985)
115 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
149 .. attribute:: is_multicast
150
Nick Coghlan730f67f2012-08-05 22:02:18 +1000151 ``True`` if the address is reserved for multicast use. See
152 :RFC:`3171` (for IPv4) or :RFC:`2373` (for IPv6).
Eli Bendersky0e497492012-07-31 17:23:11 +0300153
154 .. attribute:: is_private
155
Nick Coghlan730f67f2012-08-05 22:02:18 +1000156 ``True`` if the address is allocated for private networks. See
157 :RFC:`1918` (for IPv4) or :RFC:`4193` (for IPv6).
Eli Bendersky0e497492012-07-31 17:23:11 +0300158
159 .. attribute:: is_unspecified
160
Nick Coghlan730f67f2012-08-05 22:02:18 +1000161 ``True`` if the address is unspecified. See :RFC:`5375` (for IPv4)
162 or :RFC:`2373` (for IPv6).
Eli Bendersky0e497492012-07-31 17:23:11 +0300163
164 .. attribute:: is_reserved
165
Nick Coghlan730f67f2012-08-05 22:02:18 +1000166 ``True`` if the address is otherwise IETF reserved.
Eli Bendersky0e497492012-07-31 17:23:11 +0300167
168 .. attribute:: is_loopback
169
Nick Coghlan730f67f2012-08-05 22:02:18 +1000170 ``True`` if this is a loopback address. See :RFC:`3330` (for IPv4)
171 or :RFC:`2373` (for IPv6).
Eli Bendersky0e497492012-07-31 17:23:11 +0300172
173 .. attribute:: is_link_local
174
Nick Coghlan730f67f2012-08-05 22:02:18 +1000175 ``True`` if the address is reserved for link-local usage. See
176 :RFC:`3927`.
177
Eli Bendersky0e497492012-07-31 17:23:11 +0300178
179.. class:: IPv6Address(address)
180
181 Construct an IPv6 address. An :exc:`AddressValueError` is raised if
182 *address* is not a valid IPv6 address.
183
184 The following constitutes a valid IPv6 address:
185
186 1. A string consisting of eight groups of four hexadecimal digits, each
187 group representing 16 bits. The groups are separated by colons.
188 This describes an *exploded* (longhand) notation. The string can
189 also be *compressed* (shorthand notation) by various means. See
190 :RFC:`4291` for details. For example,
191 ``"0000:0000:0000:0000:0000:0abc:0007:0def"`` can be compressed to
192 ``"::abc:7:def"``.
193 2. An integer that fits into 128 bits.
194 3. An integer packed into a :class:`bytes` object of length 16, big-endian.
195
196 >>> ipaddress.IPv6Address('2001:db8::1000')
197 IPv6Address('2001:db8::1000')
198
Nick Coghlan730f67f2012-08-05 22:02:18 +1000199 .. attribute:: compressed
200
201 The short form of the address representation, with leading zeroes in
202 groups omitted and the longest sequence of groups consisting entirely of
203 zeroes collapsed to a single empty group.
204
205 This is also the value returned by ``str(addr)`` for IPv6 addresses.
206
207 .. attribute:: exploded
208
209 The long form of the address representation, with all leading zeroes and
210 groups consisting entirely of zeroes included.
211
212 .. attribute:: packed
213 .. attribute:: version
214 .. attribute:: max_prefixlen
215 .. attribute:: is_multicast
216 .. attribute:: is_private
217 .. attribute:: is_unspecified
218 .. attribute:: is_reserved
219 .. attribute:: is_loopback
220 .. attribute:: is_link_local
221
222 Refer to the corresponding attribute documentation in
223 :class:`IPv4Address`
Eli Bendersky0e497492012-07-31 17:23:11 +0300224
225 .. attribute:: is_site_local
226
Nick Coghlan730f67f2012-08-05 22:02:18 +1000227 ``True`` if the address is reserved for site-local usage. Note that
228 the site-local address space has been deprecated by :RFC:`3879`. Use
229 :attr:`~IPv4Address.is_private` to test if this address is in the
230 space of unique local addresses as defined by :RFC:`4193`.
Eli Bendersky0e497492012-07-31 17:23:11 +0300231
232 .. attribute:: ipv4_mapped
233
Nick Coghlan730f67f2012-08-05 22:02:18 +1000234 For addresses that appear to be IPv4 mapped addresses (starting with
235 ``::FFFF/96``), this property will report the embedded IPv4 address.
236 For any other address, this property will be ``None``.
Eli Bendersky0e497492012-07-31 17:23:11 +0300237
238 .. attribute:: sixtofour
239
Nick Coghlan730f67f2012-08-05 22:02:18 +1000240 For addresses that appear to be 6to4 addresses (starting with
241 ``2002::/16``) as defined by :RFC:`3056`, this property will report
242 the embedded IPv4 address. For any other address, this property will
243 be ``None``.
244
245 .. attribute:: teredo
246
247 For addresses that appear to be Teredo addresses (starting with
248 ``2001::/32``) as defined by :RFC:`4380`, this property will report
249 the embedded ``(server, client)`` IP address pair. For any other
250 address, this property will be ``None``.
251
252
253Conversion to Strings and Integers
254^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
255
256To interoperate with networking interfaces such as the socket module,
257addresses must be converted to strings or integers. This is handled using
258the :func:`str` and :func:`int` builtin functions::
259
260 >>> str(ipaddress.IPv4Address('192.168.0.1'))
261 '192.168.0.1'
262 >>> int(ipaddress.IPv4Address('192.168.0.1'))
263 3232235521
264 >>> str(ipaddress.IPv6Address('::1'))
265 '::1'
266 >>> int(ipaddress.IPv6Address('::1'))
267 1
Eli Bendersky0e497492012-07-31 17:23:11 +0300268
269
270Operators
271^^^^^^^^^
272
273Address objects support some operators. Unless stated otherwise, operators can
274only be applied between compatible objects (i.e. IPv4 with IPv4, IPv6 with
275IPv6).
276
Nick Coghlan730f67f2012-08-05 22:02:18 +1000277
Eli Bendersky0e497492012-07-31 17:23:11 +0300278Logical operators
279"""""""""""""""""
280
281Address objects can be compared with the usual set of logical operators. Some
282examples::
283
284 >>> IPv4Address('127.0.0.2') > IPv4Address('127.0.0.1')
285 True
286 >>> IPv4Address('127.0.0.2') == IPv4Address('127.0.0.1')
287 False
288 >>> IPv4Address('127.0.0.2') != IPv4Address('127.0.0.1')
289 True
290
Nick Coghlan730f67f2012-08-05 22:02:18 +1000291
Eli Bendersky0e497492012-07-31 17:23:11 +0300292Arithmetic operators
293""""""""""""""""""""
294
295Integers can be added to or subtracted from address objects. Some examples::
296
297 >>> IPv4Address('127.0.0.2') + 3
298 IPv4Address('127.0.0.5')
299 >>> IPv4Address('127.0.0.2') - 3
300 IPv4Address('126.255.255.255')
301 >>> IPv4Address('255.255.255.255') + 1
302 Traceback (most recent call last):
303 File "<stdin>", line 1, in <module>
304 ipaddress.AddressValueError: 4294967296 (>= 2**32) is not permitted as an IPv4 address
305
306
Nick Coghlan730f67f2012-08-05 22:02:18 +1000307IP Network definitions
308----------------------
309
310The :class:`IPv4Network` and :class:`IPv6Network` objects provide a mechanism
311for defining and inspecting IP network definitions. A network definition
312consists of a *mask* and a *network address*, and as such defines a range of
313IP addresses that equal the network address when masked (binary AND) with the
314mask. For example, a network definition with the mask ``255.255.255.0`` and
315the network address ``192.168.1.0`` consists of IP addresses in the inclusive
316range ``192.168.1.0`` to ``192.168.1.255``.
317
318
319Prefix, net mask and host mask
320^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
321
322There are several equivalent ways to specify IP network masks. A *prefix*
323``/<nbits>`` is a notation that denotes how many high-order bits are set in
324the network mask. A *net mask* is an IP address with some number of
325high-order bits set. Thus the prefix ``/24`` is equivalent to the net mask
326``255.255.255.0`` in IPv4, or ``ffff:ff00::`` in IPv6. In addition, a
327*host mask* is the logical inverse of a *net mask*, and is sometimes used
328(for example in Cisco access control lists) to denote a network mask. The
329host mask equivalent to ``/24`` in IPv4 is ``0.0.0.255``.
330
331
Eli Bendersky0e497492012-07-31 17:23:11 +0300332Network objects
Nick Coghlan730f67f2012-08-05 22:02:18 +1000333^^^^^^^^^^^^^^^
334
335All attributes implemented by address objects are implemented by network
336objects as well. In addition, network objects implement additional attributes.
337All of these are common between :class:`IPv4Network` and :class:`IPv6Network`,
338so to avoid duplication they are only documented for :class:`IPv4Network`.
Nick Coghlan9680bdb2012-06-17 17:24:10 +1000339
340.. class:: IPv4Network(address, strict=True)
341
Nick Coghlan730f67f2012-08-05 22:02:18 +1000342 Construct an IPv4 network definition. *address* can be one of the following:
343
344 1. A string consisting of an IP address and an optional mask, separated by
345 a slash (``/``). The IP address is the network address, and the mask
346 can be either a single number, which means it's a *prefix*, or a string
347 representation of an IPv4 address. If it's the latter, the mask is
348 interpreted as a *net mask* if it starts with a non-zero field, or as
349 a *host mask* if it starts with a zero field. If no mask is provided,
350 it's considered to be ``/32``.
351
352 For example, the following *address* specifications are equivalent:
353 ``192.168.1.0/24``, ``192.168.1.0/255.255.255.0`` and
354 ``192.168.1.0/0.0.0.255``.
355
356 2. An integer that fits into 32 bits. This is equivalent to a
357 single-address network, with the network address being *address* and
358 the mask being ``/32``.
359
360 3. An integer packed into a :class:`bytes` object of length 4, big-endian.
361 The interpretation is similar to an integer *address*.
362
363 An :exc:`AddressValueError` is raised if *address* is not a valid IPv4
364 address. A :exc:`NetmaskValueError` is raised if the mask is not valid for
365 an IPv4 address.
Nick Coghlan9680bdb2012-06-17 17:24:10 +1000366
367 If *strict* is ``True`` and host bits are set in the supplied address,
Nick Coghlan730f67f2012-08-05 22:02:18 +1000368 then :exc:`ValueError` is raised. Otherwise, the host bits are masked out
Nick Coghlan9680bdb2012-06-17 17:24:10 +1000369 to determine the appropriate network address.
370
Nick Coghlan7362c3e2012-08-05 22:32:37 +1000371 Unless stated otherwise, all network methods accepting other network/address
372 objects will raise :exc:`TypeError` if the argument's IP version is
373 incompatible to ``self``
374
375 .. attribute:: version
376 .. attribute:: max_prefixlen
377
378 Refer to the corresponding attribute documentation in
379 :class:`IPv4Address`
380
381 .. attribute:: is_multicast
382 .. attribute:: is_private
383 .. attribute:: is_unspecified
384 .. attribute:: is_reserved
385 .. attribute:: is_loopback
386 .. attribute:: is_link_local
387
388 These attributes are true for the network as a whole if they are true
389 true for both the network address and the broadcast address
390
391 .. attribute:: network_address
392
393 The broadcast address for the network.
Nick Coghlan730f67f2012-08-05 22:02:18 +1000394
395 .. attribute:: broadcast_address
396
Nick Coghlan7362c3e2012-08-05 22:32:37 +1000397 The broadcast address for the network.
Nick Coghlan730f67f2012-08-05 22:02:18 +1000398
399 .. attribute:: host mask
400
Nick Coghlan7362c3e2012-08-05 22:32:37 +1000401 The host mask, as a string.
Nick Coghlan730f67f2012-08-05 22:02:18 +1000402
403 .. attribute:: with_prefixlen
Nick Coghlan7362c3e2012-08-05 22:32:37 +1000404 .. attribute:: compressed
405 .. attribute:: exploded
Nick Coghlan730f67f2012-08-05 22:02:18 +1000406
Nick Coghlan7362c3e2012-08-05 22:32:37 +1000407 A string representation of the network, with the mask in prefix
408 notation.
409
410 ``with_prefixlen`` and ``compressed`` are always the same as
411 ``str(network)``.
412 ``exploded`` uses the exploded form the network address.
Nick Coghlan730f67f2012-08-05 22:02:18 +1000413
414 .. attribute:: with_netmask
415
Nick Coghlan7362c3e2012-08-05 22:32:37 +1000416 A string representation of the network, with the mask in net mask
417 notation.
Nick Coghlan730f67f2012-08-05 22:02:18 +1000418
419 .. attribute:: with_hostmask
420
Nick Coghlan7362c3e2012-08-05 22:32:37 +1000421 A string representation of the network, with the mask in host mask
422 notation.
Nick Coghlan730f67f2012-08-05 22:02:18 +1000423
424 .. attribute:: num_addresses
425
Nick Coghlan7362c3e2012-08-05 22:32:37 +1000426 The total number of addresses in the network.
Nick Coghlan730f67f2012-08-05 22:02:18 +1000427
428 .. attribute:: prefixlen
429
Nick Coghlan7362c3e2012-08-05 22:32:37 +1000430 Length of the network prefix, in bits.
Nick Coghlan730f67f2012-08-05 22:02:18 +1000431
432 .. method:: hosts()
433
Nick Coghlan7362c3e2012-08-05 22:32:37 +1000434 Returns an iterator over the usable hosts in the network. The usable
435 hosts are all the IP addresses that belong to the network, except the
436 network address itself and the network broadcast address.
Nick Coghlan730f67f2012-08-05 22:02:18 +1000437
Nick Coghlan7362c3e2012-08-05 22:32:37 +1000438 >>> list(ip_network('192.0.2.0/29').hosts())
439 [IPv4Address('192.0.2.1'), IPv4Address('192.0.2.2'),
440 IPv4Address('192.0.2.3'), IPv4Address('192.0.2.4'),
441 IPv4Address('192.0.2.5'), IPv4Address('192.0.2.6')]
Nick Coghlan730f67f2012-08-05 22:02:18 +1000442
443 .. method:: overlaps(other)
444
Nick Coghlan7362c3e2012-08-05 22:32:37 +1000445 ``True`` if this network is partly or wholly contained in *other* or
446 or *other* is wholly contained in this network.
Nick Coghlan730f67f2012-08-05 22:02:18 +1000447
448 .. method:: address_exclude(network)
449
Nick Coghlan7362c3e2012-08-05 22:32:37 +1000450 Computes the network definitions resulting from removing the given
451 *network* from this one. Returns an iterator of network objects.
452 Raises :exc:`ValueError` if *network* is not completely contained in
453 this network.
Nick Coghlan730f67f2012-08-05 22:02:18 +1000454
Nick Coghlan7362c3e2012-08-05 22:32:37 +1000455 >>> n1 = ip_network('192.0.2.0/28')
456 >>> n2 = ip_network('192.0.2.1/32')
457 >>> list(n1.address_exclude(n2))
458 [IPv4Network('192.0.2.8/29'), IPv4Network('192.0.2.4/30'),
459 IPv4Network('192.0.2.2/31'), IPv4Network('192.0.2.0/32')]
Nick Coghlan730f67f2012-08-05 22:02:18 +1000460
461 .. method:: subnets(prefixlen_diff=1, new_prefix=None)
462
Nick Coghlan7362c3e2012-08-05 22:32:37 +1000463 The subnets that join to make the current network definition, depending
464 on the argument values. *prefixlen_diff* is the amount our prefix
465 length should be increased by. *new_prefix* is the desired new
466 prefix of the subnets; it must be larger than our prefix. One and
467 only one of *prefixlen_diff* and *new_prefix* must be set. Returns an
468 iterator of network objects.
Nick Coghlan730f67f2012-08-05 22:02:18 +1000469
Nick Coghlan7362c3e2012-08-05 22:32:37 +1000470 >>> list(ip_network('192.0.2.0/24').subnets())
471 [IPv4Network('192.0.2.0/25'), IPv4Network('192.0.2.128/25')]
472 >>> list(ip_network('192.0.2.0/24').subnets(prefixlen_diff=2))
473 [IPv4Network('192.0.2.0/26'), IPv4Network('192.0.2.64/26'),
474 IPv4Network('192.0.2.128/26'), IPv4Network('192.0.2.192/26')]
475 >>> list(ip_network('192.0.2.0/24').subnets(new_prefix=26))
476 [IPv4Network('192.0.2.0/26'), IPv4Network('192.0.2.64/26'),
477 IPv4Network('192.0.2.128/26'), IPv4Network('192.0.2.192/26')]
478 >>> list(ip_network('192.0.2.0/24').subnets(new_prefix=23))
479 Traceback (most recent call last):
480 File "<stdin>", line 1, in <module>
481 raise ValueError('new prefix must be longer')
482 ValueError: new prefix must be longer
483 >>> list(ip_network('192.0.2.0/24').subnets(new_prefix=25))
484 [IPv4Network('192.0.2.0/25'), IPv4Network('192.0.2.128/25')]
Nick Coghlan730f67f2012-08-05 22:02:18 +1000485
486 .. method:: supernet(prefixlen_diff=1, new_prefix=None)
487
Nick Coghlan7362c3e2012-08-05 22:32:37 +1000488 The supernet containing this network definition, depending on the
489 argument values. *prefixlen_diff* is the amount our prefix length
490 should be decreased by. *new_prefix* is the desired new prefix of
491 the supernet; it must be smaller than our prefix. One and only one
492 of *prefixlen_diff* and *new_prefix* must be set. Returns a single
493 network object.
Nick Coghlan730f67f2012-08-05 22:02:18 +1000494
Nick Coghlan7362c3e2012-08-05 22:32:37 +1000495 >>> ip_network('192.0.2.0/24').supernet()
496 IPv4Network('192.0.2.0/23')
497 >>> ip_network('192.0.2.0/24').supernet(prefixlen_diff=2)
498 IPv4Network('192.0.0.0/22')
499 >>> ip_network('192.0.2.0/24').supernet(new_prefix=20)
500 IPv4Network('192.0.0.0/20')
Nick Coghlan730f67f2012-08-05 22:02:18 +1000501
502 .. method:: compare_networks(other)
503
Nick Coghlan7362c3e2012-08-05 22:32:37 +1000504 Compare this network to *other*. In this comparison only the network
505 addresses are considered; host bits aren't. Returns either ``-1``,
506 ``0`` or ``1``.
Nick Coghlan730f67f2012-08-05 22:02:18 +1000507
Nick Coghlan7362c3e2012-08-05 22:32:37 +1000508 >>> ip_network('192.0.2.1/32').compare_networks(ip_network('192.0.2.2/32'))
509 -1
510 >>> ip_network('192.0.2.1/32').compare_networks(ip_network('192.0.2.0/32'))
511 1
512 >>> ip_network('192.0.2.1/32').compare_networks(ip_network('192.0.2.1/32'))
513 0
Nick Coghlan9680bdb2012-06-17 17:24:10 +1000514
515
Nick Coghlan9680bdb2012-06-17 17:24:10 +1000516.. class:: IPv6Network(address, strict=True)
517
Nick Coghlan730f67f2012-08-05 22:02:18 +1000518 Construct an IPv6 network definition. *address* can be one of the following:
519
520 1. A string consisting of an IP address and an optional mask, separated by
Nick Coghlan7362c3e2012-08-05 22:32:37 +1000521 a slash (``/``). The IP address is the network address, and the mask
Nick Coghlan730f67f2012-08-05 22:02:18 +1000522 can be either a single number, which means it's a *prefix*, or a string
523 representation of an IPv6 address. If it's the latter, the mask is
524 interpreted as a *net mask*. If no mask is provided, it's considered to
525 be ``/128``.
526
527 For example, the following *address* specifications are equivalent:
528 ``2001:db00::0/24`` and ``2001:db00::0/ffff:ff00::``.
529
530 2. An integer that fits into 128 bits. This is equivalent to a
531 single-address network, with the network address being *address* and
532 the mask being ``/128``.
533
534 3. An integer packed into a :class:`bytes` object of length 16, bit-endian.
535 The interpretation is similar to an integer *address*.
536
537 An :exc:`AddressValueError` is raised if *address* is not a valid IPv6
538 address. A :exc:`NetmaskValueError` is raised if the mask is not valid for
539 an IPv6 address.
Nick Coghlan9680bdb2012-06-17 17:24:10 +1000540
541 If *strict* is ``True`` and host bits are set in the supplied address,
Nick Coghlan730f67f2012-08-05 22:02:18 +1000542 then :exc:`ValueError` is raised. Otherwise, the host bits are masked out
Nick Coghlan9680bdb2012-06-17 17:24:10 +1000543 to determine the appropriate network address.
544
Nick Coghlan7362c3e2012-08-05 22:32:37 +1000545 .. attribute:: version
546 .. attribute:: max_prefixlen
547 .. attribute:: is_multicast
548 .. attribute:: is_private
549 .. attribute:: is_unspecified
550 .. attribute:: is_reserved
551 .. attribute:: is_loopback
552 .. attribute:: is_link_local
553 .. attribute:: network_address
554 .. attribute:: broadcast_address
555 .. attribute:: host mask
556 .. attribute:: with_prefixlen
557 .. attribute:: compressed
558 .. attribute:: exploded
559 .. attribute:: with_netmask
560 .. attribute:: with_hostmask
561 .. attribute:: num_addresses
562 .. attribute:: prefixlen
563 .. method:: hosts()
564 .. method:: overlaps(other)
565 .. method:: address_exclude(network)
566 .. method:: subnets(prefixlen_diff=1, new_prefix=None)
567 .. method:: supernet(prefixlen_diff=1, new_prefix=None)
568 .. method:: compare_networks(other)
Nick Coghlan730f67f2012-08-05 22:02:18 +1000569
Nick Coghlan7362c3e2012-08-05 22:32:37 +1000570 Refer to the corresponding attribute documentation in
571 :class:`IPv4Network`
572
573 .. attribute:: is_site_local
574
575 These attribute is true for the network as a whole if it is true
576 true for both the network address and the broadcast address
Nick Coghlan730f67f2012-08-05 22:02:18 +1000577
578
579Operators
580^^^^^^^^^
581
582Network objects support some operators. Unless stated otherwise, operators can
583only be applied between compatible objects (i.e. IPv4 with IPv4, IPv6 with
584IPv6).
585
Nick Coghlan7362c3e2012-08-05 22:32:37 +1000586
Nick Coghlan730f67f2012-08-05 22:02:18 +1000587Logical operators
588"""""""""""""""""
589
590Network objects can be compared with the usual set of logical operators,
591similarly to address objects.
592
Nick Coghlan7362c3e2012-08-05 22:32:37 +1000593
Nick Coghlan730f67f2012-08-05 22:02:18 +1000594Iteration
595"""""""""
596
597Network objects can be iterated to list all the addresses belonging to the
598network. For iteration, *all* hosts are returned, including unusable hosts
599(for usable hosts, use the :meth:`~IPv4Network.hosts` method). An
600example::
601
602 >>> for addr in IPv4Network('192.0.2.0/28'):
603 ... addr
604 ...
605 IPv4Address('192.0.2.0')
606 IPv4Address('192.0.2.1')
607 IPv4Address('192.0.2.2')
608 IPv4Address('192.0.2.3')
609 IPv4Address('192.0.2.4')
610 IPv4Address('192.0.2.5')
611 IPv4Address('192.0.2.6')
612 IPv4Address('192.0.2.7')
613 IPv4Address('192.0.2.8')
614 IPv4Address('192.0.2.9')
615 IPv4Address('192.0.2.10')
616 IPv4Address('192.0.2.11')
617 IPv4Address('192.0.2.12')
618 IPv4Address('192.0.2.13')
619 IPv4Address('192.0.2.14')
620 IPv4Address('192.0.2.15')
621
Nick Coghlan7362c3e2012-08-05 22:32:37 +1000622
Nick Coghlan730f67f2012-08-05 22:02:18 +1000623Networks as containers of addresses
624"""""""""""""""""""""""""""""""""""
625
626Network objects can act as containers of addresses. Some examples::
627
628 >>> IPv4Network('192.0.2.0/28')[0]
629 IPv4Address('192.0.2.0')
630 >>> IPv4Network('192.0.2.0/28')[15]
631 IPv4Address('192.0.2.15')
632 >>> IPv4Address('192.0.2.6') in IPv4Network('192.0.2.0/28')
633 True
634 >>> IPv4Address('192.0.3.6') in IPv4Network('192.0.2.0/28')
635 False
Nick Coghlan9680bdb2012-06-17 17:24:10 +1000636
637
Eli Bendersky0e497492012-07-31 17:23:11 +0300638Interface objects
639-----------------
640
641.. class:: IPv4Interface(address)
642
643 Construct an IPv4 interface. *address* is a string or integer representing
644 the IP interface. An :exc:`AddressValueError` is raised if *address* is not
645 a valid IPv4 address.
646
647 The network address for the interface is determined by calling
648 ``IPv4Network(address, strict=False)``.
649
650 >>> ipaddress.IPv4Interface('192.168.0.0/24')
651 IPv4Interface('192.168.0.0/24')
652 >>> ipaddress.IPv4Interface('192.168.0.0/24').network
653 IPv4Network('192.168.0.0/24')
654
655
656.. class:: IPv6Interface(address)
657
658 Construct an IPv6 interface. *address* is a string or integer representing
659 the IP interface. An :exc:`AddressValueError` is raised if *address* is not
660 a valid IPv6 address.
661
662 The network address for the interface is determined by calling
663 ``IPv6Network(address, strict=False)``.
664
665 >>> ipaddress.IPv6Interface('2001:db8::1000/96')
666 IPv6Interface('2001:db8::1000/96')
667 >>> ipaddress.IPv6Interface('2001:db8::1000/96').network
668 IPv6Network('2001:db8::/96')
669
670
Nick Coghlan9680bdb2012-06-17 17:24:10 +1000671Other Module Level Functions
672----------------------------
673
674The module also provides the following module level functions:
675
676.. function:: v4_int_to_packed(address)
677
678 Represent an address as 4 packed bytes in network (big-endian) order.
679 *address* is an integer representation of an IPv4 IP address. A
680 :exc:`ValueError` is raised if the integer is negative or too large to be an
681 IPv4 IP address.
682
683 >>> ipaddress.ip_address(3221225985)
684 IPv4Address('192.0.2.1')
685 >>> ipaddress.v4_int_to_packed(3221225985)
686 b'\xc0\x00\x02\x01'
687
688
689.. function:: v6_int_to_packed(address)
690
691 Represent an address as 16 packed bytes in network (big-endian) order.
692 *address* is an integer representation of an IPv6 IP address. A
693 :exc:`ValueError` is raised if the integer is negative or too large to be an
694 IPv6 IP address.
695
696
697.. function:: summarize_address_range(first, last)
698
699 Return an iterator of the summarized network range given the first and last
700 IP addresses. *first* is the first :class:`IPv4Address` or
701 :class:`IPv6Address` in the range and *last* is the last :class:`IPv4Address`
702 or :class:`IPv6Address` in the range. A :exc:`TypeError` is raised if
703 *first* or *last* are not IP addresses or are not of the same version. A
704 :exc:`ValueError` is raised if *last* is not greater than *first* or if
705 *first* address version is not 4 or 6.
706
707 >>> [ipaddr for ipaddr in ipaddress.summarize_address_range(
708 ... ipaddress.IPv4Address('192.0.2.0'),
709 ... ipaddress.IPv4Address('192.0.2.130'))]
710 [IPv4Network('192.0.2.0/25'), IPv4Network('192.0.2.128/31'), IPv4Network('192.0.2.130/32')]
711
712
713.. function:: collapse_addresses(addresses)
714
715 Return an iterator of the collapsed :class:`IPv4Network` or
716 :class:`IPv6Network` objects. *addresses* is an iterator of
717 :class:`IPv4Network` or :class:`IPv6Network` objects. A :exc:`TypeError` is
718 raised if *addresses* contains mixed version objects.
719
720 >>> [ipaddr for ipaddr in
721 ... ipaddress.collapse_addresses([ipaddress.IPv4Network('192.0.2.0/25'),
722 ... ipaddress.IPv4Network('192.0.2.128/25')])]
723 [IPv4Network('192.0.2.0/24')]
724
725
726.. function:: get_mixed_type_key(obj)
727
728 Return a key suitable for sorting between networks and addresses. Address
729 and Network objects are not sortable by default; they're fundamentally
730 different, so the expression::
731
732 IPv4Address('192.0.2.0') <= IPv4Network('192.0.2.0/24')
733
734 doesn't make sense. There are some times however, where you may wish to
735 have :mod:`ipaddress` sort these anyway. If you need to do this, you can use
736 this function as the ``key`` argument to :func:`sorted()`.
737
738 *obj* is either a network or address object.
739
740
741Custom Exceptions
742-----------------
743
744To support more specific error reporting from class constructors, the
745module defines the following exceptions:
746
747.. exception:: AddressValueError(ValueError)
748
749 Any value error related to the address.
750
751
752.. exception:: NetmaskValueError(ValueError)
753
754 Any value error related to the netmask.