本文整理汇总了Python中ipaddress.collapse_addresses方法的典型用法代码示例。如果您正苦于以下问题:Python ipaddress.collapse_addresses方法的具体用法?Python ipaddress.collapse_addresses怎么用?Python ipaddress.collapse_addresses使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ipaddress
的用法示例。
在下文中一共展示了ipaddress.collapse_addresses方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: covering_cidr
# 需要导入模块: import ipaddress [as 别名]
# 或者: from ipaddress import collapse_addresses [as 别名]
def covering_cidr(ips: List[str]) -> str:
"""
Given list of IPs, return CIDR that covers them all.
Presumes it's at least a /24.
"""
def collapse(ns):
return list(ipaddress.collapse_addresses(ns))
assert len(ips) > 0
networks = collapse([
ipaddress.IPv4Interface(ip + "/24").network for ip in ips
])
# Increase network size until it combines everything:
while len(networks) > 1:
networks = collapse([networks[0].supernet()] + networks[1:])
return networks[0].with_prefixlen
# Script to dump resolved IPs to stdout as JSON list:
示例2: spanning_network
# 需要导入模块: import ipaddress [as 别名]
# 或者: from ipaddress import collapse_addresses [as 别名]
def spanning_network(networks):
if not networks:
raise ValueError("List of networks is empty")
if len(networks) == 1:
return networks[0]
sorter = operator.attrgetter("num_addresses")
while True:
networks = sorted(
ipaddress.collapse_addresses(networks), key=sorter, reverse=True)
if len(networks) == 1:
return networks[0]
networks[-1] = networks[-1].supernet()