## 1 Creating Address/Network/Interface objects 

Since ipaddress is a module for inspecting and manipulating IP addresses, the first thing you’ll want to do is create some objects. You can use ipaddress to create objects from strings and integers. 

## 1.1 A Note on IP Versions 

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 intro- duction, 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. 

## 1.2 IP Host Addresses 

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: 

>>> ipaddress.ip_address('192.0.2.1 ') IPv4Address('192.0.2.1') >>> ipaddress.ip_address('2001:DB8::1 ') IPv6Address('2001:db8::1') 

Addresses can also be created directly from integers. Values that will fit within 32 bits are assumed to be IPv4 addresses: 

>>> ipaddress.ip_address(3221225985 ) IPv4Address('192.0.2.1') >>> ipaddress.ip_address( 42540766411282592856903984951653826561 ) IPv6Address('2001:db8::1') 

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: 

>>> ipaddress.ip_address(1) IPv4Address('0.0.0.1') >>> ipaddress.IPv4Address( 1) IPv4Address('0.0.0.1') >>> ipaddress.IPv6Address( 1) IPv6Address('::1') 

## 1.3 Defining Networks 

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: 

