Python 3.3+ipaddressfunction
ipaddress.summarize_address_range()
range operations
Turns a 'from address to address' range into the smallest list of networks that covers it exactly.
Syntax
ipaddress.summarize_address_range(first, last)
Both ends are inclusive, and the function takes address objects, not strings. A range rarely fits a single network: 10.0.0.0 to 10.0.0.10 yields three (/29, /31, /32). Like collapse_addresses(), it returns a generator; last below first raises ValueError, mixed versions raise TypeError.
Examples
import ipaddress
first = ipaddress.IPv4Address('10.0.0.0')
last = ipaddress.IPv4Address('10.0.0.10')
print([str(n) for n in ipaddress.summarize_address_range(first, last)])
# → ['10.0.0.0/29', '10.0.0.8/31', '10.0.0.10/32']
See also
Common errors
Open in the full glossarydocs.python.orgReport an inaccuracy