summaryrefslogtreecommitdiff
path: root/devdocs/python~3.12/howto%2Fipaddress.html
diff options
context:
space:
mode:
Diffstat (limited to 'devdocs/python~3.12/howto%2Fipaddress.html')
-rw-r--r--devdocs/python~3.12/howto%2Fipaddress.html130
1 files changed, 130 insertions, 0 deletions
diff --git a/devdocs/python~3.12/howto%2Fipaddress.html b/devdocs/python~3.12/howto%2Fipaddress.html
new file mode 100644
index 00000000..da202bde
--- /dev/null
+++ b/devdocs/python~3.12/howto%2Fipaddress.html
@@ -0,0 +1,130 @@
+ <span id="ipaddress-howto"></span><h1>An introduction to the ipaddress module</h1> <dl class="field-list simple"> <dt class="field-odd">author</dt> <dd class="field-odd">
+<p>Peter Moody</p> </dd> <dt class="field-even">author</dt> <dd class="field-even">
+<p>Nick Coghlan</p> </dd> </dl> <div class="topic"> <p class="topic-title">Overview</p> <p>This document aims to provide a gentle introduction to the <a class="reference internal" href="../library/ipaddress#module-ipaddress" title="ipaddress: IPv4/IPv6 manipulation library."><code>ipaddress</code></a> module. It is aimed primarily at users that aren’t already familiar with IP networking terminology, but may also be useful to network engineers wanting an overview of how <a class="reference internal" href="../library/ipaddress#module-ipaddress" title="ipaddress: IPv4/IPv6 manipulation library."><code>ipaddress</code></a> represents IP network addressing concepts.</p> </div> <section id="creating-address-network-interface-objects"> <h2>Creating Address/Network/Interface objects</h2> <p>Since <a class="reference internal" href="../library/ipaddress#module-ipaddress" title="ipaddress: IPv4/IPv6 manipulation library."><code>ipaddress</code></a> is a module for inspecting and manipulating IP addresses, the first thing you’ll want to do is create some objects. You can use <a class="reference internal" href="../library/ipaddress#module-ipaddress" title="ipaddress: IPv4/IPv6 manipulation library."><code>ipaddress</code></a> to create objects from strings and integers.</p> <section id="a-note-on-ip-versions"> <h3>A Note on IP Versions</h3> <p>For readers that aren’t particularly familiar with IP addressing, it’s important to know that the Internet Protocol (IP) is currently in the process of moving from version 4 of the protocol to version 6. This transition is occurring largely because version 4 of the protocol doesn’t provide enough addresses to handle the needs of the whole world, especially given the increasing number of devices with direct connections to the internet.</p> <p>Explaining the details of the differences between the two versions of the protocol is beyond the scope of this introduction, but readers need to at least be aware that these two versions exist, and it will sometimes be necessary to force the use of one version or the other.</p> </section> <section id="ip-host-addresses"> <h3>IP Host Addresses</h3> <p>Addresses, often referred to as “host addresses” are the most basic unit when working with IP addressing. The simplest way to create addresses is to use the <a class="reference internal" href="../library/ipaddress#ipaddress.ip_address" title="ipaddress.ip_address"><code>ipaddress.ip_address()</code></a> factory function, which automatically determines whether to create an IPv4 or IPv6 address based on the passed in value:</p> <pre data-language="python">&gt;&gt;&gt; ipaddress.ip_address('192.0.2.1')
+IPv4Address('192.0.2.1')
+&gt;&gt;&gt; ipaddress.ip_address('2001:DB8::1')
+IPv6Address('2001:db8::1')
+</pre> <p>Addresses can also be created directly from integers. Values that will fit within 32 bits are assumed to be IPv4 addresses:</p> <pre data-language="python">&gt;&gt;&gt; ipaddress.ip_address(3221225985)
+IPv4Address('192.0.2.1')
+&gt;&gt;&gt; ipaddress.ip_address(42540766411282592856903984951653826561)
+IPv6Address('2001:db8::1')
+</pre> <p>To force the use of IPv4 or IPv6 addresses, the relevant classes can be invoked directly. This is particularly useful to force creation of IPv6 addresses for small integers:</p> <pre data-language="python">&gt;&gt;&gt; ipaddress.ip_address(1)
+IPv4Address('0.0.0.1')
+&gt;&gt;&gt; ipaddress.IPv4Address(1)
+IPv4Address('0.0.0.1')
+&gt;&gt;&gt; ipaddress.IPv6Address(1)
+IPv6Address('::1')
+</pre> </section> <section id="defining-networks"> <h3>Defining Networks</h3> <p>Host addresses are usually grouped together into IP networks, so <a class="reference internal" href="../library/ipaddress#module-ipaddress" title="ipaddress: IPv4/IPv6 manipulation library."><code>ipaddress</code></a> provides a way to create, inspect and manipulate network definitions. IP network objects are constructed from strings that define the range of host addresses that are part of that network. The simplest form for that information is a “network address/network prefix” pair, where the prefix defines the number of leading bits that are compared to determine whether or not an address is part of the network and the network address defines the expected value of those bits.</p> <p>As for addresses, a factory function is provided that determines the correct IP version automatically:</p> <pre data-language="python">&gt;&gt;&gt; ipaddress.ip_network('192.0.2.0/24')
+IPv4Network('192.0.2.0/24')
+&gt;&gt;&gt; ipaddress.ip_network('2001:db8::0/96')
+IPv6Network('2001:db8::/96')
+</pre> <p>Network objects cannot have any host bits set. The practical effect of this is that <code>192.0.2.1/24</code> does not describe a network. Such definitions are referred to as interface objects since the ip-on-a-network notation is commonly used to describe network interfaces of a computer on a given network and are described further in the next section.</p> <p>By default, attempting to create a network object with host bits set will result in <a class="reference internal" href="../library/exceptions#ValueError" title="ValueError"><code>ValueError</code></a> being raised. To request that the additional bits instead be coerced to zero, the flag <code>strict=False</code> can be passed to the constructor:</p> <pre data-language="python">&gt;&gt;&gt; ipaddress.ip_network('192.0.2.1/24')
+Traceback (most recent call last):
+ ...
+ValueError: 192.0.2.1/24 has host bits set
+&gt;&gt;&gt; ipaddress.ip_network('192.0.2.1/24', strict=False)
+IPv4Network('192.0.2.0/24')
+</pre> <p>While the string form offers significantly more flexibility, networks can also be defined with integers, just like host addresses. In this case, the network is considered to contain only the single address identified by the integer, so the network prefix includes the entire network address:</p> <pre data-language="python">&gt;&gt;&gt; ipaddress.ip_network(3221225984)
+IPv4Network('192.0.2.0/32')
+&gt;&gt;&gt; ipaddress.ip_network(42540766411282592856903984951653826560)
+IPv6Network('2001:db8::/128')
+</pre> <p>As with addresses, creation of a particular kind of network can be forced by calling the class constructor directly instead of using the factory function.</p> </section> <section id="host-interfaces"> <h3>Host Interfaces</h3> <p>As mentioned just above, if you need to describe an address on a particular network, neither the address nor the network classes are sufficient. Notation like <code>192.0.2.1/24</code> is commonly used by network engineers and the people who write tools for firewalls and routers as shorthand for “the host <code>192.0.2.1</code> on the network <code>192.0.2.0/24</code>”, Accordingly, <a class="reference internal" href="../library/ipaddress#module-ipaddress" title="ipaddress: IPv4/IPv6 manipulation library."><code>ipaddress</code></a> provides a set of hybrid classes that associate an address with a particular network. The interface for creation is identical to that for defining network objects, except that the address portion isn’t constrained to being a network address.</p> <pre data-language="python">&gt;&gt;&gt; ipaddress.ip_interface('192.0.2.1/24')
+IPv4Interface('192.0.2.1/24')
+&gt;&gt;&gt; ipaddress.ip_interface('2001:db8::1/96')
+IPv6Interface('2001:db8::1/96')
+</pre> <p>Integer inputs are accepted (as with networks), and use of a particular IP version can be forced by calling the relevant constructor directly.</p> </section> </section> <section id="inspecting-address-network-interface-objects"> <h2>Inspecting Address/Network/Interface Objects</h2> <p>You’ve gone to the trouble of creating an IPv(4|6)(Address|Network|Interface) object, so you probably want to get information about it. <a class="reference internal" href="../library/ipaddress#module-ipaddress" title="ipaddress: IPv4/IPv6 manipulation library."><code>ipaddress</code></a> tries to make doing this easy and intuitive.</p> <p>Extracting the IP version:</p> <pre data-language="python">&gt;&gt;&gt; addr4 = ipaddress.ip_address('192.0.2.1')
+&gt;&gt;&gt; addr6 = ipaddress.ip_address('2001:db8::1')
+&gt;&gt;&gt; addr6.version
+6
+&gt;&gt;&gt; addr4.version
+4
+</pre> <p>Obtaining the network from an interface:</p> <pre data-language="python">&gt;&gt;&gt; host4 = ipaddress.ip_interface('192.0.2.1/24')
+&gt;&gt;&gt; host4.network
+IPv4Network('192.0.2.0/24')
+&gt;&gt;&gt; host6 = ipaddress.ip_interface('2001:db8::1/96')
+&gt;&gt;&gt; host6.network
+IPv6Network('2001:db8::/96')
+</pre> <p>Finding out how many individual addresses are in a network:</p> <pre data-language="python">&gt;&gt;&gt; net4 = ipaddress.ip_network('192.0.2.0/24')
+&gt;&gt;&gt; net4.num_addresses
+256
+&gt;&gt;&gt; net6 = ipaddress.ip_network('2001:db8::0/96')
+&gt;&gt;&gt; net6.num_addresses
+4294967296
+</pre> <p>Iterating through the “usable” addresses on a network:</p> <pre data-language="python">&gt;&gt;&gt; net4 = ipaddress.ip_network('192.0.2.0/24')
+&gt;&gt;&gt; for x in net4.hosts():
+... print(x)
+192.0.2.1
+192.0.2.2
+192.0.2.3
+192.0.2.4
+...
+192.0.2.252
+192.0.2.253
+192.0.2.254
+</pre> <p>Obtaining the netmask (i.e. set bits corresponding to the network prefix) or the hostmask (any bits that are not part of the netmask):</p> <pre data-language="python">&gt;&gt;&gt; net4 = ipaddress.ip_network('192.0.2.0/24')
+&gt;&gt;&gt; net4.netmask
+IPv4Address('255.255.255.0')
+&gt;&gt;&gt; net4.hostmask
+IPv4Address('0.0.0.255')
+&gt;&gt;&gt; net6 = ipaddress.ip_network('2001:db8::0/96')
+&gt;&gt;&gt; net6.netmask
+IPv6Address('ffff:ffff:ffff:ffff:ffff:ffff::')
+&gt;&gt;&gt; net6.hostmask
+IPv6Address('::ffff:ffff')
+</pre> <p>Exploding or compressing the address:</p> <pre data-language="python">&gt;&gt;&gt; addr6.exploded
+'2001:0db8:0000:0000:0000:0000:0000:0001'
+&gt;&gt;&gt; addr6.compressed
+'2001:db8::1'
+&gt;&gt;&gt; net6.exploded
+'2001:0db8:0000:0000:0000:0000:0000:0000/96'
+&gt;&gt;&gt; net6.compressed
+'2001:db8::/96'
+</pre> <p>While IPv4 doesn’t support explosion or compression, the associated objects still provide the relevant properties so that version neutral code can easily ensure the most concise or most verbose form is used for IPv6 addresses while still correctly handling IPv4 addresses.</p> </section> <section id="networks-as-lists-of-addresses"> <h2>Networks as lists of Addresses</h2> <p>It’s sometimes useful to treat networks as lists. This means it is possible to index them like this:</p> <pre data-language="python">&gt;&gt;&gt; net4[1]
+IPv4Address('192.0.2.1')
+&gt;&gt;&gt; net4[-1]
+IPv4Address('192.0.2.255')
+&gt;&gt;&gt; net6[1]
+IPv6Address('2001:db8::1')
+&gt;&gt;&gt; net6[-1]
+IPv6Address('2001:db8::ffff:ffff')
+</pre> <p>It also means that network objects lend themselves to using the list membership test syntax like this:</p> <pre data-language="python">if address in network:
+ # do something
+</pre> <p>Containment testing is done efficiently based on the network prefix:</p> <pre data-language="python">&gt;&gt;&gt; addr4 = ipaddress.ip_address('192.0.2.1')
+&gt;&gt;&gt; addr4 in ipaddress.ip_network('192.0.2.0/24')
+True
+&gt;&gt;&gt; addr4 in ipaddress.ip_network('192.0.3.0/24')
+False
+</pre> </section> <section id="comparisons"> <h2>Comparisons</h2> <p><a class="reference internal" href="../library/ipaddress#module-ipaddress" title="ipaddress: IPv4/IPv6 manipulation library."><code>ipaddress</code></a> provides some simple, hopefully intuitive ways to compare objects, where it makes sense:</p> <pre data-language="python">&gt;&gt;&gt; ipaddress.ip_address('192.0.2.1') &lt; ipaddress.ip_address('192.0.2.2')
+True
+</pre> <p>A <a class="reference internal" href="../library/exceptions#TypeError" title="TypeError"><code>TypeError</code></a> exception is raised if you try to compare objects of different versions or different types.</p> </section> <section id="using-ip-addresses-with-other-modules"> <h2>Using IP Addresses with other modules</h2> <p>Other modules that use IP addresses (such as <a class="reference internal" href="../library/socket#module-socket" title="socket: Low-level networking interface."><code>socket</code></a>) usually won’t accept objects from this module directly. Instead, they must be coerced to an integer or string that the other module will accept:</p> <pre data-language="python">&gt;&gt;&gt; addr4 = ipaddress.ip_address('192.0.2.1')
+&gt;&gt;&gt; str(addr4)
+'192.0.2.1'
+&gt;&gt;&gt; int(addr4)
+3221225985
+</pre> </section> <section id="getting-more-detail-when-instance-creation-fails"> <h2>Getting more detail when instance creation fails</h2> <p>When creating address/network/interface objects using the version-agnostic factory functions, any errors will be reported as <a class="reference internal" href="../library/exceptions#ValueError" title="ValueError"><code>ValueError</code></a> with a generic error message that simply says the passed in value was not recognized as an object of that type. The lack of a specific error is because it’s necessary to know whether the value is <em>supposed</em> to be IPv4 or IPv6 in order to provide more detail on why it has been rejected.</p> <p>To support use cases where it is useful to have access to this additional detail, the individual class constructors actually raise the <a class="reference internal" href="../library/exceptions#ValueError" title="ValueError"><code>ValueError</code></a> subclasses <a class="reference internal" href="../library/ipaddress#ipaddress.AddressValueError" title="ipaddress.AddressValueError"><code>ipaddress.AddressValueError</code></a> and <a class="reference internal" href="../library/ipaddress#ipaddress.NetmaskValueError" title="ipaddress.NetmaskValueError"><code>ipaddress.NetmaskValueError</code></a> to indicate exactly which part of the definition failed to parse correctly.</p> <p>The error messages are significantly more detailed when using the class constructors directly. For example:</p> <pre data-language="python">&gt;&gt;&gt; ipaddress.ip_address("192.168.0.256")
+Traceback (most recent call last):
+ ...
+ValueError: '192.168.0.256' does not appear to be an IPv4 or IPv6 address
+&gt;&gt;&gt; ipaddress.IPv4Address("192.168.0.256")
+Traceback (most recent call last):
+ ...
+ipaddress.AddressValueError: Octet 256 (&gt; 255) not permitted in '192.168.0.256'
+
+&gt;&gt;&gt; ipaddress.ip_network("192.168.0.1/64")
+Traceback (most recent call last):
+ ...
+ValueError: '192.168.0.1/64' does not appear to be an IPv4 or IPv6 network
+&gt;&gt;&gt; ipaddress.IPv4Network("192.168.0.1/64")
+Traceback (most recent call last):
+ ...
+ipaddress.NetmaskValueError: '64' is not a valid netmask
+</pre> <p>However, both of the module specific exceptions have <a class="reference internal" href="../library/exceptions#ValueError" title="ValueError"><code>ValueError</code></a> as their parent class, so if you’re not concerned with the particular type of error, you can still write code like the following:</p> <pre data-language="python">try:
+ network = ipaddress.IPv4Network(address)
+except ValueError:
+ print('address/netmask is invalid for IPv4:', address)
+</pre> </section> <div class="_attribution">
+ <p class="_attribution-p">
+ &copy; 2001&ndash;2023 Python Software Foundation<br>Licensed under the PSF License.<br>
+ <a href="https://docs.python.org/3.12/howto/ipaddress.html" class="_attribution-link">https://docs.python.org/3.12/howto/ipaddress.html</a>
+ </p>
+</div>