How to Manipulate IP Addresses in Python using ipaddress Module?
IP Address stands for internet protocol address. It’s an identifying number that’s related to a selected computer or network. When connected to the web, the IP address allows the computers to send and receive information. Python provides ipaddress module which provides the capabilities to create, manipulate and operate on IPv4 and IPv6 addresses and networks. This module comes inbuilt with python 3.3+, so you don’t need to install it if you have python 3.3+. Although you can install it using pip.
pip install ipaddress
The module has IPv4Address class and IPv6Address class to handle IPv4 and IPv6 formats respectively. Since IPv4Address and IPv6Address objects share a lot of common attributes we will see for IPv4 format only and similarly we can do for IPv6 format.
The IPv4Address objects have a lot of attributes for IPv4 address. ipaddress.IPv4Address(‘address’) construct an IPv4Address object representing IPv4 address ‘address’. Some attributes of the class are as follows:
- max_prefixlen: Return the total number of bits in the IP address represented by IPv4Address object (32 for IPv4 and 128 for IPv6).
- is_multicast: Return True if the address is reserved for multicast use.
- is_private: Return True if the address is allocated for private networks.
- is_global: Return True if the address is allocated for public networks.
- is_unspecified: Return True if the address is unspecified.
- is_reserved: Return True if the address is otherwise IETF reserved.
- is_loopback: Return True if this is a loopback address.
- is_link_local: Return True if the address is reserved for link-local usage.
We can also use comparison operators to compare address objects. Also, we can add or subtract integers from the address object.
Now let’s see an example of these attributes.
Total no of bits in the ip: 32 Is multicast: False Is private: False Is global: True Is unspecified: False Is reversed: False Is loopback: False Is link-local: False Next ip: 184.108.40.206 Previous ip: 220.127.116.11 Is ip1 is greater than ip2: True
IPv4Network objects are used to inspect and define IP networks. All the attributes for address object are also valid for network object, additionally, network object provides additional attributes. Some of them is listed below.
- network_address: Return the network address for the network.
- broadcast_address: Return the broadcast address for the network. Packets sent to the broadcast address should be received by every host on the network.
- netmask: Return network mask of the network.
- with_netmask: Return a string representation of the network, with the mask in netmask notation.
- with_hostmask: Return a string representation of the network, with the mask in host mask notation.
- prefixlen: Return the length of the network prefix in bits.
- num_addresses: Return the total number of the address of this network.
- hosts(): Returns an iterator over the usable hosts in the network. The usable hosts are all the IP addresses that belong to the network, except the network address itself and the network broadcast address.
- overlaps(other): Return True if this network is partly or wholly contained in other or other is wholly contained in this network.
- subnets(prefixlen_diff): Return the subnets that join to make the current network definition, depending on the argument values. The prefixlen_diff parameter is the integer that indicates the amount our prefix length should be increased by.
- supernet(prefixlen_diff): Return the supernet containing this network definition, the prefixlen_diff is the amount our prefix length should be decreased by.
- subnet_of(other): Return True if this network is a subnet of other (new in python 3.7).
- supernet_of(other): Return True if this network is a supernet of other (new in python 3.7).
- compare_networks(other): Compare ip network with the other IP network. In this comparison only the network addresses are considered, host bits aren’t. It returns either -1, 0, or 1.
Now let’s see an example of the above methods.
Network address of the network: 192.168.1.0 Broadcast address: 192.168.1.255 Network mask: 255.255.255.0 with netmask: 192.168.1.0/255.255.255.0 with_hostmask: 192.168.1.0/0.0.0.255 Length of network prefix in bits: 24 Total number of hosts under the network: 256 Overlaps 192.168.0.0/16: True Supernet: 192.168.0.0/23 The network is subnet of 192.168.0.0/16: True The network is supernet of 192.168.0.0/16: False Compare the network with 192.168.0.0/16: 1
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course