如何检查它是否是有效的网络掩码



我的代码必须返回有效的网络掩码。它必须检查二进制表示是True还是False

在我的代码中有一些错误。当我检查1时,它说第一个是真的,第二个是假的。。。所有1必须返回True,所有0必须返回False

def is_valid_netmask(numberlist):
ips = numberlist
result = True
#for ip in ips:
#num = int(ip)
#if not (num >= 0 and num <= 255):
#result = False
if len(ips) != 4:
return False
if result == False:
print("not valid")
else:
print("valid")
octet = ips
octet_bin = [format(int(i), '08b') for i in octet]
binary_netmask = ("").join(octet_bin)
print(binary_netmask)
checking_ones = True
for symbol in binary_netmask:
print("the current symbol is ", symbol)
print(f"I only encountered one so far: {checking_ones}")
if checking_ones and symbol == "0":
print("so i know that the netmask is not valid")
return False
elif symbol == "1":
checking_ones = False
print("I'm done so I know the netmask is valid")
return True

输出

valid
11111111111111111111111100000000
the current symbol is  1
I only encountered one so far: True  #correct
the current symbol is  1
I only encountered one so far: False #not correct
the current symbol is  1
I only encountered one so far: False #not correct
the current symbol is  1
I only encountered one so far: False #not correct
the current symbol is  1
I only encountered one so far: False #not correct
the current symbol is  1
I only encountered one so far: False #not correct
the current symbol is  1
I only encountered one so far: False #not correct
the current symbol is  1
I only encountered one so far: False #not correct
the current symbol is  1
I only encountered one so far: False #not correct
the current symbol is  1
I only encountered one so far: False #not correct
the current symbol is  1
I only encountered one so far: False #not correct
the current symbol is  1
I only encountered one so far: False #not correct
the current symbol is  1
I only encountered one so far: False #not correct
the current symbol is  1
I only encountered one so far: False #not correct
the current symbol is  1
I only encountered one so far: False #not correct
the current symbol is  1
I only encountered one so far: False #not correct
the current symbol is  1
I only encountered one so far: False #not correct
the current symbol is  1
I only encountered one so far: False #not correct
the current symbol is  1
I only encountered one so far: False #not correct
the current symbol is  1
I only encountered one so far: False #not correct
the current symbol is  1
I only encountered one so far: False #not correct
the current symbol is  1
I only encountered one so far: False #not correct
the current symbol is  1
I only encountered one so far: False #not correct
the current symbol is  1
I only encountered one so far: False #notcorrect
the current symbol is  0
I only encountered one so far: False #correct
the current symbol is  0
I only encountered one so far: False #correct 
the current symbol is  0
I only encountered one so far: False #correct
the current symbol is  0
I only encountered one so far: False #correct
the current symbol is  0
I only encountered one so far: False #correct
the current symbol is  0
I only encountered one so far: False #correct
the current symbol is  0
I only encountered one so far: False #correct
the current symbol is  0
I only encountered one so far: False #correct
I'm done so I know the netmask is valid
True

[edit]很抱歉,我对原来的答案感到困惑。

当我们开始检查比特时,我们需要第一个数字是1。然后我们可以接受尽可能多的。然而,一旦我们遇到0,我们就只能接受剩余比特的零。

accept_zero_only = False
first_bit = True
for symbol in binary_netmask:
print("the current symbol is ", symbol)
print("Did I encounter a 0 so far?" accept_zero_only)
if accept_zero_only and symbol == "1":
print("so i know that the netmask is not valid")
return False
elif symbol == "0":
print("Encountered a zero! From now on we are only accepting zeros."
accept_zero_only = True
if first_bit and symbol == "0":
print(" First bit should be a 1!")
return False
first_bit = False

[edit2]我在您的原始脚本中集成了上述内容,并进行了一些小的更改,并为以下输出运行了它。

def is_valid_netmask(numberlist):
ips = numberlist
if len(ips) != 4:
print "Input array should contain 4 numbers, was:" , len(ips)
return False
for ip in ips:
num = int(ip)
if not (num >= 0 and num <= 255):
print "Array contained number", ip, "which is not in [0,255] range."
return False
octet = ips
octet_bin = [format(int(i), '08b') for i in octet]
binary_netmask = ("").join(octet_bin)
print(binary_netmask)
accept_zero_only = False
first_bit = True
for symbol in binary_netmask:
if accept_zero_only and symbol == "1":
print("Netmask is not valid, contains alternating 1s and 0s and 1s again")
return False
elif symbol == "0":
accept_zero_only = True
if first_bit and symbol == "0":
print("First bit should be a 1, netmask is not valid")
return False
first_bit = False
print("Netmask is valid")
return True
print is_valid_netmask([255,255,0,0,0])    # False
print is_valid_netmask([256,255,255,255])  # False
print is_valid_netmask([255,0,0,0])        # True
print is_valid_netmask([192,0,0,0])        # True
print is_valid_netmask([0,255,255,255])    # False
print is_valid_netmask([255,0,255,0])      # False

我可能漏掉了一个边缘案例。如果上面的程序没有给你想要的输出,你能在下面的评论中给我们有问题的输入吗?

最新更新