check if an IP is within a range of CIDR in Python

前端 未结 6 2320
礼貌的吻别
礼貌的吻别 2021-01-05 15:55

I know there are some similar questions up here, but they mostly either want to find the range itself (which uses some libraries, like the example that stackoverflow says is

6条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-05 16:38

    In Python 3.3 and later, you should be using the ipaddress module.

    from ipaddress import ip_network, ip_address
    
    net = ip_network("1.1.0.0/16")
    print(ip_address("1.1.2.2") in net)    # True
    

提交回复
热议问题