Question: Python function same _ subnetconverts IP addresses and subnet mask to binary strings in order to determine if two IPv 4 addresses are in the

Python function same_subnetconverts IP addresses and subnet mask to binary strings in order to determine if two IPv4 addresses are in the same subnet, returning a string indicating whether both IP addresses are in the same subnet or not.
Complete the Python function same_subnet. The function should accept three strings representing two IP addresses and a subnet mask, determine if both IP addresses are in the same subnet, and return a predefi ned string statement indicating if both IP addresses are in the same subnet. def same_subnet(ip1, ip2, subnet_mask):
# convert IP addresses to binary strings
ip1_bin =''.join([format(int(x),'08b') for x in ip1.split('.')])
ip2_bin =''.join([format(int(x),'08b') for x in ip2.split('.')])
# convert subnet mask to binary string
subnet_bin =''.join([format(int(x),'08b') for x in subnet_mask.split('.')])
# get network address portion for both IP addresses
subnet_len = subnet_bin.count('1')
network1_bin = ip1_bin[:subnet_len]
network2_bin = ip2_bin[:subnet_len]
# Predefined statement indicating if IP addresses are in the same subnet
x = f"{ip1} and {ip2} are in the same subnet"
y = f"{ip1} and {ip2} are not in the same subnet"
# Write a conditional expression to check if both IP addresses are in the same subnet
# and return the appropriate string output x or y.
# Write your code here.
# You may alter the code below to test your solution or print help documentation.
# Only the same_subnet function will be graded for this assessment.
# print(same_subnet('192.168.1.100','192.168.1.200','255.255.255.0'))

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!