How to Manipulate IP Addresses in Python using ipaddress Module?
Last Updated :
21 Sep, 2021
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.
IPv4Address
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.
Example:
Python3
import ipaddress
ip = ipaddress.IPv4Address( '112.79.234.30' )
print ( "Total no of bits in the ip:" , ip.max_prefixlen)
print ( "Is multicast:" , ip.is_multicast)
print ( "Is private:" , ip.is_private)
print ( "Is global:" , ip.is_global)
print ( "Is unspecified:" , ip.is_unspecified)
print ( "Is reversed:" , ip.is_reserved)
print ( "Is loopback:" , ip.is_loopback)
print ( "Is link-local:" , ip.is_link_local)
ip1 = ip + 1
print ( "Next ip:" , ip1)
ip2 = ip - 1
print ( "Previous ip:" , ip2)
print ( "Is ip1 is greater than ip2:" , ip1 > ip2)
|
Output:
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: 112.79.234.31
Previous ip: 112.79.234.29
Is ip1 is greater than ip2: True
IPv4Network
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.
Example:
Python3
import ipaddress
network = ipaddress.IPv4Network( "192.168.1.0/24" )
print ( "Network address of the network:" , network.network_address)
print ( "Broadcast address:" , network.broadcast_address)
print ( "Network mask:" , network.netmask)
print ( "with netmask:" , network.with_netmask)
print ( "with_hostmask:" , network.with_hostmask)
print ( "Length of network prefix in bits:" , network.prefixlen)
print ( "Total number of hosts under the network:" , network.num_addresses)
print ( "Overlaps 192.168.0.0/16:" , network.overlaps(ipaddress.IPv4Network( "192.168.0.0/16" )))
print ( "Supernet:" , network.supernet(prefixlen_diff = 1 ))
print ( "The network is subnet of 192.168.0.0/16:" ,
network.subnet_of(ipaddress.IPv4Network( "192.168.0.0/16" )))
print ( "The network is supernet of 192.168.0.0/16:" ,
network.supernet_of(ipaddress.IPv4Network( "192.168.0.0/16" )))
print ( "Compare the network with 192.168.0.0/16:" ,
network.compare_networks(ipaddress.IPv4Network( "192.168.0.0/16" )))
|
Output:
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
Share your thoughts in the comments
Please Login to comment...