← 返回题库
中级

实现屏幕IP白名单校验

未完成
中级参考 代码结构已给出,请填写 ____ 处
def solve():
    import ipaddress
    SUBNETS = ['____','____','____']
    def ip_ok(____):
        try:
            ip_obj = ipaddress.ip_address(____)
            for s in SUBNETS:
                if ip_obj in ipaddress.ip_network(____): return True, s
            return False, None
        except: return False, '____'
    tests = [(____),(____),(____),(____)]
    print(____)
    for ip, expected in tests:
        ok, subnet = ip_ok(____)
        result = '____' if ok==expected else '____'
        print("____"+ip+"____"+str(____)+"____"+str(____)+"____"+result+"____")

示例

输入
solve()
期望输出
IP whitelist check:
  10.1.5.22: ok=True subnet=10.0.0.0/8 [PASS]
  192.168.1.100: ok=True subnet=192.168.0.0/16 [PASS]
  8.8.8.8: ok=False subnet=None [PASS]
  172.16.0.1: ok=True subnet=172.16.0.0/12 [PASS]
Python 代码 🔒 登录后使用
🔒

登录后即可练习

注册免费账号,在浏览器中直接运行 Python 代码