美文网首页
Python解析IP地址CIDR的形式

Python解析IP地址CIDR的形式

作者: zhfish | 来源:发表于2018-01-25 21:23 被阅读0次

使用IPy库

 from IPy import IP
 ip = IP('127.0.0.0/30')
 for x in ip:
    print(x)      
127.0.0.0
127.0.0.1
127.0.0.2
127.0.0.3

使用netaddr库

 from netaddr import *
 ip = IPNetwork('192.0.2.16/29')
 ip_list = list(ip)
 print(ip_list)
[IPAddress('192.0.2.16'), IPAddress('192.0.2.17'), IPAddress('192.0.2.18'), IPAddress('192.0.2.19'), IPAddress('192.0.2.20'), IPAddress('192.0.2.21'), IPAddress('192.0.2.22'), IPAddress('192.0.2.23')]
 from netaddr import *
 startip = '208.130.29.30'
 endip = '208.130.29.35'
 cidrs = netaddr.iprange_to_cidrs(startip, endip)
 for k, v in enumerate(cidrs):
     iplist = v
     print iplist
208.130.29.30/31
208.130.29.32/30

相关文章

网友评论

      本文标题:Python解析IP地址CIDR的形式

      本文链接:https://www.haomeiwen.com/subject/scebaxtx.html