<s> '''<STR_LIT>''' <EOL> from itertools import imap as map <EOL> range = xrange <EOL> def long_range ( start , end ) : <EOL> while start < end : <EOL> yield start <EOL> start += <NUM_LIT:1> <EOL> bytes = bytearray <EOL> _builtin_isinstance = isinstance <EOL> def isinstance ( val , types ) : <EOL> if types is int : <EOL> types = ( int , long ) <EOL> elif type ( types ) is tuple and int in types : <EOL> types += ( long , ) <EOL> return _builtin_isinstance ( val , types ) <EOL> def _int_to_bytes ( self , length , byteorder , signed = False ) : <EOL> assert byteorder == '<STR_LIT>' and signed is False <EOL> if self < <NUM_LIT:0> or self >= <NUM_LIT> ** length : <EOL> raise OverflowError ( ) <EOL> return bytearray ( ( '<STR_LIT>' % ( length * <NUM_LIT:2> , self ) ) . decode ( '<STR_LIT>' ) ) <EOL> def _int_from_bytes ( what , byteorder , signed = False ) : <EOL> assert byteorder == '<STR_LIT>' and signed is False <EOL> return int ( str ( bytearray ( what ) ) . encode ( '<STR_LIT>' ) , <NUM_LIT:16> ) <EOL> if hasattr ( int , '<STR_LIT>' ) : <EOL> _int_bit_length = lambda i : i . bit_length ( ) <EOL> else : <EOL> _int_bit_length = lambda i : len ( bin ( abs ( i ) ) ) - <NUM_LIT:2> <EOL> """<STR_LIT>""" <EOL> __version__ = '<STR_LIT:1.0>' <EOL> import functools <EOL> IPV4LENGTH = <NUM_LIT:32> <EOL> IPV6LENGTH = <NUM_LIT> <EOL> class AddressValueError ( ValueError ) : <EOL> """<STR_LIT>""" <EOL> class NetmaskValueError ( ValueError ) : <EOL> """<STR_LIT>""" <EOL> def ip_address ( address ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> return IPv4Address ( address ) <EOL> except ( AddressValueError , NetmaskValueError ) : <EOL> pass <EOL> try : <EOL> return IPv6Address ( address ) <EOL> except ( AddressValueError , NetmaskValueError ) : <EOL> pass <EOL> raise ValueError ( '<STR_LIT>' % <EOL> address ) <EOL> def ip_network ( address , strict = True ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> return IPv4Network ( address , strict ) <EOL> except ( AddressValueError , NetmaskValueError ) : <EOL> pass <EOL> try : <EOL> return IPv6Network ( address , strict ) <EOL> except ( AddressValueError , NetmaskValueError ) : <EOL> pass <EOL> raise ValueError ( '<STR_LIT>' % <EOL> address ) <EOL> def ip_interface ( address ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> return IPv4Interface ( address ) <EOL> except ( AddressValueError , NetmaskValueError ) : <EOL> pass <EOL> try : <EOL> return IPv6Interface ( address ) <EOL> except ( AddressValueError , NetmaskValueError ) : <EOL> pass <EOL> raise ValueError ( '<STR_LIT>' % <EOL> address ) <EOL> def v4_int_to_packed ( address ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> return _int_to_bytes ( address , <NUM_LIT:4> , '<STR_LIT>' ) <EOL> except : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> def v6_int_to_packed ( address ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> return _int_to_bytes ( address , <NUM_LIT:16> , '<STR_LIT>' ) <EOL> except : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> def _split_optional_netmask ( address ) : <EOL> """<STR_LIT>""" <EOL> addr = str ( address ) . split ( '<STR_LIT:/>' ) <EOL> if len ( addr ) > <NUM_LIT:2> : <EOL> raise AddressValueError ( "<STR_LIT>" % address ) <EOL> return addr <EOL> def _find_address_range ( addresses ) : <EOL> """<STR_LIT>""" <EOL> first = last = addresses [ <NUM_LIT:0> ] <EOL> for ip in addresses [ <NUM_LIT:1> : ] : <EOL> if ip . _ip == last . _ip + <NUM_LIT:1> : <EOL> last = ip <EOL> else : <EOL> break <EOL> return ( first , last ) <EOL> def _count_righthand_zero_bits ( number , bits ) : <EOL> """<STR_LIT>""" <EOL> if number == <NUM_LIT:0> : <EOL> return bits <EOL> for i in range ( bits ) : <EOL> if ( number >> i ) & <NUM_LIT:1> : <EOL> return i <EOL> return bits <EOL> def summarize_address_range ( first , last ) : <EOL> """<STR_LIT>""" <EOL> if ( not ( isinstance ( first , _BaseAddress ) and <EOL> isinstance ( last , _BaseAddress ) ) ) : <EOL> raise TypeError ( '<STR_LIT>' ) <EOL> if first . version != last . version : <EOL> raise TypeError ( "<STR_LIT>" % ( <EOL> first , last ) ) <EOL> if first > last : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> if first . version == <NUM_LIT:4> : <EOL> ip = IPv4Network <EOL> elif first . version == <NUM_LIT:6> : <EOL> ip = IPv6Network <EOL> else : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> ip_bits = first . _max_prefixlen <EOL> first_int = first . _ip <EOL> last_int = last . _ip <EOL> while first_int <= last_int : <EOL> nbits = min ( _count_righthand_zero_bits ( first_int , ip_bits ) , <EOL> _int_bit_length ( last_int - first_int + <NUM_LIT:1> ) - <NUM_LIT:1> ) <EOL> net = ip ( '<STR_LIT>' % ( first , ip_bits - nbits ) ) <EOL> yield net <EOL> first_int += <NUM_LIT:1> << nbits <EOL> if first_int - <NUM_LIT:1> == ip . _ALL_ONES : <EOL> break <EOL> first = first . __class__ ( first_int ) <EOL> def _collapse_addresses_recursive ( addresses ) : <EOL> """<STR_LIT>""" <EOL> while True : <EOL> last_addr = None <EOL> ret_array = [ ] <EOL> optimized = False <EOL> for cur_addr in addresses : <EOL> if not ret_array : <EOL> last_addr = cur_addr <EOL> ret_array . append ( cur_addr ) <EOL> elif ( cur_addr . network_address >= last_addr . network_address and <EOL> cur_addr . broadcast_address <= last_addr . broadcast_address ) : <EOL> optimized = True <EOL> elif cur_addr == list ( last_addr . supernet ( ) . subnets ( ) ) [ <NUM_LIT:1> ] : <EOL> ret_array [ - <NUM_LIT:1> ] = last_addr = last_addr . supernet ( ) <EOL> optimized = True <EOL> else : <EOL> last_addr = cur_addr <EOL> ret_array . append ( cur_addr ) <EOL> addresses = ret_array <EOL> if not optimized : <EOL> return addresses <EOL> def collapse_addresses ( addresses ) : <EOL> """<STR_LIT>""" <EOL> i = <NUM_LIT:0> <EOL> addrs = [ ] <EOL> ips = [ ] <EOL> nets = [ ] <EOL> for ip in addresses : <EOL> if isinstance ( ip , _BaseAddress ) : <EOL> if ips and ips [ - <NUM_LIT:1> ] . _version != ip . _version : <EOL> raise TypeError ( "<STR_LIT>" % ( <EOL> ip , ips [ - <NUM_LIT:1> ] ) ) <EOL> ips . append ( ip ) <EOL> elif ip . _prefixlen == ip . _max_prefixlen : <EOL> if ips and ips [ - <NUM_LIT:1> ] . _version != ip . _version : <EOL> raise TypeError ( "<STR_LIT>" % ( <EOL> ip , ips [ - <NUM_LIT:1> ] ) ) <EOL> try : <EOL> ips . append ( ip . ip ) <EOL> except AttributeError : <EOL> ips . append ( ip . network_address ) <EOL> else : <EOL> if nets and nets [ - <NUM_LIT:1> ] . _version != ip . _version : <EOL> raise TypeError ( "<STR_LIT>" % ( <EOL> ip , nets [ - <NUM_LIT:1> ] ) ) <EOL> nets . append ( ip ) <EOL> ips = sorted ( set ( ips ) ) <EOL> nets = sorted ( set ( nets ) ) <EOL> while i < len ( ips ) : <EOL> ( first , last ) = _find_address_range ( ips [ i : ] ) <EOL> i = ips . index ( last ) + <NUM_LIT:1> <EOL> addrs . extend ( summarize_address_range ( first , last ) ) <EOL> return iter ( _collapse_addresses_recursive ( sorted ( <EOL> addrs + nets , key = _BaseNetwork . _get_networks_key ) ) ) <EOL> def get_mixed_type_key ( obj ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( obj , _BaseNetwork ) : <EOL> return obj . _get_networks_key ( ) <EOL> elif isinstance ( obj , _BaseAddress ) : <EOL> return obj . _get_address_key ( ) <EOL> return NotImplemented <EOL> class _TotalOrderingMixin ( object ) : <EOL> def __eq__ ( self , other ) : <EOL> raise NotImplementedError <EOL> def __ne__ ( self , other ) : <EOL> equal = self . __eq__ ( other ) <EOL> if equal is NotImplemented : <EOL> return NotImplemented <EOL> return not equal <EOL> def __lt__ ( self , other ) : <EOL> raise NotImplementedError <EOL> def __le__ ( self , other ) : <EOL> less = self . __lt__ ( other ) <EOL> if less is NotImplemented or not less : <EOL> return self . __eq__ ( other ) <EOL> return less <EOL> def __gt__ ( self , other ) : <EOL> less = self . __lt__ ( other ) <EOL> if less is NotImplemented : <EOL> return NotImplemented <EOL> equal = self . __eq__ ( other ) <EOL> if equal is NotImplemented : <EOL> return NotImplemented <EOL> return not ( less or equal ) <EOL> def __ge__ ( self , other ) : <EOL> less = self . __lt__ ( other ) <EOL> if less is NotImplemented : <EOL> return NotImplemented <EOL> return not less <EOL> class _IPAddressBase ( _TotalOrderingMixin ) : <EOL> """<STR_LIT>""" <EOL> @ property <EOL> def exploded ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _explode_shorthand_ip_string ( ) <EOL> @ property <EOL> def compressed ( self ) : <EOL> """<STR_LIT>""" <EOL> return str ( self ) <EOL> @ property <EOL> def reverse_pointer ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _reverse_pointer ( ) <EOL> @ property <EOL> def version ( self ) : <EOL> msg = '<STR_LIT>' % ( type ( self ) , ) <EOL> raise NotImplementedError ( msg ) <EOL> def _check_int_address ( self , address ) : <EOL> if address < <NUM_LIT:0> : <EOL> msg = "<STR_LIT>" <EOL> raise AddressValueError ( msg % ( address , self . _version ) ) <EOL> if address > self . _ALL_ONES : <EOL> msg = "<STR_LIT>" <EOL> raise AddressValueError ( msg % ( address , self . _max_prefixlen , <EOL> self . _version ) ) <EOL> def _check_packed_address ( self , address , expected_len ) : <EOL> address_len = len ( address ) <EOL> if address_len != expected_len : <EOL> msg = "<STR_LIT>" <EOL> raise AddressValueError ( msg % ( address , address_len , <EOL> expected_len , self . _version ) ) <EOL> def _ip_int_from_prefix ( self , prefixlen ) : <EOL> """<STR_LIT>""" <EOL> return self . _ALL_ONES ^ ( self . _ALL_ONES >> prefixlen ) <EOL> def _prefix_from_ip_int ( self , ip_int ) : <EOL> """<STR_LIT>""" <EOL> trailing_zeroes = _count_righthand_zero_bits ( ip_int , <EOL> self . _max_prefixlen ) <EOL> prefixlen = self . _max_prefixlen - trailing_zeroes <EOL> leading_ones = ip_int >> trailing_zeroes <EOL> all_ones = ( <NUM_LIT:1> << prefixlen ) - <NUM_LIT:1> <EOL> if leading_ones != all_ones : <EOL> byteslen = self . _max_prefixlen // <NUM_LIT:8> <EOL> details = _int_to_bytes ( ip_int , byteslen , '<STR_LIT>' ) <EOL> msg = '<STR_LIT>' <EOL> raise ValueError ( msg % details ) <EOL> return prefixlen <EOL> def _report_invalid_netmask ( self , netmask_str ) : <EOL> msg = '<STR_LIT>' % netmask_str <EOL> raise NetmaskValueError ( msg ) <EOL> def _prefix_from_prefix_string ( self , prefixlen_str ) : <EOL> """<STR_LIT>""" <EOL> if not _BaseV4 . _DECIMAL_DIGITS . issuperset ( prefixlen_str ) : <EOL> self . _report_invalid_netmask ( prefixlen_str ) <EOL> try : <EOL> prefixlen = int ( prefixlen_str ) <EOL> except ValueError : <EOL> self . _report_invalid_netmask ( prefixlen_str ) <EOL> if not ( <NUM_LIT:0> <= prefixlen <= self . _max_prefixlen ) : <EOL> self . _report_invalid_netmask ( prefixlen_str ) <EOL> return prefixlen <EOL> def _prefix_from_ip_string ( self , ip_str ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> ip_int = self . _ip_int_from_string ( ip_str ) <EOL> except AddressValueError : <EOL> self . _report_invalid_netmask ( ip_str ) <EOL> try : <EOL> return self . _prefix_from_ip_int ( ip_int ) <EOL> except ValueError : <EOL> pass <EOL> ip_int ^= self . _ALL_ONES <EOL> try : <EOL> return self . _prefix_from_ip_int ( ip_int ) <EOL> except ValueError : <EOL> self . _report_invalid_netmask ( ip_str ) <EOL> class _BaseAddress ( _IPAddressBase ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , address ) : <EOL> if ( not isinstance ( address , bytes ) <EOL> and '<STR_LIT:/>' in str ( address ) ) : <EOL> raise AddressValueError ( "<STR_LIT>" % address ) <EOL> def __int__ ( self ) : <EOL> return self . _ip <EOL> def __eq__ ( self , other ) : <EOL> try : <EOL> return ( self . _ip == other . _ip <EOL> and self . _version == other . _version ) <EOL> except AttributeError : <EOL> return NotImplemented <EOL> def __lt__ ( self , other ) : <EOL> if self . _version != other . _version : <EOL> raise TypeError ( '<STR_LIT>' % ( <EOL> self , other ) ) <EOL> if not isinstance ( other , _BaseAddress ) : <EOL> raise TypeError ( '<STR_LIT>' % ( <EOL> self , other ) ) <EOL> if self . _ip != other . _ip : <EOL> return self . _ip < other . _ip <EOL> return False <EOL> def __add__ ( self , other ) : <EOL> if not isinstance ( other , int ) : <EOL> return NotImplemented <EOL> return self . __class__ ( int ( self ) + other ) <EOL> def __sub__ ( self , other ) : <EOL> if not isinstance ( other , int ) : <EOL> return NotImplemented <EOL> return self . __class__ ( int ( self ) - other ) <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' % ( self . __class__ . __name__ , str ( self ) ) <EOL> def __str__ ( self ) : <EOL> return str ( self . _string_from_ip_int ( self . _ip ) ) <EOL> def __hash__ ( self ) : <EOL> return hash ( hex ( int ( self . _ip ) ) ) <EOL> def _get_address_key ( self ) : <EOL> return ( self . _version , self ) <EOL> class _BaseNetwork ( _IPAddressBase ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , address ) : <EOL> self . _cache = { } <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' % ( self . __class__ . __name__ , str ( self ) ) <EOL> def __str__ ( self ) : <EOL> return '<STR_LIT>' % ( self . network_address , self . prefixlen ) <EOL> def hosts ( self ) : <EOL> """<STR_LIT>""" <EOL> network = int ( self . network_address ) <EOL> broadcast = int ( self . broadcast_address ) <EOL> for x in long_range ( network + <NUM_LIT:1> , broadcast ) : <EOL> yield self . _address_class ( x ) <EOL> def __iter__ ( self ) : <EOL> network = int ( self . network_address ) <EOL> broadcast = int ( self . broadcast_address ) <EOL> for x in long_range ( network , broadcast + <NUM_LIT:1> ) : <EOL> yield self . _address_class ( x ) <EOL> def __getitem__ ( self , n ) : <EOL> network = int ( self . network_address ) <EOL> broadcast = int ( self . broadcast_address ) <EOL> if n >= <NUM_LIT:0> : <EOL> if network + n > broadcast : <EOL> raise IndexError <EOL> return self . _address_class ( network + n ) <EOL> else : <EOL> n += <NUM_LIT:1> <EOL> if broadcast + n < network : <EOL> raise IndexError <EOL> return self . _address_class ( broadcast + n ) <EOL> def __lt__ ( self , other ) : <EOL> if self . _version != other . _version : <EOL> raise TypeError ( '<STR_LIT>' % ( <EOL> self , other ) ) <EOL> if not isinstance ( other , _BaseNetwork ) : <EOL> raise TypeError ( '<STR_LIT>' % ( <EOL> self , other ) ) <EOL> if self . network_address != other . network_address : <EOL> return self . network_address < other . network_address <EOL> if self . netmask != other . netmask : <EOL> return self . netmask < other . netmask <EOL> return False <EOL> def __eq__ ( self , other ) : <EOL> try : <EOL> return ( self . _version == other . _version and <EOL> self . network_address == other . network_address and <EOL> int ( self . netmask ) == int ( other . netmask ) ) <EOL> except AttributeError : <EOL> return NotImplemented <EOL> def __hash__ ( self ) : <EOL> return hash ( int ( self . network_address ) ^ int ( self . netmask ) ) <EOL> def __contains__ ( self , other ) : <EOL> if self . _version != other . _version : <EOL> return False <EOL> if isinstance ( other , _BaseNetwork ) : <EOL> return False <EOL> else : <EOL> return ( int ( self . network_address ) <= int ( other . _ip ) <= <EOL> int ( self . broadcast_address ) ) <EOL> def overlaps ( self , other ) : <EOL> """<STR_LIT>""" <EOL> return self . network_address in other or ( <EOL> self . broadcast_address in other or ( <EOL> other . network_address in self or ( <EOL> other . broadcast_address in self ) ) ) <EOL> @ property <EOL> def broadcast_address ( self ) : <EOL> x = self . _cache . get ( '<STR_LIT>' ) <EOL> if x is None : <EOL> x = self . _address_class ( int ( self . network_address ) | <EOL> int ( self . hostmask ) ) <EOL> self . _cache [ '<STR_LIT>' ] = x <EOL> return x <EOL> @ property <EOL> def hostmask ( self ) : <EOL> x = self . _cache . get ( '<STR_LIT>' ) <EOL> if x is None : <EOL> x = self . _address_class ( int ( self . netmask ) ^ self . _ALL_ONES ) <EOL> self . _cache [ '<STR_LIT>' ] = x <EOL> return x <EOL> @ property <EOL> def with_prefixlen ( self ) : <EOL> return '<STR_LIT>' % ( self . network_address , self . _prefixlen ) <EOL> @ property <EOL> def with_netmask ( self ) : <EOL> return '<STR_LIT>' % ( self . network_address , self . netmask ) <EOL> @ property <EOL> def with_hostmask ( self ) : <EOL> return '<STR_LIT>' % ( self . network_address , self . hostmask ) <EOL> @ property <EOL> def num_addresses ( self ) : <EOL> """<STR_LIT>""" <EOL> return int ( self . broadcast_address ) - int ( self . network_address ) + <NUM_LIT:1> <EOL> @ property <EOL> def _address_class ( self ) : <EOL> msg = '<STR_LIT>' % ( type ( self ) , ) <EOL> raise NotImplementedError ( msg ) <EOL> @ property <EOL> def prefixlen ( self ) : <EOL> return self . _prefixlen <EOL> def address_exclude ( self , other ) : <EOL> """<STR_LIT>""" <EOL> if not self . _version == other . _version : <EOL> raise TypeError ( "<STR_LIT>" % ( <EOL> self , other ) ) <EOL> if not isinstance ( other , _BaseNetwork ) : <EOL> raise TypeError ( "<STR_LIT>" % other ) <EOL> if not ( other . network_address >= self . network_address and <EOL> other . broadcast_address <= self . broadcast_address ) : <EOL> raise ValueError ( '<STR_LIT>' % ( other , self ) ) <EOL> if other == self : <EOL> raise StopIteration <EOL> other = other . __class__ ( '<STR_LIT>' % ( other . network_address , <EOL> other . prefixlen ) ) <EOL> s1 , s2 = self . subnets ( ) <EOL> while s1 != other and s2 != other : <EOL> if ( other . network_address >= s1 . network_address and <EOL> other . broadcast_address <= s1 . broadcast_address ) : <EOL> yield s2 <EOL> s1 , s2 = s1 . subnets ( ) <EOL> elif ( other . network_address >= s2 . network_address and <EOL> other . broadcast_address <= s2 . broadcast_address ) : <EOL> yield s1 <EOL> s1 , s2 = s2 . subnets ( ) <EOL> else : <EOL> raise AssertionError ( '<STR_LIT>' <EOL> '<STR_LIT>' % <EOL> ( s1 , s2 , other ) ) <EOL> if s1 == other : <EOL> yield s2 <EOL> elif s2 == other : <EOL> yield s1 <EOL> else : <EOL> raise AssertionError ( '<STR_LIT>' <EOL> '<STR_LIT>' % <EOL> ( s1 , s2 , other ) ) <EOL> def compare_networks ( self , other ) : <EOL> """<STR_LIT>""" <EOL> if self . _version != other . _version : <EOL> raise TypeError ( '<STR_LIT>' % ( <EOL> self , other ) ) <EOL> if self . network_address < other . network_address : <EOL> return - <NUM_LIT:1> <EOL> if self . network_address > other . network_address : <EOL> return <NUM_LIT:1> <EOL> if self . netmask < other . netmask : <EOL> return - <NUM_LIT:1> <EOL> if self . netmask > other . netmask : <EOL> return <NUM_LIT:1> <EOL> return <NUM_LIT:0> <EOL> def _get_networks_key ( self ) : <EOL> """<STR_LIT>""" <EOL> return ( self . _version , self . network_address , self . netmask ) <EOL> def subnets ( self , prefixlen_diff = <NUM_LIT:1> , new_prefix = None ) : <EOL> """<STR_LIT>""" <EOL> if self . _prefixlen == self . _max_prefixlen : <EOL> yield self <EOL> return <EOL> if new_prefix is not None : <EOL> if new_prefix < self . _prefixlen : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> if prefixlen_diff != <NUM_LIT:1> : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> prefixlen_diff = new_prefix - self . _prefixlen <EOL> if prefixlen_diff < <NUM_LIT:0> : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> new_prefixlen = self . _prefixlen + prefixlen_diff <EOL> if new_prefixlen > self . _max_prefixlen : <EOL> raise ValueError ( <EOL> '<STR_LIT>' % ( <EOL> new_prefixlen , self ) ) <EOL> first = self . __class__ ( '<STR_LIT>' % <EOL> ( self . network_address , <EOL> self . _prefixlen + prefixlen_diff ) ) <EOL> yield first <EOL> current = first <EOL> while True : <EOL> broadcast = current . broadcast_address <EOL> if broadcast == self . broadcast_address : <EOL> return <EOL> new_addr = self . _address_class ( int ( broadcast ) + <NUM_LIT:1> ) <EOL> current = self . __class__ ( '<STR_LIT>' % ( new_addr , <EOL> new_prefixlen ) ) <EOL> yield current <EOL> def supernet ( self , prefixlen_diff = <NUM_LIT:1> , new_prefix = None ) : <EOL> """<STR_LIT>""" <EOL> if self . _prefixlen == <NUM_LIT:0> : <EOL> return self <EOL> if new_prefix is not None : <EOL> if new_prefix > self . _prefixlen : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> if prefixlen_diff != <NUM_LIT:1> : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> prefixlen_diff = self . _prefixlen - new_prefix <EOL> if self . prefixlen - prefixlen_diff < <NUM_LIT:0> : <EOL> raise ValueError ( <EOL> '<STR_LIT>' % <EOL> ( self . prefixlen , prefixlen_diff ) ) <EOL> t = self . __class__ ( '<STR_LIT>' % ( self . network_address , <EOL> self . prefixlen - prefixlen_diff ) , <EOL> strict = False ) <EOL> return t . __class__ ( '<STR_LIT>' % ( t . network_address , t . prefixlen ) ) <EOL> @ property <EOL> def is_multicast ( self ) : <EOL> """<STR_LIT>""" <EOL> return ( self . network_address . is_multicast and <EOL> self . broadcast_address . is_multicast ) <EOL> @ property <EOL> def is_reserved ( self ) : <EOL> """<STR_LIT>""" <EOL> return ( self . network_address . is_reserved and <EOL> self . broadcast_address . is_reserved ) <EOL> @ property <EOL> def is_link_local ( self ) : <EOL> """<STR_LIT>""" <EOL> return ( self . network_address . is_link_local and <EOL> self . broadcast_address . is_link_local ) <EOL> @ property <EOL> def is_private ( self ) : <EOL> """<STR_LIT>""" <EOL> return ( self . network_address . is_private and <EOL> self . broadcast_address . is_private ) <EOL> @ property <EOL> def is_global ( self ) : <EOL> """<STR_LIT>""" <EOL> return not self . is_private <EOL> @ property <EOL> def is_unspecified ( self ) : <EOL> """<STR_LIT>""" <EOL> return ( self . network_address . is_unspecified and <EOL> self . broadcast_address . is_unspecified ) <EOL> @ property <EOL> def is_loopback ( self ) : <EOL> """<STR_LIT>""" <EOL> return ( self . network_address . is_loopback and <EOL> self . broadcast_address . is_loopback ) <EOL> class _BaseV4 ( object ) : <EOL> """<STR_LIT>""" <EOL> _ALL_ONES = ( <NUM_LIT:2> ** IPV4LENGTH ) - <NUM_LIT:1> <EOL> _DECIMAL_DIGITS = frozenset ( '<STR_LIT>' ) <EOL> _valid_mask_octets = frozenset ( ( <NUM_LIT:255> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT:0> ) ) <EOL> def __init__ ( self , address ) : <EOL> self . _version = <NUM_LIT:4> <EOL> self . _max_prefixlen = IPV4LENGTH <EOL> def _explode_shorthand_ip_string ( self ) : <EOL> return str ( self ) <EOL> def _ip_int_from_string ( self , ip_str ) : <EOL> """<STR_LIT>""" <EOL> if not ip_str : <EOL> raise AddressValueError ( '<STR_LIT>' ) <EOL> octets = ip_str . split ( '<STR_LIT:.>' ) <EOL> if len ( octets ) != <NUM_LIT:4> : <EOL> raise AddressValueError ( "<STR_LIT>" % ip_str ) <EOL> try : <EOL> return _int_from_bytes ( map ( self . _parse_octet , octets ) , '<STR_LIT>' ) <EOL> except ValueError as exc : <EOL> raise AddressValueError ( "<STR_LIT>" % ( exc , ip_str ) ) <EOL> def _parse_octet ( self , octet_str ) : <EOL> """<STR_LIT>""" <EOL> if not octet_str : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> if not self . _DECIMAL_DIGITS . issuperset ( octet_str ) : <EOL> msg = "<STR_LIT>" <EOL> raise ValueError ( msg % octet_str ) <EOL> if len ( octet_str ) > <NUM_LIT:3> : <EOL> msg = "<STR_LIT>" <EOL> raise ValueError ( msg % octet_str ) <EOL> octet_int = int ( octet_str , <NUM_LIT:10> ) <EOL> if octet_int > <NUM_LIT:7> and octet_str [ <NUM_LIT:0> ] == '<STR_LIT:0>' : <EOL> msg = "<STR_LIT>" <EOL> raise ValueError ( msg % octet_str ) <EOL> if octet_int > <NUM_LIT:255> : <EOL> raise ValueError ( "<STR_LIT>" % octet_int ) <EOL> return octet_int <EOL> def _string_from_ip_int ( self , ip_int ) : <EOL> """<STR_LIT>""" <EOL> return '<STR_LIT:.>' . join ( map ( str , _int_to_bytes ( ip_int , <NUM_LIT:4> , '<STR_LIT>' ) ) ) <EOL> def _is_valid_netmask ( self , netmask ) : <EOL> """<STR_LIT>""" <EOL> mask = netmask . split ( '<STR_LIT:.>' ) <EOL> if len ( mask ) == <NUM_LIT:4> : <EOL> try : <EOL> for x in mask : <EOL> if int ( x ) not in self . _valid_mask_octets : <EOL> return False <EOL> except ValueError : <EOL> return False <EOL> for idx , y in enumerate ( mask ) : <EOL> if idx > <NUM_LIT:0> and y > mask [ idx - <NUM_LIT:1> ] : <EOL> return False <EOL> return True <EOL> try : <EOL> netmask = int ( netmask ) <EOL> except ValueError : <EOL> return False <EOL> return <NUM_LIT:0> <= netmask <= self . _max_prefixlen <EOL> def _is_hostmask ( self , ip_str ) : <EOL> """<STR_LIT>""" <EOL> bits = ip_str . split ( '<STR_LIT:.>' ) <EOL> try : <EOL> parts = [ x for x in map ( int , bits ) if x in self . _valid_mask_octets ] <EOL> except ValueError : <EOL> return False <EOL> if len ( parts ) != len ( bits ) : <EOL> return False <EOL> if parts [ <NUM_LIT:0> ] < parts [ - <NUM_LIT:1> ] : <EOL> return True <EOL> return False <EOL> def _reverse_pointer ( self ) : <EOL> """<STR_LIT>""" <EOL> reverse_octets = str ( self ) . split ( '<STR_LIT:.>' ) [ : : - <NUM_LIT:1> ] <EOL> return '<STR_LIT:.>' . join ( reverse_octets ) + '<STR_LIT>' <EOL> @ property <EOL> def max_prefixlen ( self ) : <EOL> return self . _max_prefixlen <EOL> @ property <EOL> def version ( self ) : <EOL> return self . _version <EOL> class IPv4Address ( _BaseV4 , _BaseAddress ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , address ) : <EOL> """<STR_LIT>""" <EOL> _BaseAddress . __init__ ( self , address ) <EOL> _BaseV4 . __init__ ( self , address ) <EOL> if isinstance ( address , int ) : <EOL> self . _check_int_address ( address ) <EOL> self . _ip = address <EOL> return <EOL> if isinstance ( address , bytes ) : <EOL> self . _check_packed_address ( address , <NUM_LIT:4> ) <EOL> self . _ip = _int_from_bytes ( address , '<STR_LIT>' ) <EOL> return <EOL> addr_str = str ( address ) <EOL> self . _ip = self . _ip_int_from_string ( addr_str ) <EOL> @ property <EOL> def packed ( self ) : <EOL> """<STR_LIT>""" <EOL> return v4_int_to_packed ( self . _ip ) <EOL> @ property <EOL> def is_reserved ( self ) : <EOL> """<STR_LIT>""" <EOL> reserved_network = IPv4Network ( '<STR_LIT>' ) <EOL> return self in reserved_network <EOL> @ property <EOL> def is_private ( self ) : <EOL> """<STR_LIT>""" <EOL> return ( self in IPv4Network ( '<STR_LIT>' ) or <EOL> self in IPv4Network ( '<STR_LIT>' ) or <EOL> self in IPv4Network ( '<STR_LIT>' ) or <EOL> self in IPv4Network ( '<STR_LIT>' ) or <EOL> self in IPv4Network ( '<STR_LIT>' ) or <EOL> self in IPv4Network ( '<STR_LIT>' ) or <EOL> self in IPv4Network ( '<STR_LIT>' ) or <EOL> self in IPv4Network ( '<STR_LIT>' ) or <EOL> self in IPv4Network ( '<STR_LIT>' ) or <EOL> self in IPv4Network ( '<STR_LIT>' ) or <EOL> self in IPv4Network ( '<STR_LIT>' ) or <EOL> self in IPv4Network ( '<STR_LIT>' ) or <EOL> self in IPv4Network ( '<STR_LIT>' ) or <EOL> self in IPv4Network ( '<STR_LIT>' ) ) <EOL> @ property <EOL> def is_multicast ( self ) : <EOL> """<STR_LIT>""" <EOL> multicast_network = IPv4Network ( '<STR_LIT>' ) <EOL> return self in multicast_network <EOL> @ property <EOL> def is_unspecified ( self ) : <EOL> """<STR_LIT>""" <EOL> unspecified_address = IPv4Address ( '<STR_LIT>' ) <EOL> return self == unspecified_address <EOL> @ property <EOL> def is_loopback ( self ) : <EOL> """<STR_LIT>""" <EOL> loopback_network = IPv4Network ( '<STR_LIT>' ) <EOL> return self in loopback_network <EOL> @ property <EOL> def is_link_local ( self ) : <EOL> """<STR_LIT>""" <EOL> linklocal_network = IPv4Network ( '<STR_LIT>' ) <EOL> return self in linklocal_network <EOL> class IPv4Interface ( IPv4Address ) : <EOL> def __init__ ( self , address ) : <EOL> if isinstance ( address , ( bytes , int ) ) : <EOL> IPv4Address . __init__ ( self , address ) <EOL> self . network = IPv4Network ( self . _ip ) <EOL> self . _prefixlen = self . _max_prefixlen <EOL> return <EOL> addr = _split_optional_netmask ( address ) <EOL> IPv4Address . __init__ ( self , addr [ <NUM_LIT:0> ] ) <EOL> self . network = IPv4Network ( address , strict = False ) <EOL> self . _prefixlen = self . network . _prefixlen <EOL> self . netmask = self . network . netmask <EOL> self . hostmask = self . network . hostmask <EOL> def __str__ ( self ) : <EOL> return '<STR_LIT>' % ( self . _string_from_ip_int ( self . _ip ) , <EOL> self . network . prefixlen ) <EOL> def __eq__ ( self , other ) : <EOL> address_equal = IPv4Address . __eq__ ( self , other ) <EOL> if not address_equal or address_equal is NotImplemented : <EOL> return address_equal <EOL> try : <EOL> return self . network == other . network <EOL> except AttributeError : <EOL> return False <EOL> def __lt__ ( self , other ) : <EOL> address_less = IPv4Address . __lt__ ( self , other ) <EOL> if address_less is NotImplemented : <EOL> return NotImplemented <EOL> try : <EOL> return self . network < other . network <EOL> except AttributeError : <EOL> return False <EOL> def __hash__ ( self ) : <EOL> return self . _ip ^ self . _prefixlen ^ int ( self . network . network_address ) <EOL> @ property <EOL> def ip ( self ) : <EOL> return IPv4Address ( self . _ip ) <EOL> @ property <EOL> def with_prefixlen ( self ) : <EOL> return '<STR_LIT>' % ( self . _string_from_ip_int ( self . _ip ) , <EOL> self . _prefixlen ) <EOL> @ property <EOL> def with_netmask ( self ) : <EOL> return '<STR_LIT>' % ( self . _string_from_ip_int ( self . _ip ) , <EOL> self . netmask ) <EOL> @ property <EOL> def with_hostmask ( self ) : <EOL> return '<STR_LIT>' % ( self . _string_from_ip_int ( self . _ip ) , <EOL> self . hostmask ) <EOL> class IPv4Network ( _BaseV4 , _BaseNetwork ) : <EOL> """<STR_LIT>""" <EOL> _address_class = IPv4Address <EOL> def __init__ ( self , address , strict = True ) : <EOL> """<STR_LIT>""" <EOL> _BaseV4 . __init__ ( self , address ) <EOL> _BaseNetwork . __init__ ( self , address ) <EOL> if isinstance ( address , bytes ) : <EOL> self . network_address = IPv4Address ( address ) <EOL> self . _prefixlen = self . _max_prefixlen <EOL> self . netmask = IPv4Address ( self . _ALL_ONES ) <EOL> return <EOL> if isinstance ( address , int ) : <EOL> self . network_address = IPv4Address ( address ) <EOL> self . _prefixlen = self . _max_prefixlen <EOL> self . netmask = IPv4Address ( self . _ALL_ONES ) <EOL> return <EOL> addr = _split_optional_netmask ( address ) <EOL> self . network_address = IPv4Address ( self . _ip_int_from_string ( addr [ <NUM_LIT:0> ] ) ) <EOL> if len ( addr ) == <NUM_LIT:2> : <EOL> try : <EOL> self . _prefixlen = self . _prefix_from_prefix_string ( addr [ <NUM_LIT:1> ] ) <EOL> except NetmaskValueError : <EOL> self . _prefixlen = self . _prefix_from_ip_string ( addr [ <NUM_LIT:1> ] ) <EOL> else : <EOL> self . _prefixlen = self . _max_prefixlen <EOL> self . netmask = IPv4Address ( self . _ip_int_from_prefix ( self . _prefixlen ) ) <EOL> if strict : <EOL> if ( IPv4Address ( int ( self . network_address ) & int ( self . netmask ) ) != <EOL> self . network_address ) : <EOL> raise ValueError ( '<STR_LIT>' % self ) <EOL> self . network_address = IPv4Address ( int ( self . network_address ) & <EOL> int ( self . netmask ) ) <EOL> if self . _prefixlen == ( self . _max_prefixlen - <NUM_LIT:1> ) : <EOL> self . hosts = self . __iter__ <EOL> @ property <EOL> def is_global ( self ) : <EOL> """<STR_LIT>""" <EOL> return ( not ( self . network_address in IPv4Network ( '<STR_LIT>' ) and <EOL> self . broadcast_address in IPv4Network ( '<STR_LIT>' ) ) and <EOL> not self . is_private ) <EOL> class _BaseV6 ( object ) : <EOL> """<STR_LIT>""" <EOL> _ALL_ONES = ( <NUM_LIT:2> ** IPV6LENGTH ) - <NUM_LIT:1> <EOL> _HEXTET_COUNT = <NUM_LIT:8> <EOL> _HEX_DIGITS = frozenset ( '<STR_LIT>' ) <EOL> def __init__ ( self , address ) : <EOL> self . _version = <NUM_LIT:6> <EOL> self . _max_prefixlen = IPV6LENGTH <EOL> def _ip_int_from_string ( self , ip_str ) : <EOL> """<STR_LIT>""" <EOL> if not ip_str : <EOL> raise AddressValueError ( '<STR_LIT>' ) <EOL> parts = ip_str . split ( '<STR_LIT::>' ) <EOL> _min_parts = <NUM_LIT:3> <EOL> if len ( parts ) < _min_parts : <EOL> msg = "<STR_LIT>" % ( _min_parts , ip_str ) <EOL> raise AddressValueError ( msg ) <EOL> if '<STR_LIT:.>' in parts [ - <NUM_LIT:1> ] : <EOL> try : <EOL> ipv4_int = IPv4Address ( parts . pop ( ) ) . _ip <EOL> except AddressValueError as exc : <EOL> raise AddressValueError ( "<STR_LIT>" % ( exc , ip_str ) ) <EOL> parts . append ( '<STR_LIT>' % ( ( ipv4_int >> <NUM_LIT:16> ) & <NUM_LIT> ) ) <EOL> parts . append ( '<STR_LIT>' % ( ipv4_int & <NUM_LIT> ) ) <EOL> _max_parts = self . _HEXTET_COUNT + <NUM_LIT:1> <EOL> if len ( parts ) > _max_parts : <EOL> msg = "<STR_LIT>" % ( _max_parts - <NUM_LIT:1> , ip_str ) <EOL> raise AddressValueError ( msg ) <EOL> skip_index = None <EOL> for i in range ( <NUM_LIT:1> , len ( parts ) - <NUM_LIT:1> ) : <EOL> if not parts [ i ] : <EOL> if skip_index is not None : <EOL> msg = "<STR_LIT>" % ip_str <EOL> raise AddressValueError ( msg ) <EOL> skip_index = i <EOL> if skip_index is not None : <EOL> parts_hi = skip_index <EOL> parts_lo = len ( parts ) - skip_index - <NUM_LIT:1> <EOL> if not parts [ <NUM_LIT:0> ] : <EOL> parts_hi -= <NUM_LIT:1> <EOL> if parts_hi : <EOL> msg = "<STR_LIT>" <EOL> raise AddressValueError ( msg % ip_str ) <EOL> if not parts [ - <NUM_LIT:1> ] : <EOL> parts_lo -= <NUM_LIT:1> <EOL> if parts_lo : <EOL> msg = "<STR_LIT>" <EOL> raise AddressValueError ( msg % ip_str ) <EOL> parts_skipped = self . _HEXTET_COUNT - ( parts_hi + parts_lo ) <EOL> if parts_skipped < <NUM_LIT:1> : <EOL> msg = "<STR_LIT>" <EOL> raise AddressValueError ( msg % ( self . _HEXTET_COUNT - <NUM_LIT:1> , ip_str ) ) <EOL> else : <EOL> if len ( parts ) != self . _HEXTET_COUNT : <EOL> msg = "<STR_LIT>" <EOL> raise AddressValueError ( msg % ( self . _HEXTET_COUNT , ip_str ) ) <EOL> if not parts [ <NUM_LIT:0> ] : <EOL> msg = "<STR_LIT>" <EOL> raise AddressValueError ( msg % ip_str ) <EOL> if not parts [ - <NUM_LIT:1> ] : <EOL> msg = "<STR_LIT>" <EOL> raise AddressValueError ( msg % ip_str ) <EOL> parts_hi = len ( parts ) <EOL> parts_lo = <NUM_LIT:0> <EOL> parts_skipped = <NUM_LIT:0> <EOL> try : <EOL> ip_int = <NUM_LIT:0> <EOL> for i in range ( parts_hi ) : <EOL> ip_int <<= <NUM_LIT:16> <EOL> ip_int |= self . _parse_hextet ( parts [ i ] ) <EOL> ip_int <<= <NUM_LIT:16> * parts_skipped <EOL> for i in range ( - parts_lo , <NUM_LIT:0> ) : <EOL> ip_int <<= <NUM_LIT:16> <EOL> ip_int |= self . _parse_hextet ( parts [ i ] ) <EOL> return ip_int <EOL> except ValueError as exc : <EOL> raise AddressValueError ( "<STR_LIT>" % ( exc , ip_str ) ) <EOL> def _parse_hextet ( self , hextet_str ) : <EOL> """<STR_LIT>""" <EOL> if not self . _HEX_DIGITS . issuperset ( hextet_str ) : <EOL> raise ValueError ( "<STR_LIT>" % hextet_str ) <EOL> if len ( hextet_str ) > <NUM_LIT:4> : <EOL> msg = "<STR_LIT>" <EOL> raise ValueError ( msg % hextet_str ) <EOL> return int ( hextet_str , <NUM_LIT:16> ) <EOL> def _compress_hextets ( self , hextets ) : <EOL> """<STR_LIT>""" <EOL> best_doublecolon_start = - <NUM_LIT:1> <EOL> best_doublecolon_len = <NUM_LIT:0> <EOL> doublecolon_start = - <NUM_LIT:1> <EOL> doublecolon_len = <NUM_LIT:0> <EOL> for index , hextet in enumerate ( hextets ) : <EOL> if hextet == '<STR_LIT:0>' : <EOL> doublecolon_len += <NUM_LIT:1> <EOL> if doublecolon_start == - <NUM_LIT:1> : <EOL> doublecolon_start = index <EOL> if doublecolon_len > best_doublecolon_len : <EOL> best_doublecolon_len = doublecolon_len <EOL> best_doublecolon_start = doublecolon_start <EOL> else : <EOL> doublecolon_len = <NUM_LIT:0> <EOL> doublecolon_start = - <NUM_LIT:1> <EOL> if best_doublecolon_len > <NUM_LIT:1> : <EOL> best_doublecolon_end = ( best_doublecolon_start + <EOL> best_doublecolon_len ) <EOL> if best_doublecolon_end == len ( hextets ) : <EOL> hextets += [ '<STR_LIT>' ] <EOL> hextets [ best_doublecolon_start : best_doublecolon_end ] = [ '<STR_LIT>' ] <EOL> if best_doublecolon_start == <NUM_LIT:0> : <EOL> hextets = [ '<STR_LIT>' ] + hextets <EOL> return hextets <EOL> def _string_from_ip_int ( self , ip_int = None ) : <EOL> """<STR_LIT>""" <EOL> if ip_int is None : <EOL> ip_int = int ( self . _ip ) <EOL> if ip_int > self . _ALL_ONES : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> hex_str = '<STR_LIT>' % ip_int <EOL> hextets = [ '<STR_LIT>' % int ( hex_str [ x : x + <NUM_LIT:4> ] , <NUM_LIT:16> ) for x in range ( <NUM_LIT:0> , <NUM_LIT:32> , <NUM_LIT:4> ) ] <EOL> hextets = self . _compress_hextets ( hextets ) <EOL> return '<STR_LIT::>' . join ( hextets ) <EOL> def _explode_shorthand_ip_string ( self ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( self , IPv6Network ) : <EOL> ip_str = str ( self . network_address ) <EOL> elif isinstance ( self , IPv6Interface ) : <EOL> ip_str = str ( self . ip ) <EOL> else : <EOL> ip_str = str ( self ) <EOL> ip_int = self . _ip_int_from_string ( ip_str ) <EOL> hex_str = '<STR_LIT>' % ip_int <EOL> parts = [ hex_str [ x : x + <NUM_LIT:4> ] for x in range ( <NUM_LIT:0> , <NUM_LIT:32> , <NUM_LIT:4> ) ] <EOL> if isinstance ( self , ( _BaseNetwork , IPv6Interface ) ) : <EOL> return '<STR_LIT>' % ( '<STR_LIT::>' . join ( parts ) , self . _prefixlen ) <EOL> return '<STR_LIT::>' . join ( parts ) <EOL> def _reverse_pointer ( self ) : <EOL> """<STR_LIT>""" <EOL> reverse_chars = self . exploded [ : : - <NUM_LIT:1> ] . replace ( '<STR_LIT::>' , '<STR_LIT>' ) <EOL> return '<STR_LIT:.>' . join ( reverse_chars ) + '<STR_LIT>' <EOL> @ property <EOL> def max_prefixlen ( self ) : <EOL> return self . _max_prefixlen <EOL> @ property <EOL> def version ( self ) : <EOL> return self . _version <EOL> class IPv6Address ( _BaseV6 , _BaseAddress ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , address ) : <EOL> """<STR_LIT>""" <EOL> _BaseAddress . __init__ ( self , address ) <EOL> _BaseV6 . __init__ ( self , address ) <EOL> if isinstance ( address , int ) : <EOL> self . _check_int_address ( address ) <EOL> self . _ip = address <EOL> return <EOL> if isinstance ( address , bytes ) : <EOL> self . _check_packed_address ( address , <NUM_LIT:16> ) <EOL> self . _ip = _int_from_bytes ( address , '<STR_LIT>' ) <EOL> return <EOL> addr_str = str ( address ) <EOL> self . _ip = self . _ip_int_from_string ( addr_str ) <EOL> @ property <EOL> def packed ( self ) : <EOL> """<STR_LIT>""" <EOL> return v6_int_to_packed ( self . _ip ) <EOL> @ property <EOL> def is_multicast ( self ) : <EOL> """<STR_LIT>""" <EOL> multicast_network = IPv6Network ( '<STR_LIT>' ) <EOL> return self in multicast_network <EOL> @ property <EOL> def is_reserved ( self ) : <EOL> """<STR_LIT>""" <EOL> reserved_networks = [ IPv6Network ( '<STR_LIT>' ) , IPv6Network ( '<STR_LIT>' ) , <EOL> IPv6Network ( '<STR_LIT>' ) , IPv6Network ( '<STR_LIT>' ) , <EOL> IPv6Network ( '<STR_LIT>' ) , IPv6Network ( '<STR_LIT>' ) , <EOL> IPv6Network ( '<STR_LIT>' ) , IPv6Network ( '<STR_LIT>' ) , <EOL> IPv6Network ( '<STR_LIT>' ) , IPv6Network ( '<STR_LIT>' ) , <EOL> IPv6Network ( '<STR_LIT>' ) , IPv6Network ( '<STR_LIT>' ) , <EOL> IPv6Network ( '<STR_LIT>' ) , IPv6Network ( '<STR_LIT>' ) , <EOL> IPv6Network ( '<STR_LIT>' ) ] <EOL> return any ( self in x for x in reserved_networks ) <EOL> @ property <EOL> def is_link_local ( self ) : <EOL> """<STR_LIT>""" <EOL> linklocal_network = IPv6Network ( '<STR_LIT>' ) <EOL> return self in linklocal_network <EOL> @ property <EOL> def is_site_local ( self ) : <EOL> """<STR_LIT>""" <EOL> sitelocal_network = IPv6Network ( '<STR_LIT>' ) <EOL> return self in sitelocal_network <EOL> @ property <EOL> def is_private ( self ) : <EOL> """<STR_LIT>""" <EOL> return ( self in IPv6Network ( '<STR_LIT>' ) or <EOL> self in IPv6Network ( '<STR_LIT>' ) or <EOL> self in IPv6Network ( '<STR_LIT>' ) or <EOL> self in IPv6Network ( '<STR_LIT>' ) or <EOL> self in IPv6Network ( '<STR_LIT>' ) or <EOL> self in IPv6Network ( '<STR_LIT>' ) or <EOL> self in IPv6Network ( '<STR_LIT>' ) or <EOL> self in IPv6Network ( '<STR_LIT>' ) or <EOL> self in IPv6Network ( '<STR_LIT>' ) or <EOL> self in IPv6Network ( '<STR_LIT>' ) ) <EOL> @ property <EOL> def is_global ( self ) : <EOL> """<STR_LIT>""" <EOL> return not self . is_private <EOL> @ property <EOL> def is_unspecified ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _ip == <NUM_LIT:0> <EOL> @ property <EOL> def is_loopback ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _ip == <NUM_LIT:1> <EOL> @ property <EOL> def ipv4_mapped ( self ) : <EOL> """<STR_LIT>""" <EOL> if ( self . _ip >> <NUM_LIT:32> ) != <NUM_LIT> : <EOL> return None <EOL> return IPv4Address ( self . _ip & <NUM_LIT> ) <EOL> @ property <EOL> def teredo ( self ) : <EOL> """<STR_LIT>""" <EOL> if ( self . _ip >> <NUM_LIT> ) != <NUM_LIT> : <EOL> return None <EOL> return ( IPv4Address ( ( self . _ip >> <NUM_LIT:64> ) & <NUM_LIT> ) , <EOL> IPv4Address ( ~ self . _ip & <NUM_LIT> ) ) <EOL> @ property <EOL> def sixtofour ( self ) : <EOL> """<STR_LIT>""" <EOL> if ( self . _ip >> <NUM_LIT> ) != <NUM_LIT> : <EOL> return None <EOL> return IPv4Address ( ( self . _ip >> <NUM_LIT> ) & <NUM_LIT> ) <EOL> class IPv6Interface ( IPv6Address ) : <EOL> def __init__ ( self , address ) : <EOL> if isinstance ( address , ( bytes , int ) ) : <EOL> IPv6Address . __init__ ( self , address ) <EOL> self . network = IPv6Network ( self . _ip ) <EOL> self . _prefixlen = self . _max_prefixlen <EOL> return <EOL> addr = _split_optional_netmask ( address ) <EOL> IPv6Address . __init__ ( self , addr [ <NUM_LIT:0> ] ) <EOL> self . network = IPv6Network ( address , strict = False ) <EOL> self . netmask = self . network . netmask <EOL> self . _prefixlen = self . network . _prefixlen <EOL> self . hostmask = self . network . hostmask <EOL> def __str__ ( self ) : <EOL> return '<STR_LIT>' % ( self . _string_from_ip_int ( self . _ip ) , <EOL> self . network . prefixlen ) <EOL> def __eq__ ( self , other ) : <EOL> address_equal = IPv6Address . __eq__ ( self , other ) <EOL> if not address_equal or address_equal is NotImplemented : <EOL> return address_equal <EOL> try : <EOL> return self . network == other . network <EOL> except AttributeError : <EOL> return False <EOL> def __lt__ ( self , other ) : <EOL> address_less = IPv6Address . __lt__ ( self , other ) <EOL> if address_less is NotImplemented : <EOL> return NotImplemented <EOL> try : <EOL> return self . network < other . network <EOL> except AttributeError : <EOL> return False <EOL> def __hash__ ( self ) : <EOL> return self . _ip ^ self . _prefixlen ^ int ( self . network . network_address ) <EOL> @ property <EOL> def ip ( self ) : <EOL> return IPv6Address ( self . _ip ) <EOL> @ property <EOL> def with_prefixlen ( self ) : <EOL> return '<STR_LIT>' % ( self . _string_from_ip_int ( self . _ip ) , <EOL> self . _prefixlen ) <EOL> @ property <EOL> def with_netmask ( self ) : <EOL> return '<STR_LIT>' % ( self . _string_from_ip_int ( self . _ip ) , <EOL> self . netmask ) <EOL> @ property <EOL> def with_hostmask ( self ) : <EOL> return '<STR_LIT>' % ( self . _string_from_ip_int ( self . _ip ) , <EOL> self . hostmask ) <EOL> @ property <EOL> def is_unspecified ( self ) : <EOL> return self . _ip == <NUM_LIT:0> and self . network . is_unspecified <EOL> @ property <EOL> def is_loopback ( self ) : <EOL> return self . _ip == <NUM_LIT:1> and self . network . is_loopback <EOL> class IPv6Network ( _BaseV6 , _BaseNetwork ) : <EOL> """<STR_LIT>""" <EOL> _address_class = IPv6Address <EOL> def __init__ ( self , address , strict = True ) : <EOL> """<STR_LIT>""" <EOL> _BaseV6 . __init__ ( self , address ) <EOL> _BaseNetwork . __init__ ( self , address ) <EOL> if isinstance ( address , int ) : <EOL> self . network_address = IPv6Address ( address ) <EOL> self . _prefixlen = self . _max_prefixlen <EOL> self . netmask = IPv6Address ( self . _ALL_ONES ) <EOL> return <EOL> if isinstance ( address , bytes ) : <EOL> self . network_address = IPv6Address ( address ) <EOL> self . _prefixlen = self . _max_prefixlen <EOL> self . netmask = IPv6Address ( self . _ALL_ONES ) <EOL> return <EOL> addr = _split_optional_netmask ( address ) <EOL> self . network_address = IPv6Address ( self . _ip_int_from_string ( addr [ <NUM_LIT:0> ] ) ) <EOL> if len ( addr ) == <NUM_LIT:2> : <EOL> self . _prefixlen = self . _prefix_from_prefix_string ( addr [ <NUM_LIT:1> ] ) <EOL> else : <EOL> self . _prefixlen = self . _max_prefixlen <EOL> self . netmask = IPv6Address ( self . _ip_int_from_prefix ( self . _prefixlen ) ) <EOL> if strict : <EOL> if ( IPv6Address ( int ( self . network_address ) & int ( self . netmask ) ) != <EOL> self . network_address ) : <EOL> raise ValueError ( '<STR_LIT>' % self ) <EOL> self . network_address = IPv6Address ( int ( self . network_address ) & <EOL> int ( self . netmask ) ) <EOL> if self . _prefixlen == ( self . _max_prefixlen - <NUM_LIT:1> ) : <EOL> self . hosts = self . __iter__ <EOL> def hosts ( self ) : <EOL> """<STR_LIT>""" <EOL> network = int ( self . network_address ) <EOL> broadcast = int ( self . broadcast_address ) <EOL> for x in range ( <NUM_LIT:1> , broadcast - network + <NUM_LIT:1> ) : <EOL> yield self . _address_class ( network + x ) <EOL> @ property <EOL> def is_site_local ( self ) : <EOL> """<STR_LIT>""" <EOL> return ( self . network_address . is_site_local and <EOL> self . broadcast_address . is_site_local ) </s>
