Python IPaddress Module

Python IPaddress Module

We all have heard about the term ‘IP Address’ and how each device is connected with this term. In the term ‘IP Address’, IP stands for Internet Protocol, which refers to locating a device present on the internet. Internet Protocols are the protocols or set of rules that govern a proper data format defined by the authorities and sent through the local network or internet. We all have our residence address from where someone can find us or locate us, and exactly in the same way, our devices also have their address. Our device’s virtual address, either on the local network or on the internet, is known as the IP address of our device. One can use this device’s IP address to locate where the device is present or from where this device is connected to the internet. The IP address is very helpful as it helps reduce cyber & other crimes and also helps in locating a missing device. We can use this IP address to find out where the device is present when the device is using the internet or a local network, and that’s how we can also locate the person who is using that device. An IP address is also helpful in sending information and data between two or more devices connected to the internet or a local network. The IP address is a set of four numbers that are different for different devices like smartphones, Wi-Fi routers, websites, etc. Knowing the IP address makes it very easy for us to identify whether the protocols are sent from a smartphone, website, or Wi-Fi router.

We can use many methods to manipulate a given IP address. We can even manipulate, modify, or change the IP address of a system or website. Also, IP addresses are of different types, and therefore, different methods are required to manipulate or modify different types of IP addresses. Many programming languages provide us with the packages and libraries we can use to manipulate a given IP address. These libraries come with many functions that can work with different types of IP addresses. If we talk specifically about Python, it also has modules and packages that can work with different types of IP addresses. The ipaddress module of Python is one such package of Python that comes with many built-in functions. We can use these built-in functions of this module to manipulate a given IP address. Therefore, we will learn about the ipaddress module of Python in this tutorial and learn how we can manipulate a given IP address using this module.

Introduction to Ipaddress Module of Python

As we have already talked about the IP address, we would have an idea by now how important the IP address is. Every device present on the internet has an IP address, and no device can communicate or work on the internet without an IP address. The different types of IP addresses can be differentiated using the different number of bits present in the given IP address, and that’s how we can find out that given IP address is from which type of device. The ipaddress module of Python is particularly designed to work with different types of IP addresses and have lots of functions. This module can be used to create, manipulate, modify an IP address by using functions of this module in a Python program.

This module’s functions are broadly categorized into the following two categories:

  1. IPv4Address class functions
  2. IPv6Address class functions

The IPv4Address and IPv6Address class functions are different types of functions of the ipaddress module, which are used to handle IP addresses of IPv4 and IPv6 format, respectively. Both IPv6 and IPv4 class functions share multiple common functionalities and work similarly. Therefore, we will only talk about the IPv4 format class and its functions to manipulate an IP address in this tutorial. If we understand about working IPv4 class format functions of this module, it will become very simple and easy to understand the functions of the second category. We will learn about this IPv4Address and the functions of this category in the implementation part of this tutorial. But before proceeding to the implementation part of this module, we have to go through the installation process of the ipaddress module.

Ipaddress Module of Python: Installation

The ipaddress module is very simple to use, and we can easily manipulate a given IP address using functions of this module, but before we start manipulating or working with IP addresses with functions of this module, we have first to make sure that the ipaddress module is present in our system. The ipaddress module is not an in-built module of Python (until the latest versions of Python) which means that this module requires an external installation process to be installed in our system. Therefore, if the ipaddress module is not already installed in our system, we have to install it by performing the given installation process of this module. Although many methods can be used to install this module, the pip installer method is the simplest and easiest installation method for this module. Therefore, in this installation section of this module, we will use the pip installation method for installing this ipaddress module in our device. While installing this module through the command prompt shell, we will have an option to change the default directory (where the module will be installed), but it is advisable to install it in the default directory. To install this module with the pip installation method, we will first open the command prompt shell present in our device and write the following given command in it:

After writing the installation command in the terminal shell as given above, we can start the installation process of this module simply by pressing the ‘enter’ key. Once the installation process starts, we will have to wait until all the dependencies for this module are downloaded completely.

Python IPaddress Module

As we can see, the ‘ipaddress module is successfully installed’ message is now displayed on the installation screen of the command prompt shell. This means that the ipaddress module is successfully installed in our system, and it will be verified when we run a Python program in which we use the functions of this module.

Note: The ipaddress module of Python is now coming in-built with the latest versions of Python, and therefore, if any of us have the latest version of Python, we don’t need to perform this installation process.

As the ipaddress module is now present in our system, we can start working with this module and understand the implementation of this module.

Ipaddress Module of Python: Implementation

In this part, we will learn how we can use the functions of this module in Python programs to manipulate a given IP address. We will use the IPv4 class format functions in the example programs and understand their implementation. But first, we will learn about the IPv4Address class format of the IP address and the ipaddress module functions that come under this format.

IPv4Address of ipaddress module

The IPv4Address class format of this module has formats for the IP address of IPv4 type and different functions that we can use to manipulate the given IP address. The IPv4Address category functions of this module work on the IPv4 format and share common functionalities. We can use functions under this category by creating an object for the IPv4Address format.

We can use the following syntax in a given Python program to create or construct a function object for using functions of this category in the program:

As we can see, the syntax given above takes only one argument in the IPv4Address object construction. The only argument we have to give while creating the IPv4 format object is an IP address that can be given in the data or given by the user.

There are many IPv4 class format functions in this format, but here we will talk about only some important of them, which come under the IPv4Address category of the ipaddress module.

Following are some of the most important IPv4Address category functions with their detailed explanation:

(i) max_prefixlen (): This function of the ipaddress module returns the total number of bits present in the given IP address in the output. We can use this function of the ipaddress module in a Python program to return a total number of bits of an IP address. The given IP address in the program, represented by the IPv4Address class object, will return the number of bits (32 bits for IPv4 format and 128 bits for IPv6 format).

(ii) is_link_local (): This function returns only true and false in the output, and it is used to check if the given IP address is reserved for link-local usage. The is_link_local() function of the ipaddress module will return true in the output only when the given IP address in the program is reserved for link-local usage; otherwise, it will return false.

(iii) is_reserved (): The is_reserved() function of the IPv4 class format also returns only true and false in the output. We can use this function of the ipaddress module to find out that the given IP address is IETF reserved. If the given IP address is IETF reserved, this function will return true in the output; otherwise, it will return false.

This module helps us recognize if the given IP address is public or not by returning true and false in the output.

(iv) is_global (): We can use the is_global() function of the ipaddress module to differentiate between a public or private address and find out if the given IP address is private or public. If the given IP address in the program is allocated for public networks, this function will return true in the output; otherwise, this function will return false.

(v) is_multicast (): We can use this function to find out if the IP address in the program is used or reserved for multicast use (It means that given IP address is used from multiple devices). The is_multicast() function of the ipaddress module returns true in the output when the given IP address in the program is reserved for multicast use, and otherwise, it will return false in all other cases.

(vi) is_loopback (): This function returns only true and false in the output, and it is used to check if the given IP address is a loopback type IP address or not. We can use the is_loopback() function of the ipaddress module in a Python program to find out if the given IP address is a loopback address by checking the program’s output. If the program’s output is True after execution of this function, it means that the given IP address in the program is a loopback address otherwise, it means that the given address is not a loopback address.

(vii) is_unspecified (): Any IP address given in the program is either of two types (specified and unspecified). Now, if we want to check if the IP address we have given in the program is specified or unspecified, we can use is_unspecified() of the ipaddress module. The is_unspecified() function of the ipaddress returns true when the given IP address in the program is unspecified and returns false in the output when the IP address is specified.

(viii) is_private (): The is_private() function of the ipaddress works similar to the is_global() function. We can also use this function to check if the given IP address in the program is a public or private IP address. This function will return true when the given IP address in the program is private; otherwise, it will return false in the output.

These are some important IPv4Address class functions of the ipaddress module, which we can use in a Python program to find out the details from the given IP address. We can use the explanation of these functions to find out a particular detail from the given IP address in the program. Other than this, we can also use comparison operators in an example program to compare the address objects of the IP address given in the program. Once an address object is created in the program, we can also perform addition or subtraction on it and add or subtract integers in the subtract object. We will now understand the IPv4Address category of the ipaddress module by using the functions of this class format in an example program. We will give an IP address in the example program to manipulate it using the functions about whom we have learned above. That’s how we will understand the functioning and implementation of these functions of the IPv4 class. Look at the following example program to understand the functioning of IPv4Address format functions of the ipaddress module:

Example 1: Look at the following Python program where we have manipulated a given IP address using the IPv4Address format functions:

Output:

Total no of bits present in the given IP address:  32
The given IP address is reserved for multicast use:  False
The given IP address allocated for private networks:  False
The given IP address allocated with global network:  True
If the given IP address is unspecified network address:  False
If the given IP address is otherwise IETF reserved:  False
If the given IP address is a loopback IP address:  False
If the given IP address is a Link-local IP address:  False
The Next IP address in the series of given IP address is:  112.69.240.31
The previous IP address in the series of given IP address is:  112.69.240.29
Is the ipFirst IP address defined by us is greater than the ipSecond IP address:  True

As we can see, with the given IP address we have printed many properties related to it. We have found out many things associated with the given IP address or not. We have also found out the next & previous IP address in the series of given IP address. We have also compared two defined IP addresses in the program and found out which one is greater. That’s how we can use the functions of IPv4Address class functions as well as IPv6Address class functions to work and manipulate the given IPv4 & IPv6 format IP address, respectively.

Explanation: We first imported the ipaddress module as ipa in the program to define the IPv4Address class object. After that, we defined the IPv4Address class object in the variable ipObj using the IPv4Address() function to use IPv4 format functions in the format. Inside the IPv4Address() function, we have used an IP address as the function’s argument so that we can manipulate the given IP address. After that, we have used the ipObj variable in which we defined the IPv4Address class object with the IPv4 format functions to find various properties associated with the given IP address. We found out in the program that if the given IP address is private or global, is it a Local Link address, is it specified or unspecified, and many other properties with the IPv4 format functions. After that, we have added and subtracted integers from the given IP address to find out the next & previous IP address in the line. Lastly, we compared two IP addresses that we have defined in the program and found out which one is greater. We have performed all the major functions on the given IPv4 format IP address with the IPv4Address class functions.

IPv4Network Class Format

The class object defined with the IPv4Network format is used to define and inspect IP networks of the IPv4 class format. All the functions and attributes that we have studied for the IPv4Address format are valid in the IPv4Network class format, but this class format also has some additional functions and attributes that are very helpful in inspecting a given IP address. Therefore, it becomes important for us to learn about all these attributes and functions of the IPv4Network class format to use them in the example program to inspect the given IP network. But before we learn about these IPv4Network class format functions, we should learn how to define the class object of the IPv4Network because we can use these attributes and functions only after defining the IPv4Network class object in the example program. Following is the syntax for defining the IPv4Network class object in a Python program:

As we can see, we have to provide the given IP network as an argument in the IPv4Network function to define the IPv4Network class object in the program. The IPv4Network function takes only this argument, and after that, we can start inspecting this given IP network by using functions and attributes of the IPv4Network class format.

Following is a brief description of some such attributes and functions that are present in the IPv4Network class format:

(i) compare_networks(other IP address): This function of the IPv4Network class format is used to compare two networks by the IP address of these networks. We can use this function to compare the given IP network with the other IP network that will be provided as an argument in the function. This function results in -1 (When given IP network is smaller than provided IP network), 0 (When both IP networks are equal), 1 (When given IP network is greater). We can use these values given in the output to compare the given IP network with the provided IP network.

(ii) subnet_of(other IP address): The subnet_of() is a function of the IPv4Network format, which we can use in the example program to inspect if the given IP network is a subnet of the second IP network. We have to provide this second IP network as an argument inside the subnet_of() function, and only after that will we know if the given IP network is the subnet of the provided IP network. This function returns the execution result in ‘true’ & ‘false’ form.

(iii) subnets(prefixlen_diff): We can use this function of the IPv4Network class format to find the subnet IP network of the given IP network by providing the prefix length difference for the given IP network. The prefix length difference we have to provide in the program should be in integer form like 1, 2, etc.

(iv) hosts(): The host() is a function in the IPv4Network class format which we can use in the example program to find out the number of usable hosts in the given IP network by providing the given IP address as an argument in this function.

(v) overlaps(Other IP address): The overlaps() function of the IPv4Network class format can be used to find out if the given IP network in the program is completely or partially overlapping the other IP network. We have to provide this other IP network as an argument inside the overlaps() function to find the results.

(vi) prefixlen: We can use the prefixlen attribute of the IPv4Network to print the length of the given IP network’s prefix in bits in the output.

(vii) netmask: We can use the netmask attribute of the IPv4Network format to find out the netmask of the given IP network as this attribute returns the netmask in the output when executed.

Other than this, there are also many other major attributes and functions present in the IPv4Network class format, but we will not discuss them in this part. We will understand the implementation of all the functions & attributes described above, as well as the other important functions of the IPv4Network by using all of them in an example program. We will give an IP network in the example program, and then we will use all major attributes & functions of the IPv4Network class format to inspect the given IP network.

Look at the following example program to understand the implementation of the functions and attributes of the IPv4Network format:

Example 2: Look at the following Python program where we have inspected a given IP network:

Output:

The actual network address of the given IP network in the program is:  192.168.1.0
The actual broadcast address of the given IP network in the program is:  192.168.1.0
The Network mask of the given IP network is:  255.255.255.255
The With Net mask value of the given IP network is:  192.168.1.0/255.255.255.255
The with host mask value of the given IP network is:  192.168.1.0/0.0.0.0
The prefix Length of the given IP network in bits format is:  32
The Total number of usable hosts that comes under the given IP network except the network itself are:  1
If the given IP network is overlapping the 192.168.0.0/16 IP address:  False
The Supernet of the given IP network is:  192.168.1.0/31
If the given IP network is a subnet network of 192.168.0.0/24 IP address:  False
If the given IP network is a supernet network of 192.168.0.0/24 IP address:  False
The result of the Comparison the given IP network with the provided 192.168.0.0/24 network address:  1

As we can see, we have inspected and found out properties of the given IP network in the program using the IPv4Network format functions and attributes in the example program.


原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/263734.html

(0)
上一篇 2022年5月30日
下一篇 2022年5月30日

相关推荐

发表回复

登录后才能评论