~dkuhlman/python-training-materials/Materials

« back to all changes in this revision

Viewing changes to python-3.5.1-docs-html/_sources/library/ipaddress.txt

  • Committer: Dave Kuhlman
  • Date: 2017-04-15 16:24:56 UTC
  • Revision ID: dkuhlman@davekuhlman.org-20170415162456-iav9vozzg4iwqwv3
Updated docs

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
: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
 
 
12
 
:mod:`ipaddress` provides the capabilities to create, manipulate and
13
 
operate on IPv4 and IPv6 addresses and networks.
14
 
 
15
 
The functions and classes in this module make it straightforward to handle
16
 
various tasks related to IP addresses, including checking whether or not two
17
 
hosts are on the same subnet, iterating over all hosts in a particular
18
 
subnet, checking whether or not a string represents a valid IP address or
19
 
network definition, and so on.
20
 
 
21
 
This is the full module API reference—for an overview and introduction, see
22
 
:ref:`ipaddress-howto`.
23
 
 
24
 
.. versionadded:: 3.3
25
 
 
26
 
 
27
 
Convenience factory functions
28
 
-----------------------------
29
 
 
30
 
The :mod:`ipaddress` module provides factory functions to conveniently create
31
 
IP addresses, networks and interfaces:
32
 
 
33
 
.. function:: ip_address(address)
34
 
 
35
 
   Return an :class:`IPv4Address` or :class:`IPv6Address` object depending on
36
 
   the IP address passed as argument.  Either IPv4 or IPv6 addresses may be
37
 
   supplied; integers less than 2**32 will be considered to be IPv4 by default.
38
 
   A :exc:`ValueError` is raised if *address* does not represent a valid IPv4
39
 
   or IPv6 address.
40
 
 
41
 
.. testsetup::
42
 
   >>> import ipaddress
43
 
   >>> from ipaddress import (ip_network, IPv4Address, IPv4Interface,
44
 
   ...                        IPv4Network)
45
 
 
46
 
::
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
61
 
   :exc:`ValueError` is raised if *address* does not represent a valid IPv4 or
62
 
   IPv6 address, or if the network has host bits set.
63
 
 
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;
73
 
   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
75
 
   IPv6 address.
76
 
 
77
 
One downside of these convenience functions is that the need to handle both
78
 
IPv4 and IPv6 formats means that error messages provide minimal
79
 
information on the precise error, as the functions don't know whether the
80
 
IPv4 or IPv6 format was intended. More detailed error reporting can be
81
 
obtained by calling the appropriate version specific class constructors
82
 
directly.
83
 
 
84
 
 
85
 
IP Addresses
86
 
------------
87
 
 
88
 
Address objects
89
 
^^^^^^^^^^^^^^^
90
 
 
91
 
The :class:`IPv4Address` and :class:`IPv6Address` objects share a lot of common
92
 
attributes.  Some attributes that are only meaningful for IPv6 addresses are
93
 
also implemented by :class:`IPv4Address` objects, in order to make it easier to
94
 
write code that handles both IP versions correctly.
95
 
 
96
 
.. class:: IPv4Address(address)
97
 
 
98
 
   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
105
 
      integer represents an octet (byte) in the address. Leading zeroes are
106
 
      tolerated only for values less than 8 (as there is no ambiguity
107
 
      between the decimal and octal interpretations of such strings).
108
 
   2. An integer that fits into 32 bits.
109
 
   3. An integer packed into a :class:`bytes` object of length 4 (most
110
 
      significant octet first).
111
 
 
112
 
   >>> ipaddress.IPv4Address('192.168.0.1')
113
 
   IPv4Address('192.168.0.1')
114
 
   >>> ipaddress.IPv4Address(3232235521)
115
 
   IPv4Address('192.168.0.1')
116
 
   >>> ipaddress.IPv4Address(b'\xC0\xA8\x00\x01')
117
 
   IPv4Address('192.168.0.1')
118
 
 
119
 
   .. attribute:: version
120
 
 
121
 
      The appropriate version number: ``4`` for IPv4, ``6`` for IPv6.
122
 
 
123
 
   .. attribute:: max_prefixlen
124
 
 
125
 
      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.
148
 
 
149
 
   .. attribute:: reverse_pointer
150
 
 
151
 
      The name of the reverse DNS PTR record for the IP address, e.g.::
152
 
 
153
 
          >>> ipaddress.ip_address("127.0.0.1").reverse_pointer
154
 
          '1.0.0.127.in-addr.arpa'
155
 
          >>> ipaddress.ip_address("2001:db8::1").reverse_pointer
156
 
          '1.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.8.b.d.0.1.0.0.2.ip6.arpa'
157
 
 
158
 
      This is the name that could be used for performing a PTR lookup, not the
159
 
      resolved hostname itself.
160
 
 
161
 
   .. versionadded:: 3.5
162
 
 
163
 
   .. attribute:: is_multicast
164
 
 
165
 
      ``True`` if the address is reserved for multicast use.  See
166
 
      :RFC:`3171` (for IPv4) or :RFC:`2373` (for IPv6).
167
 
 
168
 
   .. attribute:: is_private
169
 
 
170
 
      ``True`` if the address is allocated for private networks.  See
171
 
      iana-ipv4-special-registry_ (for IPv4) or iana-ipv6-special-registry_
172
 
      (for IPv6).
173
 
 
174
 
   .. attribute:: is_global
175
 
 
176
 
      ``True`` if the address is allocated for public networks.  See
177
 
      iana-ipv4-special-registry_ (for IPv4) or iana-ipv6-special-registry_
178
 
      (for IPv6).
179
 
 
180
 
      .. versionadded:: 3.4
181
 
 
182
 
   .. attribute:: is_unspecified
183
 
 
184
 
      ``True`` if the address is unspecified.  See :RFC:`5735` (for IPv4)
185
 
      or :RFC:`2373` (for IPv6).
186
 
 
187
 
   .. attribute:: is_reserved
188
 
 
189
 
      ``True`` if the address is otherwise IETF reserved.
190
 
 
191
 
   .. attribute:: is_loopback
192
 
 
193
 
      ``True`` if this is a loopback address.  See :RFC:`3330` (for IPv4)
194
 
      or :RFC:`2373` (for IPv6).
195
 
 
196
 
   .. attribute:: is_link_local
197
 
 
198
 
      ``True`` if the address is reserved for link-local usage.  See
199
 
      :RFC:`3927`.
200
 
 
201
 
.. _iana-ipv4-special-registry: http://www.iana.org/assignments/iana-ipv4-special-registry/iana-ipv4-special-registry.xhtml
202
 
.. _iana-ipv6-special-registry: http://www.iana.org/assignments/iana-ipv6-special-registry/iana-ipv6-special-registry.xhtml
203
 
 
204
 
 
205
 
.. class:: IPv6Address(address)
206
 
 
207
 
   Construct an IPv6 address.  An :exc:`AddressValueError` is raised if
208
 
   *address* is not a valid IPv6 address.
209
 
 
210
 
   The following constitutes a valid IPv6 address:
211
 
 
212
 
   1. A string consisting of eight groups of four hexadecimal digits, each
213
 
      group representing 16 bits.  The groups are separated by colons.
214
 
      This describes an *exploded* (longhand) notation.  The string can
215
 
      also be *compressed* (shorthand notation) by various means.  See
216
 
      :RFC:`4291` for details.  For example,
217
 
      ``"0000:0000:0000:0000:0000:0abc:0007:0def"`` can be compressed to
218
 
      ``"::abc:7:def"``.
219
 
   2. An integer that fits into 128 bits.
220
 
   3. An integer packed into a :class:`bytes` object of length 16, big-endian.
221
 
 
222
 
   >>> ipaddress.IPv6Address('2001:db8::1000')
223
 
   IPv6Address('2001:db8::1000')
224
 
 
225
 
   .. attribute:: compressed
226
 
 
227
 
   The short form of the address representation, with leading zeroes in
228
 
   groups omitted and the longest sequence of groups consisting entirely of
229
 
   zeroes collapsed to a single empty group.
230
 
 
231
 
   This is also the value returned by ``str(addr)`` for IPv6 addresses.
232
 
 
233
 
   .. attribute:: exploded
234
 
 
235
 
   The long form of the address representation, with all leading zeroes and
236
 
   groups consisting entirely of zeroes included.
237
 
 
238
 
 
239
 
   For the following attributes, see the corresponding documention of the
240
 
   :class:`IPv4Address` class:
241
 
 
242
 
   .. attribute:: packed
243
 
   .. attribute:: reverse_pointer
244
 
   .. attribute:: version
245
 
   .. attribute:: max_prefixlen
246
 
   .. attribute:: is_multicast
247
 
   .. attribute:: is_private
248
 
   .. attribute:: is_global
249
 
   .. attribute:: is_unspecified
250
 
   .. attribute:: is_reserved
251
 
   .. attribute:: is_loopback
252
 
   .. attribute:: is_link_local
253
 
 
254
 
      .. versionadded:: 3.4
255
 
         is_global
256
 
 
257
 
   .. attribute:: is_site_local
258
 
 
259
 
      ``True`` if the address is reserved for site-local usage.  Note that
260
 
      the site-local address space has been deprecated by :RFC:`3879`. Use
261
 
      :attr:`~IPv4Address.is_private` to test if this address is in the
262
 
      space of unique local addresses as defined by :RFC:`4193`.
263
 
 
264
 
   .. attribute:: ipv4_mapped
265
 
 
266
 
      For addresses that appear to be IPv4 mapped addresses (starting with
267
 
      ``::FFFF/96``), this property will report the embedded IPv4 address.
268
 
      For any other address, this property will be ``None``.
269
 
 
270
 
   .. attribute:: sixtofour
271
 
 
272
 
      For addresses that appear to be 6to4 addresses  (starting with
273
 
      ``2002::/16``) as defined by :RFC:`3056`, this property will report
274
 
      the embedded IPv4 address.  For any other address, this property will
275
 
      be ``None``.
276
 
 
277
 
   .. attribute:: teredo
278
 
 
279
 
      For addresses that appear to be Teredo addresses (starting with
280
 
      ``2001::/32``) as defined by :RFC:`4380`, this property will report
281
 
      the embedded ``(server, client)`` IP address pair.  For any other
282
 
      address, this property will be ``None``.
283
 
 
284
 
 
285
 
Conversion to Strings and Integers
286
 
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
287
 
 
288
 
To interoperate with networking interfaces such as the socket module,
289
 
addresses must be converted to strings or integers. This is handled using
290
 
the :func:`str` and :func:`int` builtin functions::
291
 
 
292
 
   >>> str(ipaddress.IPv4Address('192.168.0.1'))
293
 
   '192.168.0.1'
294
 
   >>> int(ipaddress.IPv4Address('192.168.0.1'))
295
 
   3232235521
296
 
   >>> str(ipaddress.IPv6Address('::1'))
297
 
   '::1'
298
 
   >>> int(ipaddress.IPv6Address('::1'))
299
 
   1
300
 
 
301
 
 
302
 
Operators
303
 
^^^^^^^^^
304
 
 
305
 
Address objects support some operators.  Unless stated otherwise, operators can
306
 
only be applied between compatible objects (i.e. IPv4 with IPv4, IPv6 with
307
 
IPv6).
308
 
 
309
 
 
310
 
Comparison operators
311
 
""""""""""""""""""""
312
 
 
313
 
Address objects can be compared with the usual set of comparison operators.  Some
314
 
examples::
315
 
 
316
 
   >>> IPv4Address('127.0.0.2') > IPv4Address('127.0.0.1')
317
 
   True
318
 
   >>> IPv4Address('127.0.0.2') == IPv4Address('127.0.0.1')
319
 
   False
320
 
   >>> IPv4Address('127.0.0.2') != IPv4Address('127.0.0.1')
321
 
   True
322
 
 
323
 
 
324
 
Arithmetic operators
325
 
""""""""""""""""""""
326
 
 
327
 
Integers can be added to or subtracted from address objects.  Some examples::
328
 
 
329
 
   >>> IPv4Address('127.0.0.2') + 3
330
 
   IPv4Address('127.0.0.5')
331
 
   >>> IPv4Address('127.0.0.2') - 3
332
 
   IPv4Address('126.255.255.255')
333
 
   >>> IPv4Address('255.255.255.255') + 1
334
 
   Traceback (most recent call last):
335
 
     File "<stdin>", line 1, in <module>
336
 
   ipaddress.AddressValueError: 4294967296 (>= 2**32) is not permitted as an IPv4 address
337
 
 
338
 
 
339
 
IP Network definitions
340
 
----------------------
341
 
 
342
 
The :class:`IPv4Network` and :class:`IPv6Network` objects provide a mechanism
343
 
for defining and inspecting IP network definitions.  A network definition
344
 
consists of a *mask* and a *network address*, and as such defines a range of
345
 
IP addresses that equal the network address when masked (binary AND) with the
346
 
mask.  For example, a network definition with the mask ``255.255.255.0`` and
347
 
the network address ``192.168.1.0`` consists of IP addresses in the inclusive
348
 
range ``192.168.1.0`` to ``192.168.1.255``.
349
 
 
350
 
 
351
 
Prefix, net mask and host mask
352
 
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
353
 
 
354
 
There are several equivalent ways to specify IP network masks.  A *prefix*
355
 
``/<nbits>`` is a notation that denotes how many high-order bits are set in
356
 
the network mask.  A *net mask* is an IP address with some number of
357
 
high-order bits set.  Thus the prefix ``/24`` is equivalent to the net mask
358
 
``255.255.255.0`` in IPv4, or ``ffff:ff00::`` in IPv6.  In addition, a
359
 
*host mask* is the logical inverse of a *net mask*, and is sometimes used
360
 
(for example in Cisco access control lists) to denote a network mask.  The
361
 
host mask equivalent to ``/24`` in IPv4 is ``0.0.0.255``.
362
 
 
363
 
 
364
 
Network objects
365
 
^^^^^^^^^^^^^^^
366
 
 
367
 
All attributes implemented by address objects are implemented by network
368
 
objects as well.  In addition, network objects implement additional attributes.
369
 
All of these are common between :class:`IPv4Network` and :class:`IPv6Network`,
370
 
so to avoid duplication they are only documented for :class:`IPv4Network`.
371
 
 
372
 
.. class:: IPv4Network(address, strict=True)
373
 
 
374
 
   Construct an IPv4 network definition.  *address* can be one of the following:
375
 
 
376
 
   1. A string consisting of an IP address and an optional mask, separated by
377
 
      a slash (``/``).  The IP address is the network address, and the mask
378
 
      can be either a single number, which means it's a *prefix*, or a string
379
 
      representation of an IPv4 address.  If it's the latter, the mask is
380
 
      interpreted as a *net mask* if it starts with a non-zero field, or as
381
 
      a *host mask* if it starts with a zero field.  If no mask is provided,
382
 
      it's considered to be ``/32``.
383
 
 
384
 
      For example, the following *address* specifications are equivalent:
385
 
      ``192.168.1.0/24``, ``192.168.1.0/255.255.255.0`` and
386
 
      ``192.168.1.0/0.0.0.255``.
387
 
 
388
 
   2. An integer that fits into 32 bits.  This is equivalent to a
389
 
      single-address network, with the network address being *address* and
390
 
      the mask being ``/32``.
391
 
 
392
 
   3. An integer packed into a :class:`bytes` object of length 4, big-endian.
393
 
      The interpretation is similar to an integer *address*.
394
 
 
395
 
   4. A two-tuple of an address description and a netmask, where the address
396
 
      description is either a string, a 32-bits integer, a 4-bytes packed
397
 
      integer, or an existing IPv4Address object; and the netmask is either
398
 
      an integer representing the prefix length (e.g. ``24``) or a string
399
 
      representing the prefix mask (e.g. ``255.255.255.0``).
400
 
 
401
 
   An :exc:`AddressValueError` is raised if *address* is not a valid IPv4
402
 
   address.  A :exc:`NetmaskValueError` is raised if the mask is not valid for
403
 
   an IPv4 address.
404
 
 
405
 
   If *strict* is ``True`` and host bits are set in the supplied address,
406
 
   then :exc:`ValueError` is raised.  Otherwise, the host bits are masked out
407
 
   to determine the appropriate network address.
408
 
 
409
 
   Unless stated otherwise, all network methods accepting other network/address
410
 
   objects will raise :exc:`TypeError` if the argument's IP version is
411
 
   incompatible to ``self``
412
 
 
413
 
   .. versionchanged:: 3.5
414
 
 
415
 
      Added the two-tuple form for the *address* constructor parameter.
416
 
 
417
 
   .. attribute:: version
418
 
   .. attribute:: max_prefixlen
419
 
 
420
 
      Refer to the corresponding attribute documentation in
421
 
      :class:`IPv4Address`
422
 
 
423
 
   .. attribute:: is_multicast
424
 
   .. attribute:: is_private
425
 
   .. attribute:: is_unspecified
426
 
   .. attribute:: is_reserved
427
 
   .. attribute:: is_loopback
428
 
   .. attribute:: is_link_local
429
 
 
430
 
      These attributes are true for the network as a whole if they are true
431
 
      for both the network address and the broadcast address
432
 
 
433
 
   .. attribute:: network_address
434
 
 
435
 
      The network address for the network. The network address and the
436
 
      prefix length together uniquely define a network.
437
 
 
438
 
   .. attribute:: broadcast_address
439
 
 
440
 
      The broadcast address for the network. Packets sent to the broadcast
441
 
      address should be received by every host on the network.
442
 
 
443
 
   .. attribute:: hostmask
444
 
 
445
 
      The host mask, as a string.
446
 
 
447
 
   .. attribute:: with_prefixlen
448
 
   .. attribute:: compressed
449
 
   .. attribute:: exploded
450
 
 
451
 
      A string representation of the network, with the mask in prefix
452
 
      notation.
453
 
 
454
 
      ``with_prefixlen`` and ``compressed`` are always the same as
455
 
      ``str(network)``.
456
 
      ``exploded`` uses the exploded form the network address.
457
 
 
458
 
   .. attribute:: with_netmask
459
 
 
460
 
      A string representation of the network, with the mask in net mask
461
 
      notation.
462
 
 
463
 
   .. attribute:: with_hostmask
464
 
 
465
 
      A string representation of the network, with the mask in host mask
466
 
      notation.
467
 
 
468
 
   .. attribute:: num_addresses
469
 
 
470
 
      The total number of addresses in the network.
471
 
 
472
 
   .. attribute:: prefixlen
473
 
 
474
 
      Length of the network prefix, in bits.
475
 
 
476
 
   .. method:: hosts()
477
 
 
478
 
      Returns an iterator over the usable hosts in the network.  The usable
479
 
      hosts are all the IP addresses that belong to the network, except the
480
 
      network address itself and the network broadcast address.
481
 
 
482
 
         >>> list(ip_network('192.0.2.0/29').hosts())  #doctest: +NORMALIZE_WHITESPACE
483
 
         [IPv4Address('192.0.2.1'), IPv4Address('192.0.2.2'),
484
 
          IPv4Address('192.0.2.3'), IPv4Address('192.0.2.4'),
485
 
          IPv4Address('192.0.2.5'), IPv4Address('192.0.2.6')]
486
 
 
487
 
   .. method:: overlaps(other)
488
 
 
489
 
      ``True`` if this network is partly or wholly contained in *other* or
490
 
      *other* is wholly contained in this network.
491
 
 
492
 
   .. method:: address_exclude(network)
493
 
 
494
 
      Computes the network definitions resulting from removing the given
495
 
      *network* from this one.  Returns an iterator of network objects.
496
 
      Raises :exc:`ValueError` if *network* is not completely contained in
497
 
      this network.
498
 
 
499
 
         >>> n1 = ip_network('192.0.2.0/28')
500
 
         >>> n2 = ip_network('192.0.2.1/32')
501
 
         >>> list(n1.address_exclude(n2))  #doctest: +NORMALIZE_WHITESPACE
502
 
         [IPv4Network('192.0.2.8/29'), IPv4Network('192.0.2.4/30'),
503
 
          IPv4Network('192.0.2.2/31'), IPv4Network('192.0.2.0/32')]
504
 
 
505
 
   .. method:: subnets(prefixlen_diff=1, new_prefix=None)
506
 
 
507
 
      The subnets that join to make the current network definition, depending
508
 
      on the argument values.  *prefixlen_diff* is the amount our prefix
509
 
      length should be increased by.  *new_prefix* is the desired new
510
 
      prefix of the subnets; it must be larger than our prefix.  One and
511
 
      only one of *prefixlen_diff* and *new_prefix* must be set.  Returns an
512
 
      iterator of network objects.
513
 
 
514
 
         >>> list(ip_network('192.0.2.0/24').subnets())
515
 
         [IPv4Network('192.0.2.0/25'), IPv4Network('192.0.2.128/25')]
516
 
         >>> list(ip_network('192.0.2.0/24').subnets(prefixlen_diff=2))  #doctest: +NORMALIZE_WHITESPACE
517
 
         [IPv4Network('192.0.2.0/26'), IPv4Network('192.0.2.64/26'),
518
 
          IPv4Network('192.0.2.128/26'), IPv4Network('192.0.2.192/26')]
519
 
         >>> list(ip_network('192.0.2.0/24').subnets(new_prefix=26))  #doctest: +NORMALIZE_WHITESPACE
520
 
         [IPv4Network('192.0.2.0/26'), IPv4Network('192.0.2.64/26'),
521
 
          IPv4Network('192.0.2.128/26'), IPv4Network('192.0.2.192/26')]
522
 
         >>> list(ip_network('192.0.2.0/24').subnets(new_prefix=23))
523
 
         Traceback (most recent call last):
524
 
           File "<stdin>", line 1, in <module>
525
 
             raise ValueError('new prefix must be longer')
526
 
         ValueError: new prefix must be longer
527
 
         >>> list(ip_network('192.0.2.0/24').subnets(new_prefix=25))
528
 
         [IPv4Network('192.0.2.0/25'), IPv4Network('192.0.2.128/25')]
529
 
 
530
 
   .. method:: supernet(prefixlen_diff=1, new_prefix=None)
531
 
 
532
 
      The supernet containing this network definition, depending on the
533
 
      argument values.  *prefixlen_diff* is the amount our prefix length
534
 
      should be decreased by.  *new_prefix* is the desired new prefix of
535
 
      the supernet; it must be smaller than our prefix.  One and only one
536
 
      of *prefixlen_diff* and *new_prefix* must be set.  Returns a single
537
 
      network object.
538
 
 
539
 
         >>> ip_network('192.0.2.0/24').supernet()
540
 
         IPv4Network('192.0.2.0/23')
541
 
         >>> ip_network('192.0.2.0/24').supernet(prefixlen_diff=2)
542
 
         IPv4Network('192.0.0.0/22')
543
 
         >>> ip_network('192.0.2.0/24').supernet(new_prefix=20)
544
 
         IPv4Network('192.0.0.0/20')
545
 
 
546
 
   .. method:: compare_networks(other)
547
 
 
548
 
      Compare this network to *other*.  In this comparison only the network
549
 
      addresses are considered; host bits aren't.  Returns either ``-1``,
550
 
      ``0`` or ``1``.
551
 
 
552
 
         >>> ip_network('192.0.2.1/32').compare_networks(ip_network('192.0.2.2/32'))
553
 
         -1
554
 
         >>> ip_network('192.0.2.1/32').compare_networks(ip_network('192.0.2.0/32'))
555
 
         1
556
 
         >>> ip_network('192.0.2.1/32').compare_networks(ip_network('192.0.2.1/32'))
557
 
         0
558
 
 
559
 
 
560
 
.. class:: IPv6Network(address, strict=True)
561
 
 
562
 
   Construct an IPv6 network definition.  *address* can be one of the following:
563
 
 
564
 
   1. A string consisting of an IP address and an optional mask, separated by
565
 
      a slash (``/``).  The IP address is the network address, and the mask
566
 
      can be either a single number, which means it's a *prefix*, or a string
567
 
      representation of an IPv6 address.  If it's the latter, the mask is
568
 
      interpreted as a *net mask*.  If no mask is provided, it's considered to
569
 
      be ``/128``.
570
 
 
571
 
      For example, the following *address* specifications are equivalent:
572
 
      ``2001:db00::0/24`` and ``2001:db00::0/ffff:ff00::``.
573
 
 
574
 
   2. An integer that fits into 128 bits.  This is equivalent to a
575
 
      single-address network, with the network address being *address* and
576
 
      the mask being ``/128``.
577
 
 
578
 
   3. An integer packed into a :class:`bytes` object of length 16, big-endian.
579
 
      The interpretation is similar to an integer *address*.
580
 
 
581
 
   4. A two-tuple of an address description and a netmask, where the address
582
 
      description is either a string, a 128-bits integer, a 16-bytes packed
583
 
      integer, or an existing IPv4Address object; and the netmask is an
584
 
      integer representing the prefix length.
585
 
 
586
 
   An :exc:`AddressValueError` is raised if *address* is not a valid IPv6
587
 
   address.  A :exc:`NetmaskValueError` is raised if the mask is not valid for
588
 
   an IPv6 address.
589
 
 
590
 
   If *strict* is ``True`` and host bits are set in the supplied address,
591
 
   then :exc:`ValueError` is raised.  Otherwise, the host bits are masked out
592
 
   to determine the appropriate network address.
593
 
 
594
 
   .. versionchanged:: 3.5
595
 
 
596
 
      Added the two-tuple form for the *address* constructor parameter.
597
 
 
598
 
   .. attribute:: version
599
 
   .. attribute:: max_prefixlen
600
 
   .. attribute:: is_multicast
601
 
   .. attribute:: is_private
602
 
   .. attribute:: is_unspecified
603
 
   .. attribute:: is_reserved
604
 
   .. attribute:: is_loopback
605
 
   .. attribute:: is_link_local
606
 
   .. attribute:: network_address
607
 
   .. attribute:: broadcast_address
608
 
   .. attribute:: hostmask
609
 
   .. attribute:: with_prefixlen
610
 
   .. attribute:: compressed
611
 
   .. attribute:: exploded
612
 
   .. attribute:: with_netmask
613
 
   .. attribute:: with_hostmask
614
 
   .. attribute:: num_addresses
615
 
   .. attribute:: prefixlen
616
 
   .. method:: hosts()
617
 
   .. method:: overlaps(other)
618
 
   .. method:: address_exclude(network)
619
 
   .. method:: subnets(prefixlen_diff=1, new_prefix=None)
620
 
   .. method:: supernet(prefixlen_diff=1, new_prefix=None)
621
 
   .. method:: compare_networks(other)
622
 
 
623
 
      Refer to the corresponding attribute documentation in
624
 
      :class:`IPv4Network`
625
 
 
626
 
   .. attribute:: is_site_local
627
 
 
628
 
      These attribute is true for the network as a whole if it is true
629
 
      for both the network address and the broadcast address
630
 
 
631
 
 
632
 
Operators
633
 
^^^^^^^^^
634
 
 
635
 
Network objects support some operators.  Unless stated otherwise, operators can
636
 
only be applied between compatible objects (i.e. IPv4 with IPv4, IPv6 with
637
 
IPv6).
638
 
 
639
 
 
640
 
Logical operators
641
 
"""""""""""""""""
642
 
 
643
 
Network objects can be compared with the usual set of logical operators,
644
 
similarly to address objects.
645
 
 
646
 
 
647
 
Iteration
648
 
"""""""""
649
 
 
650
 
Network objects can be iterated to list all the addresses belonging to the
651
 
network.  For iteration, *all* hosts are returned, including unusable hosts
652
 
(for usable hosts, use the :meth:`~IPv4Network.hosts` method).  An
653
 
example::
654
 
 
655
 
   >>> for addr in IPv4Network('192.0.2.0/28'):
656
 
   ...   addr
657
 
   ...
658
 
   IPv4Address('192.0.2.0')
659
 
   IPv4Address('192.0.2.1')
660
 
   IPv4Address('192.0.2.2')
661
 
   IPv4Address('192.0.2.3')
662
 
   IPv4Address('192.0.2.4')
663
 
   IPv4Address('192.0.2.5')
664
 
   IPv4Address('192.0.2.6')
665
 
   IPv4Address('192.0.2.7')
666
 
   IPv4Address('192.0.2.8')
667
 
   IPv4Address('192.0.2.9')
668
 
   IPv4Address('192.0.2.10')
669
 
   IPv4Address('192.0.2.11')
670
 
   IPv4Address('192.0.2.12')
671
 
   IPv4Address('192.0.2.13')
672
 
   IPv4Address('192.0.2.14')
673
 
   IPv4Address('192.0.2.15')
674
 
 
675
 
 
676
 
Networks as containers of addresses
677
 
"""""""""""""""""""""""""""""""""""
678
 
 
679
 
Network objects can act as containers of addresses.  Some examples::
680
 
 
681
 
   >>> IPv4Network('192.0.2.0/28')[0]
682
 
   IPv4Address('192.0.2.0')
683
 
   >>> IPv4Network('192.0.2.0/28')[15]
684
 
   IPv4Address('192.0.2.15')
685
 
   >>> IPv4Address('192.0.2.6') in IPv4Network('192.0.2.0/28')
686
 
   True
687
 
   >>> IPv4Address('192.0.3.6') in IPv4Network('192.0.2.0/28')
688
 
   False
689
 
 
690
 
 
691
 
Interface objects
692
 
-----------------
693
 
 
694
 
.. class:: IPv4Interface(address)
695
 
 
696
 
   Construct an IPv4 interface.  The meaning of *address* is as in the
697
 
   constructor of :class:`IPv4Network`, except that arbitrary host addresses
698
 
   are always accepted.
699
 
 
700
 
   :class:`IPv4Interface` is a subclass of :class:`IPv4Address`, so it inherits
701
 
   all the attributes from that class.  In addition, the following attributes
702
 
   are available:
703
 
 
704
 
   .. attribute:: ip
705
 
 
706
 
      The address (:class:`IPv4Address`) without network information.
707
 
 
708
 
         >>> interface = IPv4Interface('192.0.2.5/24')
709
 
         >>> interface.ip
710
 
         IPv4Address('192.0.2.5')
711
 
 
712
 
   .. attribute:: network
713
 
 
714
 
      The network (:class:`IPv4Network`) this interface belongs to.
715
 
 
716
 
         >>> interface = IPv4Interface('192.0.2.5/24')
717
 
         >>> interface.network
718
 
         IPv4Network('192.0.2.0/24')
719
 
 
720
 
   .. attribute:: with_prefixlen
721
 
 
722
 
      A string representation of the interface with the mask in prefix notation.
723
 
 
724
 
         >>> interface = IPv4Interface('192.0.2.5/24')
725
 
         >>> interface.with_prefixlen
726
 
         '192.0.2.5/24'
727
 
 
728
 
   .. attribute:: with_netmask
729
 
 
730
 
      A string representation of the interface with the network as a net mask.
731
 
 
732
 
         >>> interface = IPv4Interface('192.0.2.5/24')
733
 
         >>> interface.with_netmask
734
 
         '192.0.2.5/255.255.255.0'
735
 
 
736
 
   .. attribute:: with_hostmask
737
 
 
738
 
      A string representation of the interface with the network as a host mask.
739
 
 
740
 
         >>> interface = IPv4Interface('192.0.2.5/24')
741
 
         >>> interface.with_hostmask
742
 
         '192.0.2.5/0.0.0.255'
743
 
 
744
 
 
745
 
.. class:: IPv6Interface(address)
746
 
 
747
 
   Construct an IPv6 interface.  The meaning of *address* is as in the
748
 
   constructor of :class:`IPv6Network`, except that arbitrary host addresses
749
 
   are always accepted.
750
 
 
751
 
   :class:`IPv6Interface` is a subclass of :class:`IPv6Address`, so it inherits
752
 
   all the attributes from that class.  In addition, the following attributes
753
 
   are available:
754
 
 
755
 
   .. attribute:: ip
756
 
   .. attribute:: network
757
 
   .. attribute:: with_prefixlen
758
 
   .. attribute:: with_netmask
759
 
   .. attribute:: with_hostmask
760
 
 
761
 
      Refer to the corresponding attribute documentation in
762
 
      :class:`IPv4Interface`.
763
 
 
764
 
 
765
 
Other Module Level Functions
766
 
----------------------------
767
 
 
768
 
The module also provides the following module level functions:
769
 
 
770
 
.. function:: v4_int_to_packed(address)
771
 
 
772
 
   Represent an address as 4 packed bytes in network (big-endian) order.
773
 
   *address* is an integer representation of an IPv4 IP address.  A
774
 
   :exc:`ValueError` is raised if the integer is negative or too large to be an
775
 
   IPv4 IP address.
776
 
 
777
 
   >>> ipaddress.ip_address(3221225985)
778
 
   IPv4Address('192.0.2.1')
779
 
   >>> ipaddress.v4_int_to_packed(3221225985)
780
 
   b'\xc0\x00\x02\x01'
781
 
 
782
 
 
783
 
.. function:: v6_int_to_packed(address)
784
 
 
785
 
   Represent an address as 16 packed bytes in network (big-endian) order.
786
 
   *address* is an integer representation of an IPv6 IP address.  A
787
 
   :exc:`ValueError` is raised if the integer is negative or too large to be an
788
 
   IPv6 IP address.
789
 
 
790
 
 
791
 
.. function:: summarize_address_range(first, last)
792
 
 
793
 
   Return an iterator of the summarized network range given the first and last
794
 
   IP addresses.  *first* is the first :class:`IPv4Address` or
795
 
   :class:`IPv6Address` in the range and *last* is the last :class:`IPv4Address`
796
 
   or :class:`IPv6Address` in the range.  A :exc:`TypeError` is raised if
797
 
   *first* or *last* are not IP addresses or are not of the same version.  A
798
 
   :exc:`ValueError` is raised if *last* is not greater than *first* or if
799
 
   *first* address version is not 4 or 6.
800
 
 
801
 
   >>> [ipaddr for ipaddr in ipaddress.summarize_address_range(
802
 
   ...    ipaddress.IPv4Address('192.0.2.0'),
803
 
   ...    ipaddress.IPv4Address('192.0.2.130'))]
804
 
   [IPv4Network('192.0.2.0/25'), IPv4Network('192.0.2.128/31'), IPv4Network('192.0.2.130/32')]
805
 
 
806
 
 
807
 
.. function:: collapse_addresses(addresses)
808
 
 
809
 
   Return an iterator of the collapsed :class:`IPv4Network` or
810
 
   :class:`IPv6Network` objects.  *addresses* is an iterator of
811
 
   :class:`IPv4Network` or :class:`IPv6Network` objects.  A :exc:`TypeError` is
812
 
   raised if *addresses* contains mixed version objects.
813
 
 
814
 
   >>> [ipaddr for ipaddr in
815
 
   ... ipaddress.collapse_addresses([ipaddress.IPv4Network('192.0.2.0/25'),
816
 
   ... ipaddress.IPv4Network('192.0.2.128/25')])]
817
 
   [IPv4Network('192.0.2.0/24')]
818
 
 
819
 
 
820
 
.. function:: get_mixed_type_key(obj)
821
 
 
822
 
   Return a key suitable for sorting between networks and addresses.  Address
823
 
   and Network objects are not sortable by default; they're fundamentally
824
 
   different, so the expression::
825
 
 
826
 
     IPv4Address('192.0.2.0') <= IPv4Network('192.0.2.0/24')
827
 
 
828
 
   doesn't make sense.  There are some times however, where you may wish to
829
 
   have :mod:`ipaddress` sort these anyway.  If you need to do this, you can use
830
 
   this function as the ``key`` argument to :func:`sorted()`.
831
 
 
832
 
   *obj* is either a network or address object.
833
 
 
834
 
 
835
 
Custom Exceptions
836
 
-----------------
837
 
 
838
 
To support more specific error reporting from class constructors, the
839
 
module defines the following exceptions:
840
 
 
841
 
.. exception:: AddressValueError(ValueError)
842
 
 
843
 
   Any value error related to the address.
844
 
 
845
 
 
846
 
.. exception:: NetmaskValueError(ValueError)
847
 
 
848
 
   Any value error related to the netmask.