How to Manipulate IP Addresses in Python using ipaddress Module? - GeeksforGeeks

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.
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# Creating an object of IPv4Address class and# initializing it with an IPv4 address.ip = ipaddress.IPv4Address('112.79.234.30') # Print total number of bits in the ip.print("Total no of bits in the ip:", ip.max_prefixlen) # Print True if the IP address is reserved for multicast use.print("Is multicast:", ip.is_multicast) # Print True if the IP address is allocated for private networks.print("Is private:", ip.is_private) # Print True if the IP address is global.print("Is global:", ip.is_global) # Print True if the IP address is unspecified.print("Is unspecified:", ip.is_unspecified) # Print True if the IP address is otherwise IETF reserved.print("Is reversed:", ip.is_reserved) # Print True if the IP address is a loopback address.print("Is loopback:", ip.is_loopback) # Print True if the IP address is Link-localprint("Is link-local:", ip.is_link_local) # next ip addressip1 = ip + 1print("Next ip:", ip1) # previous ip addressip2 = ip - 1print("Previous ip:", ip2) # Print True if ip1 is greater than ip2print("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 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 # Initializing an IPv4 Network.network = ipaddress.IPv4Network("192.168.1.0/24") # Print the network address of the network.print("Network address of the network:", network.network_address) # Print the broadcast addressprint("Broadcast address:", network.broadcast_address) # Print the network mask.print("Network mask:", network.netmask) # Print with_netmask.print("with netmask:", network.with_netmask) # Print with_hostmask.print("with_hostmask:", network.with_hostmask) # Print Length of network prefix in bits.print("Length of network prefix in bits:", network.prefixlen) # Print the number of hosts under the network.print("Total number of hosts under the network:", network.num_addresses) # Print if this network is under (or overlaps) 192.168.0.0/16print("Overlaps 192.168.0.0/16:", network.overlaps(ipaddress.IPv4Network("192.168.0.0/16"))) # Print the supernet of this networkprint("Supernet:", network.supernet(prefixlen_diff=1)) # Print if the network is subnet of 192.168.0.0/16.print("The network is subnet of 192.168.0.0/16:",      network.subnet_of(ipaddress.IPv4Network("192.168.0.0/16"))) # Print if the network is supernet of 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"))) # Compare the ip network with 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
 
simmytarika5
Picked
python-modules
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
How to Install PIP on Windows ?
How to drop one or multiple columns in Pandas Dataframe
How To Convert Python Dictionary To JSON?
Check if element exists in list in Python
Python | Pandas dataframe.groupby()
Defaultdict in Python
Python | Get unique values from a list
Python Classes and Objects
Python | os.path.join() method
Create a directory in Python