使用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