IP Address is a numerical address assigned to the computer when it connects to the internet to uniquely identify it. It's referred to as the host address. IP Address has two versions (IPv4 and IPv6). IPv4 is the first version that uses 32 bits numbers to represent an IP address. Due to the increasing demand of people connecting to the internet, 32-bit IPv4 address was not enough to cope up with demand, hence IPv6 was introduced. IPv6 addresses are 128 bits long.
IPv4 Representation:
IPV4 is generally represented as four numbers ranging between 0-255 separated by dots (Ex: "49.34.86.241"). A total of 32 bits are grouped into four groups each representing 8 bits. As each group has 8 bits to represent, it can hold numbers between 0-255(2^8-1).
IPv6 Representation:
IPv6 is generally represented as numbers separated by colon (Ex: "0f0f:0241:0f0f:0f0f:0f0f:0f0f:0f0f:0101"). The address has 8 groups each consisting of 16 bits. Each group has generally 4 numbers each of 4-bits hence can represent a hexadecimal number between 0-f(2^4-1). If we are not given all 4 digits for the group then we'll remaining digits will be added as "0". As the IPv6 address is quite long, it's common practice to avoid groups where digits are not present. Instead, the only double colon will be used to represent that all the groups following a particular group will have a value of "0". Ex: "0f0f:0241::" is equivalent to "0f0f:0241:0000:0000:0000:0000:0000:0000" and "::0f0f:0241" is equivalent to "0000:0000:0000:0000:0000:0000:0f0f:0241".
The network address is represented as an address followed by a number of digits of the network which is the same for all addresses present in the network. The network address and a number of bits are separated by a slash. Ex: "49.34.86.0/24". Here network consists of all addresses in the range "49.34.86.0" - "49.34.86.255". The same logic goes for the IPv6 addresses as well. We just give the number of bits that are fixed for all addresses of the network.
Network Interfaces are generally used by network administrators to identify individual computers on the network. They have the same structure as that of network address but has actual address present instead of the top-level network address. Ex- "49.34.86.212/24"
As a part of this tutorial, we'll be explaining how we can maintain IP addresses, network addresses, and network interfaces using ipaddress module of Python. We'll explain how we can maintain addresses, perform arithmetic operations with them, explain properties of addresses, list addresses in a network address, etc. We'll explain all of this with simple and easy-to-understand examples.
Please make a NOTE that it'll be helpful to easily go through this tutorial and grasp the API of the ipaddress module soon if the reader has a background of IPv4 and IPv6 address structure, network addresses, and interface addresses. We recommend that readers go through at least the intro section of these two Wikipedia articles.
Our first example, explains how we can create IPV4 and IPV6 addresses using ipaddress module. The ipaddress module provides us method named ip_address() and classes named IPv4Address() & IPv6Address() that can be used to create IP address in Python.
ip_address(address) - It takes as input a string or an integer and return instance of IPv4Address or IPv6Address. It finds out the version of the IP address based on the string representation. If an integer is given then an integer less than 2^32 will be considered IPv4 and numbers greater than it will be considered IPv6. It also checks for the validity of addresses and will raise an error in case it does not satisfy all requirements.
IPv4Address(address) - It accepts a string or an integer as input and returns an instance of IPv4Address which holds an IP address.
IPv6Address(address) - It accepts a string or an integer as input and returns an instance of IPv6Address which holds an IP address.
Our code for this part explains various ways to create IPv4 and IPv6 addresses. It also explains ways which can fail to create an address hinting at invalid addresses. We have created IP addresses from both string and integer values both. We can easily convert IP address instances to strings or integers by wrapping them into str or int constructor.
import ipaddress
### IPV4
print("==================== IPV4 =========================")
print("IPV4 Address Length : {} bits\n".format(ipaddress.IPV4LENGTH))
host_address = ipaddress.ip_address("49.34.86.241")
print("Host Address Type : {}, Value : {}\n".format(type(host_address).__name__, host_address))
host_address = ipaddress.ip_address(2**32-1)
print("Host Address Type : {}, Value : {}\n".format(type(host_address).__name__, host_address))
host_address = ipaddress.IPv4Address("49.34.86.241")
print("Host Address Type : {}, Value : {}\n".format(type(host_address).__name__, host_address))
try:
host_address = ipaddress.ip_address("49.34.86.256")
except Exception as e:
print("ErrorType : {}, Error : {}\n".format(type(e).__name__, e))
host_address = ipaddress.IPv4Address("49.34.86.241")
print("Host Address Type : {}, Int Value : {}\n".format(type(host_address).__name__, int(host_address)))
import ipaddress
### IPV6
print("==================== IPV6 =========================")
print("IPV6 Address Length : {} bits\n".format(ipaddress.IPV6LENGTH))
host_address = ipaddress.ip_address("0f0f:0241::")
print("Host Address Type : {}, Value : {}\n".format(type(host_address).__name__, host_address))
host_address = ipaddress.ip_address(2**32)
print("Host Address Type : {}, Value : {}\n".format(type(host_address).__name__, host_address))
host_address = ipaddress.IPv6Address("0f0f:0241::")
print("Host Address Type : {}, Value : {}\n".format(type(host_address).__name__, host_address))
host_address = ipaddress.IPv6Address("::0f0f:0241")
print("Host Address Type : {}, Value : {}\n".format(type(host_address).__name__, host_address))
try:
host_address = ipaddress.ip_address("0f0g:0::")
except Exception as e:
print("ErrorType : {}, Error : {}\n".format(type(e).__name__, e))
try:
host_address = ipaddress.ip_address(2**128)
except Exception as e:
print("ErrorType : {}, Error : {}".format(type(e).__name__, e))
host_address = ipaddress.IPv6Address("0f0f:0241::")
print("Host Address Type : {}, Int Value : {}\n".format(type(host_address).__name__, int(host_address)))
As a part of our second example, we'll demonstrate how we can maintain network addresses using ipaddress module. The ipaddress module provides us with method named ip_network() and classes named IPv4Network() & IPv6Network() for creating network addresses in Python.
Our code for this part explains in a different way how we can create IPv4 and IPv6 network addresses using the methods specified above. It also highlights cases where the methods will fail if invalid network addresses are provided. We have also explained the usage of strict parameters with examples. We have tried to cover different scenarios to explain how we can create network addresses using ipaddress. We have also explained how we can get the IP addresses of the network.
Please make a NOTE that instances of IPv4Network and IPv6Network can be treated as an iterator. We can just use it inside of a loop and it'll loop through all possible IP addresses inside of the network. We can also wrap it inside the list constructor and it'll return a list of all IP addresses present in that network.
import ipaddress
#### IPV4
print("==================== IPV4 =========================")
network = ipaddress.ip_network("49.34.86.0/24")
print("Network Address Type : {}, Value : {}".format(type(network).__name__, network))
print("Number of Addresses on Network : {}\n".format(len(list(network))))
network = ipaddress.ip_network("49.34.86.0")
print("Network Address Type : {}, Value : {}".format(type(network).__name__, network))
print("Number of Addresses on Network : {}\n".format(len(list(network))))
network = ipaddress.ip_network("49.34.86.0/28")
print("Network Address Type : {}, Value : {}".format(type(network).__name__, network))
print("Number of Addresses on Network : {}\n".format(len(list(network))))
network = ipaddress.ip_network(2**32 - 256)
print("Network Address Type : {}, Value : {}".format(type(network).__name__, network))
print("Number of Addresses on Network : {}\n".format(len(list(network))))
network = ipaddress.IPv4Network("49.34.86.0/28")
print("Network Address Type : {}, Value : {}".format(type(network).__name__, network))
print("Number of Addresses on Network : {}\n".format(len(list(network))))
try:
network = ipaddress.ip_network("49.34.86.0/16")
print("Network Address Type : {}, Value : {}\n".format(type(network).__name__, network))
except Exception as e:
print("ErrorType : {}, Error : {}\n".format(type(e).__name__, e))
network = ipaddress.ip_network("49.34.86.0/16", strict=False)
print("Network Address Type : {}, Value : {}".format(type(network).__name__, network))
print("Number of Addresses on Network : {}\n".format(len(list(network))))
print("\nFirst Few Addresses : {}".format(list(network)[:5]))
import ipaddress
#### IPV6
print("==================== IPV6 =========================")
network = ipaddress.ip_network("0f0f:0f0f:0f0f:0f0f:0f0f:0f0f:0f0f:0/112")
print("Network Address Type : {}, Value : {}".format(type(network).__name__, network))
print("Number of Addresses on Network : {}\n".format(len(list(network))))
network = ipaddress.ip_network("0f0f:0f0f:0f0f:0f0f:0f0f:0f0f:0f0f:0")
print("Network Address Type : {}, Value : {}".format(type(network).__name__, network))
print("Number of Addresses on Network : {}\n".format(len(list(network))))
network = ipaddress.ip_network("0f0f:0f0f:0f0f:0f0f:0f0f:0f0f:0f0f:0/120")
print("Network Address Type : {}, Value : {}".format(type(network).__name__, network))
print("Number of Addresses on Network : {}\n".format(len(list(network))))
try:
network = ipaddress.ip_network("0f0f:0f0f:0f0f:0f0f:0f0f:0f0f:0f0f:0f0f/112")
print("Network Address Type : {}, Value : {}\n".format(type(network).__name__, network))
except Exception as e:
print("ErrorType : {}, Error : {}\n".format(type(e).__name__, e))
network = ipaddress.ip_network("0f0f:0f0f:0f0f:0f0f:0f0f:0f0f:0f0f:0f0f/112", strict=False)
print("Network Address Type : {}, Value : {}".format(type(network).__name__, network))
print("Number of Addresses on Network : {}\n".format(len(list(network))))
network = ipaddress.ip_network(2**128 - 2**16)
print("Network Address Type : {}, Value : {}".format(type(network).__name__, network))
print("Number of Addresses on Network : {}\n".format(len(list(network))))
network = ipaddress.IPv6Network("0f0f:0f0f:0f0f:0f0f:0f0f:0f0f:0f0f:0f0f/112", strict=False)
print("Network Address Type : {}, Value : {}".format(type(network).__name__, network))
print("Number of Addresses on Network : {}\n".format(len(list(network))))
print("\nFirst Few Addresses : {}".format(list(network)[:5]))
As a part of our third example, we'll explain how we can create interfaces using ipaddress module. The ipaddress module provides us with method named ip_interface() and classes named IPv4Interface & IPv6Interface that can be used to create interfaces. As we had discussed earlier, interfaces as used by network engineers and others as shorthand for referring individual hosts on a particular network.
ip_interface() - It takes as input a string or an integer and return instance of IPv4Interface or IPv6Interface. It finds out the version of the interface based on the string representation. If an integer is given then an integer less than 2^32 will be considered IPv4 and numbers greater than it will be considered IPv6. It also checks for the validity of addresses and will raise an error in case it does not satisfy all requirements.
IPv4Interface(address) - It accepts a string or an integer as input and returns an instance of IPv4Interface which holds the IPv4 interface.
IPv6Interface(address) - It accepts a string or an integer as input and returns an instance of IPv6Interface which holds the IPv6 interface.
Our code for this example explains various ways to create interfaces. It tries to explain possible scenarios of creating IPv4 and IPv6 interfaces. We have explained how we can create interfaces from string and integer both.
import ipaddress
### IPV4
print("==================== IPV4 =========================")
interface = ipaddress.ip_interface("49.34.86.241/24")
print("Interface Type : {}, Value : {}\n".format(type(interface).__name__, interface))
host_address = ipaddress.ip_interface(2**32-2*8)
print("Host Address Type : {}, Value : {}\n".format(type(interface).__name__, interface))
host_address = ipaddress.ip_interface("49.34.86.241/24")
print("Host Address Type : {}, Value : {}\n".format(type(interface).__name__, interface))
host_address = ipaddress.IPv4Interface("49.34.86.241/24")
print("Host Address Type : {}, Value : {}\n".format(type(interface).__name__, interface))
import ipaddress
### IPV6
print("==================== IPV6 =========================")
interface = ipaddress.ip_interface("0f0f:0241::1/112")
print("Host Address Type : {}, Value : {}\n".format(type(interface).__name__, interface))
host_address = ipaddress.ip_interface("0f0f:0241:0f0f:0f0f:0f0f:0f0f:0f0f:0f0f/112")
print("Host Address Type : {}, Value : {}\n".format(type(interface).__name__, interface))
host_address = ipaddress.ip_interface(2**128 - 2**16)
print("Host Address Type : {}, Value : {}\n".format(type(interface).__name__, interface))
host_address = ipaddress.IPv6Interface("0f0f:0241:0f0f:0f0f:0f0f:0f0f:0f0f:0f0f/112")
print("Host Address Type : {}, Value : {}\n".format(type(interface).__name__, interface))
As a part of our fourth example, we'll demonstrate different attributes that are present in the instance of IPv4Address and IPv6Address.
Our code for this example simply creates instances of type IPv4Address and IPv6Address. It then prints all properties mentioned above for each address type to show us the value of different attributes.
import ipaddress
host_address = ipaddress.ip_address("49.34.86.241")
print("Host Address Type : {}, Value : {}\n".format(type(host_address).__name__, host_address))
print("IP Address Version : {}".format(host_address.version))
print("IP Address Number of Bits for Representaion : {}".format(host_address.max_prefixlen))
print("IP Address Compressed : {}".format(host_address.compressed))
print("IP Address Exploded : {}".format(host_address.exploded))
print("IP Address Binary Representation : {}".format(host_address.packed))
print("IP Address Reverse DNS PTR Record : {}".format(host_address.reverse_pointer))
print()
print("Is IP Address Multicast? : {}".format(host_address.is_multicast))
print("Is IP Address Private? : {}".format(host_address.is_private))
print("Is IP Address Global? : {}".format(host_address.is_global))
print("Is IP Address Unspecified? : {}".format(host_address.is_unspecified))
print("Is IP Address Reserved? : {}".format(host_address.is_reserved))
print("Is IP Address Loopback? : {}".format(host_address.is_loopback))
print("Is IP Address Link-Local? : {}".format(host_address.is_link_local))
import ipaddress
host_address = ipaddress.ip_address("0f0f:0241::")
print("Host Address Type : {}, Value : {}\n".format(type(host_address).__name__, host_address))
print("IP Address Version : {}".format(host_address.version))
print("IP Address Number of Bits for Representaion : {}".format(host_address.max_prefixlen))
print("IP Address Compressed : {}".format(host_address.compressed))
print("IP Address Exploded : {}".format(host_address.exploded))
print("IP Address Binary Representation : {}".format(host_address.packed))
print("IP Address Reverse DNS PTR Record : {}".format(host_address.reverse_pointer))
print("IP Address IPV4 Mapping : {}".format(host_address.ipv4_mapped))
#print("IP Address Scope ID : {}".format(host_address.scope_id))
#print("IP Address 6to4 Value : {}".format(host_address.sixtofour))
#print("IP Address Teredo Value : {}".format(host_address.teredo))
print()
print("Is IP Address Multicast? : {}".format(host_address.is_multicast))
print("Is IP Address Private? : {}".format(host_address.is_private))
print("Is IP Address Global? : {}".format(host_address.is_global))
print("Is IP Address Unspecified? : {}".format(host_address.is_unspecified))
print("Is IP Address Reserved? : {}".format(host_address.is_reserved))
print("Is IP Address Loopback? : {}".format(host_address.is_loopback))
print("Is IP Address Link-Local? : {}".format(host_address.is_link_local))
print("Is IP Address Site-Local? : {}".format(host_address.is_site_local))
As a part of our fifth example, we'll explain how we can compare different IP addresses and perform arithmetic operations on them.
Our code for this part starts by creating instances of IPv4Address and IPv6Address. It then compares them for different comparison types like equal to, greater than, less than, etc. It then creates new addresses by adding and subtracting integers from numbers to create new addresses. It then again performs comparison operations on these newly-created addresses.
Please make a NOTE that we can easily add and subtract an integer from an instance of IP address to create a new address which we have explained in the code of this example.
import ipaddress
host_address1 = ipaddress.ip_address("49.34.86.241")
print("Host Address 1 Type : {}, Value : {}\n".format(type(host_address1).__name__, host_address1))
host_address2 = ipaddress.ip_address("49.34.86.245")
print("Host Address 2 Type : {}, Value : {}\n".format(type(host_address2).__name__, host_address2))
print("Is Address 1 equal to Address 2? : {}".format(host_address1 == host_address2))
print("Is Address 1 not equal to Address 2? : {}".format(host_address1 != host_address2))
print("Is Address 1 greater than Address 2? : {}".format(host_address1 > host_address2))
print("Is Address 1 greater than or equal to Address 2? : {}".format(host_address1 >= host_address2))
print("Is Address 1 less than Address 2? : {}".format(host_address1 < host_address2))
print("Is Address 1 less than or equal to Address 2? : {}".format(host_address1 <= host_address2))
host_address3 = host_address1 + 4
print("Host Address 3 Type : {}, Value : {}".format(type(host_address3).__name__, host_address3))
print("\nIs Address 3 equal to Address 2? : {}".format(host_address3 == host_address2))
print("Is Address 3 not equal to Address 2? : {}".format(host_address3 != host_address2))
print("Is Address 3 greater than Address 2? : {}".format(host_address3 > host_address2))
print("Is Address 3 greater than or equal to Address 2? : {}".format(host_address3 >= host_address2))
print("Is Address 3 less than Address 2? : {}".format(host_address3 < host_address2))
print("Is Address 3 less than or equal to Address 2? : {}".format(host_address3 <= host_address2))
host_address4 = host_address2 - 4
print("Host Address 4 Type : {}, Value : {}".format(type(host_address3).__name__, host_address4))
print("\nIs Address 4 equal to Address 1? : {}".format(host_address4 == host_address1))
print("Is Address 4 not equal to Address 1? : {}".format(host_address4 != host_address1))
print("Is Address 4 greater than Address 1? : {}".format(host_address4 > host_address1))
print("Is Address 4 greater than or equal to Address 1? : {}".format(host_address4 >= host_address1))
print("Is Address 4 less than Address 1? : {}".format(host_address4 < host_address1))
print("Is Address 4 less than or equal to Address 1? : {}".format(host_address4 <= host_address1))
import ipaddress
host_address1 = ipaddress.ip_address("0f0f:0241::")
print("Host Address Type : {}, Value : {}\n".format(type(host_address1).__name__, host_address1.exploded))
host_address2 = ipaddress.ip_address("0f0f:0241:0:0:0:0:0:0005")
print("Host Address Type : {}, Value : {}\n".format(type(host_address2).__name__, host_address2.exploded))
print("Is Address 1 equal to Address 2? : {}".format(host_address1 == host_address2))
print("Is Address 1 not equal to Address 2? : {}".format(host_address1 != host_address2))
print("Is Address 1 greater than Address 2? : {}".format(host_address1 > host_address2))
print("Is Address 1 greater than or equal to Address 2? : {}".format(host_address1 >= host_address2))
print("Is Address 1 less than Address 2? : {}".format(host_address1 < host_address2))
print("Is Address 1 less than or equal to Address 2? : {}".format(host_address1 <= host_address2))
host_address3 = host_address1 + 5
print("Host Address 3 Type : {}, Value : {}".format(type(host_address3).__name__, host_address3.exploded))
print("\nIs Address 3 equal to Address 2? : {}".format(host_address3 == host_address2))
print("Is Address 3 not equal to Address 2? : {}".format(host_address3 != host_address2))
print("Is Address 3 greater than Address 2? : {}".format(host_address3 > host_address2))
print("Is Address 3 greater than or equal to Address 2? : {}".format(host_address3 >= host_address2))
print("Is Address 3 less than Address 2? : {}".format(host_address3 < host_address2))
print("Is Address 3 less than or equal to Address 2? : {}".format(host_address3 <= host_address2))
host_address4 = host_address2 - 5
print("Host Address 4 Type : {}, Value : {}".format(type(host_address3).__name__, host_address4.exploded))
print("\nIs Address 4 equal to Address 1? : {}".format(host_address4 == host_address1))
print("Is Address 4 not equal to Address 1? : {}".format(host_address4 != host_address1))
print("Is Address 4 greater than Address 1? : {}".format(host_address4 > host_address1))
print("Is Address 4 greater than or equal to Address 1? : {}".format(host_address4 >= host_address1))
print("Is Address 4 less than Address 1? : {}".format(host_address4 < host_address1))
print("Is Address 4 less than or equal to Address 1? : {}".format(host_address4 <= host_address1))
As a part of our sixth example, we'll explain attributes of network instances of type IPv4Network and IPv6Network.
Please make a NOTE that many attributes have the same meaning as that of IPv4Address and IPv6Address instances hence they won't be covered here again. We'll only highlight attributes specific to instances of the network.
Our code for this example simply creates and instances of IPv4Network and IPv6Network first. It then prints all the important attributes of both instances.
import ipaddress
network = ipaddress.ip_network("49.34.86.0/24")
print("Network Address Type : {}, Value : {}".format(type(network).__name__, network))
print("Number of Addresses on Network : {}\n".format(network.num_addresses))
print("Network Version : {}".format(network.version))
print("Network Number of Bits for Representaion : {}".format(network.max_prefixlen))
print("Network Compressed : {}".format(network.compressed))
print("Network Address : {}".format(network.network_address))
print("Network Broadcast Address : {}".format(network.broadcast_address))
print("Network Host Mask : {}".format(network.hostmask))
print("Network Mask : {}".format(network.netmask))
print("Network With Host Mask : {}".format(network.with_hostmask))
print("Network With Mask : {}".format(network.with_netmask))
print()
print("Is Network Multicast? : {}".format(network.is_multicast))
print("Is Network Private? : {}".format(network.is_private))
print("Is Network Global? : {}".format(network.is_global))
print("Is Network Unspecified? : {}".format(network.is_unspecified))
print("Is Network Reserved? : {}".format(network.is_reserved))
print("Is Network Loopback? : {}".format(network.is_loopback))
print("Is Network Link-Local? : {}".format(network.is_link_local))
import ipaddress
network = ipaddress.ip_network("0f0f:0f0f:0f0f:0f0f:0f0f:0f0f:0f0f:0/112")
print("Network Address Type : {}, Value : {}".format(type(network).__name__, network))
print("Number of Addresses on Network : {}\n".format(network.num_addresses))
print("Network Version : {}".format(network.version))
print("Network Number of Bits for Representaion : {}".format(network.max_prefixlen))
print("Network Compressed : {}".format(network.compressed))
print("Network Address : {}".format(network.network_address))
print("Network Broadcast Address : {}".format(network.broadcast_address))
print("Network Host Mask : {}".format(network.hostmask))
print("Network Mask : {}".format(network.netmask))
print("Network With Host Mask : {}".format(network.with_hostmask))
print("Network With Mask : {}".format(network.with_netmask))
print()
print("Is Network Multicast? : {}".format(network.is_multicast))
print("Is Network Private? : {}".format(network.is_private))
print("Is Network Global? : {}".format(network.is_global))
print("Is Network Unspecified? : {}".format(network.is_unspecified))
print("Is Network Reserved? : {}".format(network.is_reserved))
print("Is Network Loopback? : {}".format(network.is_loopback))
print("Is Network Link-Local? : {}".format(network.is_link_local))
As a part of our seventh example, we are going to demonstrate useful methods of IPv4Network and IPv6Network which can help us find things like whether there are IP addresses overlap between networks, whether one network is subnet or supernet of another network, whether two networks are same or not, etc.
Our code for this example creates instances of networks and then compares them using all the methods mentioned above. It helps us understand how methods work.
import ipaddress
network1 = ipaddress.ip_network("49.34.86.0/24")
print("Network Address Type : {}, Value : {}".format(type(network1).__name__, network))
print("Number of Addresses on Network 1 : {}".format(network1.num_addresses))
print("\nFirst Few Addresses : {}".format(list(network1.hosts())[:5]))
network2 = ipaddress.ip_network("49.34.86.0/28")
print("\nNumber of Addresses on Network 2 : {}\n".format(network2.num_addresses))
print("\nDoes Network 1 and Network 2 Overlaps? : {}".format(network1.overlaps(network2)))
print("\nDoes Network 2 and Network 1 Overlaps? : {}".format(network2.overlaps(network1)))
excluded_addresses = network1.address_exclude(network2)
print("\nNetwork Definitions Not Present in Network 1 and Present in Network 2 : {}".format(list(excluded_addresses)))
print("\nNetwork Subnets : {}".format(list(network1.subnets())))
print("\nNetwork Supernets : {}".format(network1.supernet()))
print("\nIs Network 1 subnet of Network 2? : {}".format(network1.subnet_of(network2)))
print("Is Network 2 subnet of Network 1? : {}".format(network2.subnet_of(network1)))
print("\nIs Network 1 supernet of Network 2? : {}".format(network1.supernet_of(network2)))
print("Is Network 2 superbnet of Network 1? : {}".format(network2.supernet_of(network1)))
print("\nComparison of Network 1 & Network 2 : {}".format(network1.compare_networks(network2)))
print("Comparison of Network 2 & Network 1 : {}".format(network2.compare_networks(network1)))
print("Comparison of Network 1 & Network 1 : {}".format(network1.compare_networks(network1)))
print("Comparison of Network 2 & Network 2 : {}".format(network2.compare_networks(network2)))
import ipaddress
network1 = ipaddress.ip_network("0f0f:0f0f:0f0f:0f0f:0f0f:0f0f:0f0f:0/112")
print("Network Address Type : {}, Value : {}".format(type(network1).__name__, network1))
print("Number of Addresses on Network : {}".format(network1.num_addresses))
print("\nFirst Few Addresses : {}".format(list(network1.hosts())[:5]))
network2 = ipaddress.ip_network("0f0f:0f0f:0f0f:0f0f:0f0f:0f0f:0f0f:0/116")
print("\nNumber of Addresses on Network 2 : {}\n".format(network2.num_addresses))
print("\nDoes Network 1 and Network 2 Overlaps? : {}".format(network1.overlaps(network2)))
print("\nDoes Network 2 and Network 1 Overlaps? : {}".format(network2.overlaps(network1)))
excluded_addresses = network1.address_exclude(network2)
print("\nNetwork Definitions Not Present in Network 1 and Present in Network 2 : {}".format(list(excluded_addresses)))
print("\nNetwork Subnets : {}".format(list(network1.subnets())))
print("\nNetwork Supernets : {}".format(network1.supernet()))
print("\nIs Network 1 subnet of Network 2? : {}".format(network1.subnet_of(network2)))
print("Is Network 2 subnet of Network 1? : {}".format(network2.subnet_of(network1)))
print("\nIs Network 1 supernet of Network 2? : {}".format(network1.supernet_of(network2)))
print("Is Network 2 superbnet of Network 1? : {}".format(network2.supernet_of(network1)))
print("\nComparison of Network 1 & Network 2 : {}".format(network1.compare_networks(network2)))
print("Comparison of Network 2 & Network 1 : {}".format(network2.compare_networks(network1)))
print("Comparison of Network 1 & Network 1 : {}".format(network1.compare_networks(network1)))
print("Comparison of Network 2 & Network 2 : {}".format(network2.compare_networks(network2)))
As a part of our eighth example, we'll be demonstrating various attributes of instances of IPv4Interface and IPv6Interface. The IPv4Interface extends IPv4Address and IPv6Interface extends IPv6Address hence majority of properties as the same as one of the IP address instances that we have explained. We'll only cover properties that were not explained earlier in this part.
Our code for this example simply creates instances of IPv4Interface and IPv6Interface first. It then prints values of all the attributes of both instances to explain them.
import ipaddress
interface = ipaddress.ip_interface("49.34.86.241/24")
print("Interface Type : {}, Value : {}\n".format(type(interface).__name__, interface.exploded))
print("Interface Version : {}".format(interface.version))
print("Interface IP Address : {}".format(interface.ip))
print("Interface Number of Bits for Representaion : {}".format(interface.max_prefixlen))
print("Interface Compressed : {}".format(interface.compressed))
print("Interface Network : {}".format(interface.network))
print("Interface Host Mask : {}".format(interface.hostmask))
print("Interface Mask : {}".format(interface.netmask))
print("Interface With Host Mask : {}".format(interface.with_hostmask))
print("Interface With Mask : {}".format(interface.with_netmask))
print("Interface With Prefix Length : {}".format(interface.with_prefixlen))
print()
print("Is Interface Multicast? : {}".format(interface.is_multicast))
print("Is Interface Private? : {}".format(interface.is_private))
print("Is Interface Global? : {}".format(interface.is_global))
print("Is Interface Unspecified? : {}".format(interface.is_unspecified))
print("Is Interface Reserved? : {}".format(interface.is_reserved))
print("Is Interface Loopback? : {}".format(interface.is_loopback))
print("Is Interface Link-Local? : {}".format(interface.is_link_local))
import ipaddress
interface = ipaddress.ip_interface("0f0f:0241::1/112")
print("Host Address Type : {}, Value : {}\n".format(type(interface).__name__, interface.exploded))
print("Interface Version : {}".format(interface.version))
print("Interface IP Address : {}".format(interface.ip))
print("Interface Number of Bits for Representaion : {}".format(interface.max_prefixlen))
print("Interface Compressed : {}".format(interface.compressed))
print("Interface Network : {}".format(interface.network))
print("Interface Host Mask : {}".format(interface.hostmask))
print("Interface Mask : {}".format(interface.netmask))
print("Interface With Host Mask : {}".format(interface.with_hostmask))
print("Interface With Mask : {}".format(interface.with_netmask))
print("Interface With Prefix Length : {}".format(interface.with_prefixlen))
print()
print("Is Interface Multicast? : {}".format(interface.is_multicast))
print("Is Interface Private? : {}".format(interface.is_private))
print("Is Interface Global? : {}".format(interface.is_global))
print("Is Interface Unspecified? : {}".format(interface.is_unspecified))
print("Is Interface Reserved? : {}".format(interface.is_reserved))
print("Is Interface Loopback? : {}".format(interface.is_loopback))
print("Is Interface Link-Local? : {}".format(interface.is_link_local))
This ends our small tutorial explaining the usage of the API of ipaddress module. It covers how we can work with IP address, the network address, and network interfaces in Python with simple examples. Please feel free to let us know your views in the comments section.
If you are more comfortable learning through video tutorials then we would recommend that you subscribe to our YouTube channel.
When going through coding examples, it's quite common to have doubts and errors.
If you have doubts about some code examples or are stuck somewhere when trying our code, send us an email at coderzcolumn07@gmail.com. We'll help you or point you in the direction where you can find a solution to your problem.
You can even send us a mail if you are trying something new and need guidance regarding coding. We'll try to respond as soon as possible.
If you want to