好吧所以我需要输入两个输入的IP地址,如128.233.17.12,然后是给定的子网掩码,如果它们在同一网络上,则能够输出。
我设法将它们变成二进制格式并正确格式化,但我现在不知道如何比较它们。为了确定它们是否在同一网络上,我需要进行垂直添加。
IE
#128.233.17.12 = 10000000111010010001000|100001100
#128.233.12.17 = 10000000111010010000110|000010001
#255.255.0.0 = 11111111111111110000000000000000
只有当子网(底部)为1时,前两个ip才需要相同。因此,这些IP在同一网络上。到目前为止这是我的代码,但我不确定如何输入所有变量。
IPAdress1 = input("Please enter a dotted quad notation IP address: ")
IPAdress2 = input("Please enter another: ")
SubnetMask = input("Please enter the subnet mask: ")
binaryIP1 = [bin(int(IPAdress1))[2:].rjust(8,'0') for IPAdress1 in IPAdress1.split('.')]
IP1 = ''.join(binaryIP1)
binaryIP2 = [bin(int(IPAdress2))[2:].rjust(8,'0') for IPAdress2 in IPAdress2.split('.')]
IP2 = ''.join(binaryIP2)
binarysub = [bin(int(SubnetMask))[2:].rjust(8,'0') for SubnetMask in SubnetMask.split('.')]
sub = ''.join(binarysub)
i=0
j=0
for (i,j) in zip(binaryIP1,binaryIP2):
if i == j:
print("Same Network" )
else:
print("Different networks")
任何帮助表示赞赏
答案 0 :(得分:2)
我建议使用netaddr模块来播放地址:
from netaddr import IPNetwork
if IPNetwork("128.233.17.12/255.255.0.0") == IPNetwork("128.233.12.17/255.255.0.0"):
print "Same!"
else:
print "Different!"