2011-07-14 12 views

答えて

1

Google's ipaddr-py libraryはsummarize_address_range(最初、最後)と呼ばれるメソッドを有します。

summarize_address_range(first, last): 
"""Summarize a network range given the first and last IP addresses. 

Example: 
    >>> summarize_address_range(IPv4Address('1.1.1.0'), 
     IPv4Address('1.1.1.130')) 
    [IPv4Network('1.1.1.0/25'), IPv4Network('1.1.1.128/31'), 
    IPv4Network('1.1.1.130/32')] 

Args: 
    first: the first IPv4Address or IPv6Address in the range. 
    last: the last IPv4Address or IPv6Address in the range. 

Returns: 
    The address range collapsed to a list of IPv4Network's or 
    IPv6Network's. 

Raise: 
    TypeError: 
     If the first and last objects are not IP addresses. 
     If the first and last objects are not the same version. 
    ValueError: 
     If the last object is not greater than the first. 
     If the version is not 4 or 6. 
""" 
1

別の解決策:

from ipaddress import IPv4Address, summarize_address_range 

a=" ".join(map(str, summarize_address_range(IPv4Address('8.8.8.8'), IPv4Address('8.8.9.1')))) 

print(a) 
関連する問題