【问题标题】:How to check if it is a valid netmask?如何检查它是否是有效的网络掩码?
【发布时间】:2020-12-14 09:02:35
【问题描述】:

我的代码必须返回一个有效的网络掩码。它必须检查二进制表示是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

【问题讨论】:

  • 如果符号为“1”,则将checking_ones 设置为False。问题可能来自那里。

标签: python ip netmask


【解决方案1】:

[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

我可能错过了一个极端情况。如果上面的程序没有给你想要的输出,能不能把下面cmets中有问题的输入给我们?

【讨论】:

  • 我使用了您的代码,但它不起作用。它仍然没有给出正确的结果
  • 哦!我的错。我想我修好了。
  • 你能检查一下 edit2 程序吗?如果它仍然没有给你你想要的输出,你可以给一个程序失败的输入吗?
猜你喜欢
  • 1970-01-01
  • 2010-12-31
  • 2012-04-22
  • 2016-12-27
  • 2010-10-09
  • 2022-01-02
  • 1970-01-01
  • 2015-02-24
  • 1970-01-01
相关资源
最近更新 更多