xref: /third_party/python/Doc/howto/ipaddress.rst (revision 7db96d56)
17db96d56Sopenharmony_ci.. testsetup::
27db96d56Sopenharmony_ci
37db96d56Sopenharmony_ci   import ipaddress
47db96d56Sopenharmony_ci
57db96d56Sopenharmony_ci.. _ipaddress-howto:
67db96d56Sopenharmony_ci
77db96d56Sopenharmony_ci***************************************
87db96d56Sopenharmony_ciAn introduction to the ipaddress module
97db96d56Sopenharmony_ci***************************************
107db96d56Sopenharmony_ci
117db96d56Sopenharmony_ci:author: Peter Moody
127db96d56Sopenharmony_ci:author: Nick Coghlan
137db96d56Sopenharmony_ci
147db96d56Sopenharmony_ci.. topic:: Overview
157db96d56Sopenharmony_ci
167db96d56Sopenharmony_ci   This document aims to provide a gentle introduction to the
177db96d56Sopenharmony_ci   :mod:`ipaddress` module. It is aimed primarily at users that aren't
187db96d56Sopenharmony_ci   already familiar with IP networking terminology, but may also be useful
197db96d56Sopenharmony_ci   to network engineers wanting an overview of how :mod:`ipaddress`
207db96d56Sopenharmony_ci   represents IP network addressing concepts.
217db96d56Sopenharmony_ci
227db96d56Sopenharmony_ci
237db96d56Sopenharmony_ciCreating Address/Network/Interface objects
247db96d56Sopenharmony_ci==========================================
257db96d56Sopenharmony_ci
267db96d56Sopenharmony_ciSince :mod:`ipaddress` is a module for inspecting and manipulating IP addresses,
277db96d56Sopenharmony_cithe first thing you'll want to do is create some objects.  You can use
287db96d56Sopenharmony_ci:mod:`ipaddress` to create objects from strings and integers.
297db96d56Sopenharmony_ci
307db96d56Sopenharmony_ci
317db96d56Sopenharmony_ciA Note on IP Versions
327db96d56Sopenharmony_ci---------------------
337db96d56Sopenharmony_ci
347db96d56Sopenharmony_ciFor readers that aren't particularly familiar with IP addressing, it's
357db96d56Sopenharmony_ciimportant to know that the Internet Protocol (IP) is currently in the process
367db96d56Sopenharmony_ciof moving from version 4 of the protocol to version 6. This transition is
377db96d56Sopenharmony_cioccurring largely because version 4 of the protocol doesn't provide enough
387db96d56Sopenharmony_ciaddresses to handle the needs of the whole world, especially given the
397db96d56Sopenharmony_ciincreasing number of devices with direct connections to the internet.
407db96d56Sopenharmony_ci
417db96d56Sopenharmony_ciExplaining the details of the differences between the two versions of the
427db96d56Sopenharmony_ciprotocol is beyond the scope of this introduction, but readers need to at
437db96d56Sopenharmony_cileast be aware that these two versions exist, and it will sometimes be
447db96d56Sopenharmony_cinecessary to force the use of one version or the other.
457db96d56Sopenharmony_ci
467db96d56Sopenharmony_ci
477db96d56Sopenharmony_ciIP Host Addresses
487db96d56Sopenharmony_ci-----------------
497db96d56Sopenharmony_ci
507db96d56Sopenharmony_ciAddresses, often referred to as "host addresses" are the most basic unit
517db96d56Sopenharmony_ciwhen working with IP addressing. The simplest way to create addresses is
527db96d56Sopenharmony_cito use the :func:`ipaddress.ip_address` factory function, which automatically
537db96d56Sopenharmony_cidetermines whether to create an IPv4 or IPv6 address based on the passed in
547db96d56Sopenharmony_civalue:
557db96d56Sopenharmony_ci
567db96d56Sopenharmony_ci   >>> ipaddress.ip_address('192.0.2.1')
577db96d56Sopenharmony_ci   IPv4Address('192.0.2.1')
587db96d56Sopenharmony_ci   >>> ipaddress.ip_address('2001:DB8::1')
597db96d56Sopenharmony_ci   IPv6Address('2001:db8::1')
607db96d56Sopenharmony_ci
617db96d56Sopenharmony_ciAddresses can also be created directly from integers. Values that will
627db96d56Sopenharmony_cifit within 32 bits are assumed to be IPv4 addresses::
637db96d56Sopenharmony_ci
647db96d56Sopenharmony_ci   >>> ipaddress.ip_address(3221225985)
657db96d56Sopenharmony_ci   IPv4Address('192.0.2.1')
667db96d56Sopenharmony_ci   >>> ipaddress.ip_address(42540766411282592856903984951653826561)
677db96d56Sopenharmony_ci   IPv6Address('2001:db8::1')
687db96d56Sopenharmony_ci
697db96d56Sopenharmony_ciTo force the use of IPv4 or IPv6 addresses, the relevant classes can be
707db96d56Sopenharmony_ciinvoked directly. This is particularly useful to force creation of IPv6
717db96d56Sopenharmony_ciaddresses for small integers::
727db96d56Sopenharmony_ci
737db96d56Sopenharmony_ci   >>> ipaddress.ip_address(1)
747db96d56Sopenharmony_ci   IPv4Address('0.0.0.1')
757db96d56Sopenharmony_ci   >>> ipaddress.IPv4Address(1)
767db96d56Sopenharmony_ci   IPv4Address('0.0.0.1')
777db96d56Sopenharmony_ci   >>> ipaddress.IPv6Address(1)
787db96d56Sopenharmony_ci   IPv6Address('::1')
797db96d56Sopenharmony_ci
807db96d56Sopenharmony_ci
817db96d56Sopenharmony_ciDefining Networks
827db96d56Sopenharmony_ci-----------------
837db96d56Sopenharmony_ci
847db96d56Sopenharmony_ciHost addresses are usually grouped together into IP networks, so
857db96d56Sopenharmony_ci:mod:`ipaddress` provides a way to create, inspect and manipulate network
867db96d56Sopenharmony_cidefinitions. IP network objects are constructed from strings that define the
877db96d56Sopenharmony_cirange of host addresses that are part of that network. The simplest form
887db96d56Sopenharmony_cifor that information is a "network address/network prefix" pair, where the
897db96d56Sopenharmony_ciprefix defines the number of leading bits that are compared to determine
907db96d56Sopenharmony_ciwhether or not an address is part of the network and the network address
917db96d56Sopenharmony_cidefines the expected value of those bits.
927db96d56Sopenharmony_ci
937db96d56Sopenharmony_ciAs for addresses, a factory function is provided that determines the correct
947db96d56Sopenharmony_ciIP version automatically::
957db96d56Sopenharmony_ci
967db96d56Sopenharmony_ci   >>> ipaddress.ip_network('192.0.2.0/24')
977db96d56Sopenharmony_ci   IPv4Network('192.0.2.0/24')
987db96d56Sopenharmony_ci   >>> ipaddress.ip_network('2001:db8::0/96')
997db96d56Sopenharmony_ci   IPv6Network('2001:db8::/96')
1007db96d56Sopenharmony_ci
1017db96d56Sopenharmony_ciNetwork objects cannot have any host bits set.  The practical effect of this
1027db96d56Sopenharmony_ciis that ``192.0.2.1/24`` does not describe a network.  Such definitions are
1037db96d56Sopenharmony_cireferred to as interface objects since the ip-on-a-network notation is
1047db96d56Sopenharmony_cicommonly used to describe network interfaces of a computer on a given network
1057db96d56Sopenharmony_ciand are described further in the next section.
1067db96d56Sopenharmony_ci
1077db96d56Sopenharmony_ciBy default, attempting to create a network object with host bits set will
1087db96d56Sopenharmony_ciresult in :exc:`ValueError` being raised. To request that the
1097db96d56Sopenharmony_ciadditional bits instead be coerced to zero, the flag ``strict=False`` can
1107db96d56Sopenharmony_cibe passed to the constructor::
1117db96d56Sopenharmony_ci
1127db96d56Sopenharmony_ci   >>> ipaddress.ip_network('192.0.2.1/24')
1137db96d56Sopenharmony_ci   Traceback (most recent call last):
1147db96d56Sopenharmony_ci      ...
1157db96d56Sopenharmony_ci   ValueError: 192.0.2.1/24 has host bits set
1167db96d56Sopenharmony_ci   >>> ipaddress.ip_network('192.0.2.1/24', strict=False)
1177db96d56Sopenharmony_ci   IPv4Network('192.0.2.0/24')
1187db96d56Sopenharmony_ci
1197db96d56Sopenharmony_ciWhile the string form offers significantly more flexibility, networks can
1207db96d56Sopenharmony_cialso be defined with integers, just like host addresses. In this case, the
1217db96d56Sopenharmony_cinetwork is considered to contain only the single address identified by the
1227db96d56Sopenharmony_ciinteger, so the network prefix includes the entire network address::
1237db96d56Sopenharmony_ci
1247db96d56Sopenharmony_ci   >>> ipaddress.ip_network(3221225984)
1257db96d56Sopenharmony_ci   IPv4Network('192.0.2.0/32')
1267db96d56Sopenharmony_ci   >>> ipaddress.ip_network(42540766411282592856903984951653826560)
1277db96d56Sopenharmony_ci   IPv6Network('2001:db8::/128')
1287db96d56Sopenharmony_ci
1297db96d56Sopenharmony_ciAs with addresses, creation of a particular kind of network can be forced
1307db96d56Sopenharmony_ciby calling the class constructor directly instead of using the factory
1317db96d56Sopenharmony_cifunction.
1327db96d56Sopenharmony_ci
1337db96d56Sopenharmony_ci
1347db96d56Sopenharmony_ciHost Interfaces
1357db96d56Sopenharmony_ci---------------
1367db96d56Sopenharmony_ci
1377db96d56Sopenharmony_ciAs mentioned just above, if you need to describe an address on a particular
1387db96d56Sopenharmony_cinetwork, neither the address nor the network classes are sufficient.
1397db96d56Sopenharmony_ciNotation like ``192.0.2.1/24`` is commonly used by network engineers and the
1407db96d56Sopenharmony_cipeople who write tools for firewalls and routers as shorthand for "the host
1417db96d56Sopenharmony_ci``192.0.2.1`` on the network ``192.0.2.0/24``", Accordingly, :mod:`ipaddress`
1427db96d56Sopenharmony_ciprovides a set of hybrid classes that associate an address with a particular
1437db96d56Sopenharmony_cinetwork. The interface for creation is identical to that for defining network
1447db96d56Sopenharmony_ciobjects, except that the address portion isn't constrained to being a network
1457db96d56Sopenharmony_ciaddress.
1467db96d56Sopenharmony_ci
1477db96d56Sopenharmony_ci   >>> ipaddress.ip_interface('192.0.2.1/24')
1487db96d56Sopenharmony_ci   IPv4Interface('192.0.2.1/24')
1497db96d56Sopenharmony_ci   >>> ipaddress.ip_interface('2001:db8::1/96')
1507db96d56Sopenharmony_ci   IPv6Interface('2001:db8::1/96')
1517db96d56Sopenharmony_ci
1527db96d56Sopenharmony_ciInteger inputs are accepted (as with networks), and use of a particular IP
1537db96d56Sopenharmony_civersion can be forced by calling the relevant constructor directly.
1547db96d56Sopenharmony_ci
1557db96d56Sopenharmony_ci
1567db96d56Sopenharmony_ciInspecting Address/Network/Interface Objects
1577db96d56Sopenharmony_ci============================================
1587db96d56Sopenharmony_ci
1597db96d56Sopenharmony_ciYou've gone to the trouble of creating an IPv(4|6)(Address|Network|Interface)
1607db96d56Sopenharmony_ciobject, so you probably want to get information about it.  :mod:`ipaddress`
1617db96d56Sopenharmony_citries to make doing this easy and intuitive.
1627db96d56Sopenharmony_ci
1637db96d56Sopenharmony_ciExtracting the IP version::
1647db96d56Sopenharmony_ci
1657db96d56Sopenharmony_ci   >>> addr4 = ipaddress.ip_address('192.0.2.1')
1667db96d56Sopenharmony_ci   >>> addr6 = ipaddress.ip_address('2001:db8::1')
1677db96d56Sopenharmony_ci   >>> addr6.version
1687db96d56Sopenharmony_ci   6
1697db96d56Sopenharmony_ci   >>> addr4.version
1707db96d56Sopenharmony_ci   4
1717db96d56Sopenharmony_ci
1727db96d56Sopenharmony_ciObtaining the network from an interface::
1737db96d56Sopenharmony_ci
1747db96d56Sopenharmony_ci   >>> host4 = ipaddress.ip_interface('192.0.2.1/24')
1757db96d56Sopenharmony_ci   >>> host4.network
1767db96d56Sopenharmony_ci   IPv4Network('192.0.2.0/24')
1777db96d56Sopenharmony_ci   >>> host6 = ipaddress.ip_interface('2001:db8::1/96')
1787db96d56Sopenharmony_ci   >>> host6.network
1797db96d56Sopenharmony_ci   IPv6Network('2001:db8::/96')
1807db96d56Sopenharmony_ci
1817db96d56Sopenharmony_ciFinding out how many individual addresses are in a network::
1827db96d56Sopenharmony_ci
1837db96d56Sopenharmony_ci   >>> net4 = ipaddress.ip_network('192.0.2.0/24')
1847db96d56Sopenharmony_ci   >>> net4.num_addresses
1857db96d56Sopenharmony_ci   256
1867db96d56Sopenharmony_ci   >>> net6 = ipaddress.ip_network('2001:db8::0/96')
1877db96d56Sopenharmony_ci   >>> net6.num_addresses
1887db96d56Sopenharmony_ci   4294967296
1897db96d56Sopenharmony_ci
1907db96d56Sopenharmony_ciIterating through the "usable" addresses on a network::
1917db96d56Sopenharmony_ci
1927db96d56Sopenharmony_ci   >>> net4 = ipaddress.ip_network('192.0.2.0/24')
1937db96d56Sopenharmony_ci   >>> for x in net4.hosts():
1947db96d56Sopenharmony_ci   ...     print(x)  # doctest: +ELLIPSIS
1957db96d56Sopenharmony_ci   192.0.2.1
1967db96d56Sopenharmony_ci   192.0.2.2
1977db96d56Sopenharmony_ci   192.0.2.3
1987db96d56Sopenharmony_ci   192.0.2.4
1997db96d56Sopenharmony_ci   ...
2007db96d56Sopenharmony_ci   192.0.2.252
2017db96d56Sopenharmony_ci   192.0.2.253
2027db96d56Sopenharmony_ci   192.0.2.254
2037db96d56Sopenharmony_ci
2047db96d56Sopenharmony_ci
2057db96d56Sopenharmony_ciObtaining the netmask (i.e. set bits corresponding to the network prefix) or
2067db96d56Sopenharmony_cithe hostmask (any bits that are not part of the netmask):
2077db96d56Sopenharmony_ci
2087db96d56Sopenharmony_ci   >>> net4 = ipaddress.ip_network('192.0.2.0/24')
2097db96d56Sopenharmony_ci   >>> net4.netmask
2107db96d56Sopenharmony_ci   IPv4Address('255.255.255.0')
2117db96d56Sopenharmony_ci   >>> net4.hostmask
2127db96d56Sopenharmony_ci   IPv4Address('0.0.0.255')
2137db96d56Sopenharmony_ci   >>> net6 = ipaddress.ip_network('2001:db8::0/96')
2147db96d56Sopenharmony_ci   >>> net6.netmask
2157db96d56Sopenharmony_ci   IPv6Address('ffff:ffff:ffff:ffff:ffff:ffff::')
2167db96d56Sopenharmony_ci   >>> net6.hostmask
2177db96d56Sopenharmony_ci   IPv6Address('::ffff:ffff')
2187db96d56Sopenharmony_ci
2197db96d56Sopenharmony_ci
2207db96d56Sopenharmony_ciExploding or compressing the address::
2217db96d56Sopenharmony_ci
2227db96d56Sopenharmony_ci   >>> addr6.exploded
2237db96d56Sopenharmony_ci   '2001:0db8:0000:0000:0000:0000:0000:0001'
2247db96d56Sopenharmony_ci   >>> addr6.compressed
2257db96d56Sopenharmony_ci   '2001:db8::1'
2267db96d56Sopenharmony_ci   >>> net6.exploded
2277db96d56Sopenharmony_ci   '2001:0db8:0000:0000:0000:0000:0000:0000/96'
2287db96d56Sopenharmony_ci   >>> net6.compressed
2297db96d56Sopenharmony_ci   '2001:db8::/96'
2307db96d56Sopenharmony_ci
2317db96d56Sopenharmony_ciWhile IPv4 doesn't support explosion or compression, the associated objects
2327db96d56Sopenharmony_cistill provide the relevant properties so that version neutral code can
2337db96d56Sopenharmony_cieasily ensure the most concise or most verbose form is used for IPv6
2347db96d56Sopenharmony_ciaddresses while still correctly handling IPv4 addresses.
2357db96d56Sopenharmony_ci
2367db96d56Sopenharmony_ci
2377db96d56Sopenharmony_ciNetworks as lists of Addresses
2387db96d56Sopenharmony_ci==============================
2397db96d56Sopenharmony_ci
2407db96d56Sopenharmony_ciIt's sometimes useful to treat networks as lists.  This means it is possible
2417db96d56Sopenharmony_cito index them like this::
2427db96d56Sopenharmony_ci
2437db96d56Sopenharmony_ci   >>> net4[1]
2447db96d56Sopenharmony_ci   IPv4Address('192.0.2.1')
2457db96d56Sopenharmony_ci   >>> net4[-1]
2467db96d56Sopenharmony_ci   IPv4Address('192.0.2.255')
2477db96d56Sopenharmony_ci   >>> net6[1]
2487db96d56Sopenharmony_ci   IPv6Address('2001:db8::1')
2497db96d56Sopenharmony_ci   >>> net6[-1]
2507db96d56Sopenharmony_ci   IPv6Address('2001:db8::ffff:ffff')
2517db96d56Sopenharmony_ci
2527db96d56Sopenharmony_ci
2537db96d56Sopenharmony_ciIt also means that network objects lend themselves to using the list
2547db96d56Sopenharmony_cimembership test syntax like this::
2557db96d56Sopenharmony_ci
2567db96d56Sopenharmony_ci   if address in network:
2577db96d56Sopenharmony_ci       # do something
2587db96d56Sopenharmony_ci
2597db96d56Sopenharmony_ciContainment testing is done efficiently based on the network prefix::
2607db96d56Sopenharmony_ci
2617db96d56Sopenharmony_ci   >>> addr4 = ipaddress.ip_address('192.0.2.1')
2627db96d56Sopenharmony_ci   >>> addr4 in ipaddress.ip_network('192.0.2.0/24')
2637db96d56Sopenharmony_ci   True
2647db96d56Sopenharmony_ci   >>> addr4 in ipaddress.ip_network('192.0.3.0/24')
2657db96d56Sopenharmony_ci   False
2667db96d56Sopenharmony_ci
2677db96d56Sopenharmony_ci
2687db96d56Sopenharmony_ciComparisons
2697db96d56Sopenharmony_ci===========
2707db96d56Sopenharmony_ci
2717db96d56Sopenharmony_ci:mod:`ipaddress` provides some simple, hopefully intuitive ways to compare
2727db96d56Sopenharmony_ciobjects, where it makes sense::
2737db96d56Sopenharmony_ci
2747db96d56Sopenharmony_ci   >>> ipaddress.ip_address('192.0.2.1') < ipaddress.ip_address('192.0.2.2')
2757db96d56Sopenharmony_ci   True
2767db96d56Sopenharmony_ci
2777db96d56Sopenharmony_ciA :exc:`TypeError` exception is raised if you try to compare objects of
2787db96d56Sopenharmony_cidifferent versions or different types.
2797db96d56Sopenharmony_ci
2807db96d56Sopenharmony_ci
2817db96d56Sopenharmony_ciUsing IP Addresses with other modules
2827db96d56Sopenharmony_ci=====================================
2837db96d56Sopenharmony_ci
2847db96d56Sopenharmony_ciOther modules that use IP addresses (such as :mod:`socket`) usually won't
2857db96d56Sopenharmony_ciaccept objects from this module directly. Instead, they must be coerced to
2867db96d56Sopenharmony_cian integer or string that the other module will accept::
2877db96d56Sopenharmony_ci
2887db96d56Sopenharmony_ci   >>> addr4 = ipaddress.ip_address('192.0.2.1')
2897db96d56Sopenharmony_ci   >>> str(addr4)
2907db96d56Sopenharmony_ci   '192.0.2.1'
2917db96d56Sopenharmony_ci   >>> int(addr4)
2927db96d56Sopenharmony_ci   3221225985
2937db96d56Sopenharmony_ci
2947db96d56Sopenharmony_ci
2957db96d56Sopenharmony_ciGetting more detail when instance creation fails
2967db96d56Sopenharmony_ci================================================
2977db96d56Sopenharmony_ci
2987db96d56Sopenharmony_ciWhen creating address/network/interface objects using the version-agnostic
2997db96d56Sopenharmony_cifactory functions, any errors will be reported as :exc:`ValueError` with
3007db96d56Sopenharmony_cia generic error message that simply says the passed in value was not
3017db96d56Sopenharmony_cirecognized as an object of that type. The lack of a specific error is
3027db96d56Sopenharmony_cibecause it's necessary to know whether the value is *supposed* to be IPv4
3037db96d56Sopenharmony_cior IPv6 in order to provide more detail on why it has been rejected.
3047db96d56Sopenharmony_ci
3057db96d56Sopenharmony_ciTo support use cases where it is useful to have access to this additional
3067db96d56Sopenharmony_cidetail, the individual class constructors actually raise the
3077db96d56Sopenharmony_ci:exc:`ValueError` subclasses :exc:`ipaddress.AddressValueError` and
3087db96d56Sopenharmony_ci:exc:`ipaddress.NetmaskValueError` to indicate exactly which part of
3097db96d56Sopenharmony_cithe definition failed to parse correctly.
3107db96d56Sopenharmony_ci
3117db96d56Sopenharmony_ciThe error messages are significantly more detailed when using the
3127db96d56Sopenharmony_ciclass constructors directly. For example::
3137db96d56Sopenharmony_ci
3147db96d56Sopenharmony_ci   >>> ipaddress.ip_address("192.168.0.256")
3157db96d56Sopenharmony_ci   Traceback (most recent call last):
3167db96d56Sopenharmony_ci     ...
3177db96d56Sopenharmony_ci   ValueError: '192.168.0.256' does not appear to be an IPv4 or IPv6 address
3187db96d56Sopenharmony_ci   >>> ipaddress.IPv4Address("192.168.0.256")
3197db96d56Sopenharmony_ci   Traceback (most recent call last):
3207db96d56Sopenharmony_ci     ...
3217db96d56Sopenharmony_ci   ipaddress.AddressValueError: Octet 256 (> 255) not permitted in '192.168.0.256'
3227db96d56Sopenharmony_ci
3237db96d56Sopenharmony_ci   >>> ipaddress.ip_network("192.168.0.1/64")
3247db96d56Sopenharmony_ci   Traceback (most recent call last):
3257db96d56Sopenharmony_ci     ...
3267db96d56Sopenharmony_ci   ValueError: '192.168.0.1/64' does not appear to be an IPv4 or IPv6 network
3277db96d56Sopenharmony_ci   >>> ipaddress.IPv4Network("192.168.0.1/64")
3287db96d56Sopenharmony_ci   Traceback (most recent call last):
3297db96d56Sopenharmony_ci     ...
3307db96d56Sopenharmony_ci   ipaddress.NetmaskValueError: '64' is not a valid netmask
3317db96d56Sopenharmony_ci
3327db96d56Sopenharmony_ciHowever, both of the module specific exceptions have :exc:`ValueError` as their
3337db96d56Sopenharmony_ciparent class, so if you're not concerned with the particular type of error,
3347db96d56Sopenharmony_ciyou can still write code like the following::
3357db96d56Sopenharmony_ci
3367db96d56Sopenharmony_ci   try:
3377db96d56Sopenharmony_ci       network = ipaddress.IPv4Network(address)
3387db96d56Sopenharmony_ci   except ValueError:
3397db96d56Sopenharmony_ci       print('address/netmask is invalid for IPv4:', address)
3407db96d56Sopenharmony_ci
341