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

前端 未结 6 2394
礼貌的吻别
礼貌的吻别 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:28

    For python 2 & 3 use:

    from ipaddress import ip_network, ip_address
    
    def in_cidr(ip, cidr):
      return ip_address(ip) in ip_network(cidr)
    

    Demo


    For pyhton 2.7 install using:

    pip install ipaddress
    

提交回复
热议问题