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

时间:2020-08-25 16:00:09

标签: python ip netmask

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

在我的代码中有问题。当我检查1时,它说第一个为true,第二个为false ...所有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

1 个答案:

答案 0 :(得分:0)

[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

我可能错过了一个极端情况。如果上面的程序没有提供您想要的输出,您可以在下面的注释中给我们有问题的输入吗?