Navigation

This document aims to provide a gentle introduction to the
ipaddress 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 ipaddress
represents IP network addressing concepts.

For readers that aren’t particularly familiar with IP addressing, it’s
important to know that the Internet Protocol 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.

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.

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 ipaddress.ip_address() factory function, which automatically
determines whether to create an IPv4 or IPv6 address based on the passed in
value:

Host addresses are usually grouped together into IP networks, so
ipaddress 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.

As for addresses, a factory function is provided that determines the correct
IP version automatically:

Network objects cannot have any host bits set. The practical effect of this
is that 192.0.2.1/24 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.

By default, attempting to create a network object with host bits set will
result in ValueError being raised. To request that the
additional bits instead be coerced to zero, the flag strict=False can
be passed to the constructor:

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:

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 192.0.2.1/24 is commonly used by network engineers and the
people who write tools for firewalls and routers as shorthand for “the host
192.0.2.1 on the network 192.0.2.0/24”, Accordingly, ipaddress
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.

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.

When creating address/network/interface objects using the version-agnostic
factory functions, any errors will be reported as ValueError 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 supposed to be IPv4
or IPv6 in order to provide more detail on why it has been rejected.