ip_set = netaddr.IPSet()
ip_set.add(netaddr.IPRange(ip_pool.first_ip, ip_pool.last_ip)) //生成开始到结束的所有IP
av_set = ip_set.difference(ip_allocations) //将已经分配的IP给去掉,可得到可分配的IP
if av_set.size == 0:
continue
if prefer_next:
window = 1
else:
# Compute a value for the selection window
window = min(av_set.size, 1000)
ip_index = random.randint(1, window) \\随机数
candidate_ips = list(itertools.islice(av_set, ip_index)) \\将IP池分片
allocated_ip = candidate_ips[
random.randint(0, len(candidate_ips) - 1)] \\随机取IP
netaddr用来管理IP分配
猜你喜欢
转载自blog.csdn.net/llxx1234/article/details/104963721
今日推荐
周排行