Python program to find if two IP Address belongs to Same or Different Network
Prerequisites: Classless Inter Domain Routing (CIDR)
Given two IP Addresses in CIDR Notation determine whether they belong to Same Network or Different Network. Two IP addresses are said to be in Same Network if the Network ID of both the IP Addresses are same.
Input : IP1 = 192.168.1.0/8, IP2 = 220.127.116.11/8
Output : Same Network
Explanation : The number of bits in Network ID of both the IP Addresses is 8 bits and the first
8 bits (i.e 1st octet) of both the IP Addresses is same (i.e 192). Hence both IP Addresses
belong to Same Network.
Input : IP1 = 18.104.22.168/20, IP2 = 22.214.171.124/20
Output : Different Network
Explanation : The number of bits in Network ID of both the IP Addresses is 20 bits but the first
20 bits of both the IP Addresses are different. Hence both IP Addresses belong to Different
To implement it, we will use ip_network and network_address method of ipaddress module of Python 3.3 .
Same Network Different Network
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