Overview of the Python "ipaddress" module


In this article, we'll take a look at the ipaddress module available in Python 3.3 and above. This tutorial is intended to provide a brief reference for network engineers who want to know how to parse and use IP addresses in Python.

In this overview, you will learn:

  • What is the difference between IPv4 and IPv6 addresses

  • How to handle IPv4 addresses using Python's ipaddress module

  • How to handle IPv6 addresses using Python's ipaddress module

IPv4 vs IPv6 Addresses - Getting Started

In broad terms, IPv4 addresses and IPv6 addresses serve the same purpose and function. However, due to the large differences in the address structure of each protocol. This tutorial uses different sections to discuss IPv4 and IPv6 respectively.

In today's Internet, the IPv4 protocol undertakes most of the IP processing tasks, and will continue to do so in the near future. Although the scale and functional enhancements brought by IPv6 are indispensable to the future Internet and are being gradually applied, the adoption rate is still very low so far.

An IPv4 address consists of 32 bits divided into four "octets". The term "octet" is used to identify an octet structure in place of the more common term "byte", but they have the same definition. The four octets are called octet1, octet2, octet3 and octet4. This is a "dotted decimal" format, where each octet corresponds to a decimal value from 0 to 255.

Example of IPv4 address : 192.168.100.10

Example of IPv4 address (CIDR notation): 192.168.100.10/24

"/24" is CIDR notation and means that the first 24 bits of the 32 bits are used to identify the network portion of the address. Remember that each octet is 8 bits long, which means that the first three bytes (3 x 8 = 24) identify the network (192.168.100.x) and the remaining eight bits of the address identify the node (xxx10).

CIDR notation can be anything from /8 bits to /30 bits, with occasional /32 bits (/31 is invalid), but /24 is usually used. For example, your home network, or your school or company network is likely to be denoted by /24 CIDR.

An early term for network identification was subnet mask, where CIDR was represented as a single dotted decimal number. For example, a /24 CIDR is equivalent to a netmask of 255.255.255.0.

IPv6 addresses are 128 bits long, a significant increase compared to the 32 bits in IPv4 addresses. There are many differences between IPv4 and IPv6, but the biggest difference is the addressing structure. The extra length provides an exponential increase in the number of supported networks and hosts.

Example of an IPv6 address : 2001:db8:abcd:100::1/64

Where IPv4 addresses use dotted decimal format, the IPv6 protocol uses hexadecimal notation. Each position in an IPv6 address represents 4 bits, with values ​​from 0 to f, organized as follows:

  • The 128 bits are divided into 8 groups of 16 bits, and each group is separated by a colon. A group is called a "quad group" or "hexadecimal group" of 4 hex characters (4 hex characters times 4 bits = 16 bits). In the example above, the first quartet is "2001".

  • Header 0s in any quartet are stripped/compressed. In the example above, the second quartet is "db8", which is actually "0db8"", and the leading 0 is removed. The last quartet is "1", which is actually "0001"", and the third header 0 is compressed.

  • If a quartet contains all zeros, it will be compressed to a single zero. For example: a quartet with ":0000:" will be compressed as ":0:".

  • If an address contains a quartet of consecutive all zeros, the consecutive zeros are packed and represented by double colons. In the example above, the double colons represent 3 quartets of all zeros, or ":0000:0000:0000:" condensed to "::". Since the example address has 5 valued quartets, the number of quartets after compression must be 3 (the total minus 8).

All IPv6 address structures use CIDR notation to determine how many leading bits are used for network identification and the rest for host/interface identification. Considering it's 128 bits, there are many combinations produced.

Python's ipaddress module and IPv4 addresses

The ipaddress module is designed in CIDR notation and is recommended for its simplicity and ease of use. The ipaddress module also contains methods for restoring the netmask if necessary.

The original definition of an IPv4 address contained a "class" defined by the address range in the first octet. The ipaddress module does not recognize the IPv4 class, so it will not be covered in this tutorial.

The ipaddress module contains three specific IPv4 address object types:

  • a "host", or a separate address object that does not contain CIDR notation

  • A single interface address object containing CIDR notation

  • And a network address object that refers to the IP address range of the entire network.

The main difference between "host" and "interface" is that a host or ip_address object does not contain CIDR notation, whereas an ip_interface object contains CIDR notation:

  • The ip_address object is most useful when dealing with IP packets that do not require or use CIDR notation.

  • The ip_interface object is most useful when using node and interface IDs to connect to IP networks that must contain network/subnet IDs.

  • The ip_network object contains all addresses in the network and is useful for network identification.

Create an IPv4 host address object with ipaddress

The ipaddress.ip_address() factory function is used to create ip_address objects. It automatically determines whether to create an IPv4 or IPv6 address based on the value passed in (IPv6 addresses are discussed later in this tutorial). As mentioned above, this object represents the IP address that a packet finds while traversing a network that does not require CIDR.

In most cases, the value used to create an ip_address object will be a string in IPv4 dotted decimal format, as shown:

640?wx_fmt=png

Alternatively, IPv4 addresses can be entered in binary form, as a decimal value of the full 32-bit binary value, or in hexadecimal form, as in this example:

640?wx_fmt=png

The first example uses the full 32-bit address, the second is the decimal value of the 32-bit address. Both are clunky, error-prone and of little value. The third example uses hexadecimal values, which can be useful because most packets in parsing or sniffing are represented in hexadecimal format.

Create an IPv4 interface address object with ipaddress

The ipaddress.ip_interface() factory function is used to create an ip_interface object, which automatically determines whether to create an IPv4 or IPv6 address based on the value passed in (IPv6 addresses are discussed later in this tutorial).

As mentioned earlier, the ip_interface object represents the IP address found on the host or network interface where the CIDR (or mask) required to process the packet correctly is located.

640?wx_fmt=png

The same options as the ip_address option (binary, decimal value, hex) can be used when creating the ip_interface option. However, the only way to efficiently create an ip_interface via CIDR notation or masking is to use a dotted-decimal IPv4 address string.

Create an IPv4 network address object with ipaddress

The ipaddress.ip_network() factory function is used to create an ip_network object that automatically determines whether to create an IPv4 or IPv6 address based on the value passed in (IPv6 addresses are discussed later in this tutorial).

IP Network Definition: A contiguous range of IP addresses that includes a network or subnet. E.g:

  • 192.168.100.0/24 is the 192.168.100.0 network, where /24 specifies that the first 3 octets make up the network identity.

  • The fourth octet is used for assignment to individual host and router interfaces.

  • The address range is 192.168.100.1 to .254.

  • 192.168.100.0 is used to define the network/subnet and 192.168.100.255 is the broadcast address of that network. None of them can be used to assign to host or router interfaces.

Creating an ip_network object follows the same syntax as creating an ip_interface object:

640?wx_fmt=png

In the above example, the network address used must be a valid network address, which is the first address in the range of IPv4 addresses that make up the network. Otherwise, Python will throw an exception:

640?wx_fmt=png

When using a host or router interface, it is often necessary to determine the network address. Can be calculated, but requires several steps, which can be done in one step using the strict=False option (strict=True is the default).

640?wx_fmt=png

In the above example, the ip_interface address is known (192.168.100.10), but not the ip_network to which the interface belongs. With the strict=False option, the ip_network address (192.168.100.0/24) is calculated and populated into the ip_network object.

Python's ipaddress module and IPv6 addresses

Like IPv4, the ipaddress module uses the same three basic factory functions as IPv4. include:

  1. a "host", or a standalone address object that does not contain CIDR notation,

  2. Interface address object containing CIDR notation

  3. And a network address object that refers to a range of IP addresses for the entire network.

Since the details have been introduced in the IPv4 section, only a brief description is given here.

Create an IPv6 host address object with ipaddress

The ipaddress.ip_address() factory function is used to create ip_address objects. It automatically determines to use the IPv6 address format based on the value passed in. Note that CIDR notation is not used with the ip_address function.

In most cases, the value used to create an ip_address object for IPv6 will be a string in IPv6 quaternary/hexadecimal format according to this example:

640?wx_fmt=png

As with IPv4, IPv6 address objects can be created with full binary, decimal or hexadecimal values. For IPv4 addresses, 32 bits are intractable, and for 128-bit IPv6 addresses it's even more awkward. In practice, it is expected that the string represented by the eight-quadrant group will be in the general form.

Create an IPv6 interface address object with ipaddress

The ipaddress.ip_interface() factory function is used to create an ip_interface object, which automatically creates an IPv6 address based on the value passed in. Note that CIDR notation must be included in the function.

640?wx_fmt=png

Create an IPv6 network address object with ipaddress

The ipaddress.ip_network() factory function is used to create an ip_network object based on the passed in value for IPv6.

Like IPv4, an IPv6 network is defined as a series of consecutive IP addresses that can be assigned to a particular host or router interface.

Using our previous example 2001:db8:abcd:100::/64, the /64 CIDR specifies that four quartets make up the complete network identity. Remember that the first three quadruplets are the global IDs assigned by the IPS, and the fourth quadruple identifies the internal subnet number. A 64-bit balance is used for host IDs in the range from "0000 : 0000 : 0000 : 0001" to "ffff : ffff : ffff : fffe".

As with IPv4 addressing, the first and last addresses in an IPv6 subnet cannot be used for host addressing. Given a /64 CIDR, this means that there are 2 to the 64th power of 2 (minus 2) possible host addresses, which means mathematically there are 18,446,744,073,709,551,614 possible host addresses per network/subnet.

640?wx_fmt=png

The above global address assignments are as follows:

  • The global identifier is assigned by the ISP: 2001:db8 : abcd :: / 48

  • Subnet ID: 2001 : db8 : abcd : 100 :: / 64

  • First available address in subnet: 2001 : db8 : abcd : 100 : : 1/64

  • Last available address in subnet: 2001 : db8 : abcd : 100 : ffff : ffff : ffff : fffeffff / 64

more resources

Here are some additional resources to learn more about the ipaddress module in Python:

Extended PDF version of this article with additional information https://dbader.org/static/img/ipaddress-module-introduction.pdf 

ipaddress module documentation https://docs.python.org/3/library/ipaddress.html 

Introduction to the ipaddress module https://docs.python.org/3/howto/ipaddress.html 

Wikipedia - IPv4 https://en.wikipedia.org/wiki/IPv4 

Wikipedia - IPv6 https://en.wikipedia.org/wiki/IPv6


∞∞∞



640?wx_fmt=jpeg&wx_lazy=1

IT School - {Technology Youth Circle} continues to pay attention to the fields of Internet, blockchain and artificial intelligence 640?wx_fmt=jpeg&wx_lazy=1



The official account replied "Python" ,

Invite you to join {IT send Python technology group} 


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325774781&siteId=291194637