<s> """<STR_LIT>""" <EOL> __version__ = '<STR_LIT>' <EOL> import sys <EOL> import types <EOL> IPv4ranges = { <EOL> '<STR_LIT:0>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:1>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> } <EOL> IPv6ranges = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:0>' * <NUM_LIT> : '<STR_LIT>' , <EOL> '<STR_LIT:0>' * <NUM_LIT> : '<STR_LIT>' , <EOL> '<STR_LIT:0>' * <NUM_LIT> + '<STR_LIT:1>' : '<STR_LIT>' , <EOL> '<STR_LIT:0>' * <NUM_LIT> + '<STR_LIT:1>' * <NUM_LIT:16> : '<STR_LIT>' , <EOL> '<STR_LIT>' + '<STR_LIT:0>' * <NUM_LIT:64> : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' + '<STR_LIT:0>' * <NUM_LIT:16> : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:100>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } <EOL> class IPint : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , data , ipversion = <NUM_LIT:0> , make_net = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> self . NoPrefixForSingleIp = <NUM_LIT:1> <EOL> self . WantPrefixLen = None <EOL> netbits = <NUM_LIT:0> <EOL> prefixlen = - <NUM_LIT:1> <EOL> if isinstance ( data , ( int , long ) ) : <EOL> self . ip = long ( data ) <EOL> if ipversion == <NUM_LIT:0> : <EOL> if self . ip < <NUM_LIT> : <EOL> ipversion = <NUM_LIT:4> <EOL> else : <EOL> ipversion = <NUM_LIT:6> <EOL> if ipversion == <NUM_LIT:4> : <EOL> if self . ip > <NUM_LIT> : <EOL> raise ValueError ( "<STR_LIT>" % self . ip ) <EOL> prefixlen = <NUM_LIT:32> <EOL> elif ipversion == <NUM_LIT:6> : <EOL> if self . ip > <NUM_LIT> : <EOL> raise ValueError ( "<STR_LIT>" % self . ip ) <EOL> prefixlen = <NUM_LIT> <EOL> else : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> self . _ipversion = ipversion <EOL> self . _prefixlen = prefixlen <EOL> elif isinstance ( data , IPint ) : <EOL> self . _ipversion = data . _ipversion <EOL> self . _prefixlen = data . _prefixlen <EOL> self . ip = data . ip <EOL> elif isinstance ( data , ( str , unicode ) ) : <EOL> x = data . split ( '<STR_LIT:->' ) <EOL> if len ( x ) == <NUM_LIT:2> : <EOL> ( ip , last ) = x <EOL> ( self . ip , parsedVersion ) = parseAddress ( ip ) <EOL> if parsedVersion != <NUM_LIT:4> : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> ( last , lastversion ) = parseAddress ( last ) <EOL> if lastversion != <NUM_LIT:4> : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> if last < self . ip : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> size = last - self . ip <EOL> netbits = _count1Bits ( size ) <EOL> if IP ( '<STR_LIT>' % ( ip , <NUM_LIT:32> - netbits ) ) . broadcast ( ) . int ( ) != last : <EOL> raise ValueError ( "<STR_LIT>" % data ) <EOL> elif len ( x ) == <NUM_LIT:1> : <EOL> x = data . split ( '<STR_LIT:/>' ) <EOL> if len ( x ) == <NUM_LIT:1> : <EOL> ip = x [ <NUM_LIT:0> ] <EOL> prefixlen = - <NUM_LIT:1> <EOL> elif len ( x ) > <NUM_LIT:2> : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> else : <EOL> ( ip , prefixlen ) = x <EOL> if prefixlen . find ( '<STR_LIT:.>' ) != - <NUM_LIT:1> : <EOL> ( netmask , vers ) = parseAddress ( prefixlen ) <EOL> if vers != <NUM_LIT:4> : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> prefixlen = _netmaskToPrefixlen ( netmask ) <EOL> elif len ( x ) > <NUM_LIT:2> : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> else : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> ( self . ip , parsedVersion ) = parseAddress ( ip ) <EOL> if ipversion == <NUM_LIT:0> : <EOL> ipversion = parsedVersion <EOL> if prefixlen == - <NUM_LIT:1> : <EOL> if ipversion == <NUM_LIT:4> : <EOL> prefixlen = <NUM_LIT:32> - netbits <EOL> elif ipversion == <NUM_LIT:6> : <EOL> prefixlen = <NUM_LIT> - netbits <EOL> else : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> self . _ipversion = ipversion <EOL> self . _prefixlen = int ( prefixlen ) <EOL> if make_net : <EOL> self . ip = self . ip & _prefixlenToNetmask ( self . _prefixlen , self . _ipversion ) <EOL> if not _checkNetaddrWorksWithPrefixlen ( self . ip , <EOL> self . _prefixlen , self . _ipversion ) : <EOL> raise ValueError ( "<STR_LIT>" % ( repr ( self ) , self . _prefixlen ) ) <EOL> else : <EOL> raise TypeError ( "<STR_LIT>" % type ( data ) ) <EOL> def int ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . ip <EOL> def version ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _ipversion <EOL> def prefixlen ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _prefixlen <EOL> def net ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . int ( ) <EOL> def broadcast ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . int ( ) + self . len ( ) - <NUM_LIT:1> <EOL> def _printPrefix ( self , want ) : <EOL> """<STR_LIT>""" <EOL> if ( self . _ipversion == <NUM_LIT:4> and self . _prefixlen == <NUM_LIT:32> ) or ( self . _ipversion == <NUM_LIT:6> and self . _prefixlen == <NUM_LIT> ) : <EOL> if self . NoPrefixForSingleIp : <EOL> want = <NUM_LIT:0> <EOL> if want == None : <EOL> want = self . WantPrefixLen <EOL> if want == None : <EOL> want = <NUM_LIT:1> <EOL> if want : <EOL> if want == <NUM_LIT:2> : <EOL> netmask = self . netmask ( ) <EOL> if not isinstance ( netmask , ( int , long ) ) : <EOL> netmask = netmask . int ( ) <EOL> return "<STR_LIT>" % ( intToIp ( netmask , self . _ipversion ) ) <EOL> elif want == <NUM_LIT:3> : <EOL> return "<STR_LIT>" % ( intToIp ( self . ip + self . len ( ) - <NUM_LIT:1> , self . _ipversion ) ) <EOL> else : <EOL> return "<STR_LIT>" % ( self . _prefixlen ) <EOL> else : <EOL> return '<STR_LIT>' <EOL> def strBin ( self , wantprefixlen = None ) : <EOL> """<STR_LIT>""" <EOL> if self . _ipversion == <NUM_LIT:4> : <EOL> bits = <NUM_LIT:32> <EOL> elif self . _ipversion == <NUM_LIT:6> : <EOL> bits = <NUM_LIT> <EOL> else : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> if self . WantPrefixLen == None and wantprefixlen == None : <EOL> wantprefixlen = <NUM_LIT:0> <EOL> ret = _intToBin ( self . ip ) <EOL> return '<STR_LIT:0>' * ( bits - len ( ret ) ) + ret + self . _printPrefix ( wantprefixlen ) <EOL> def strCompressed ( self , wantprefixlen = None ) : <EOL> """<STR_LIT>""" <EOL> if self . WantPrefixLen == None and wantprefixlen == None : <EOL> wantprefixlen = <NUM_LIT:1> <EOL> if self . _ipversion == <NUM_LIT:4> : <EOL> return self . strFullsize ( wantprefixlen ) <EOL> else : <EOL> if self . ip >> <NUM_LIT:32> == <NUM_LIT> : <EOL> ipv4 = intToIp ( self . ip & <NUM_LIT> , <NUM_LIT:4> ) <EOL> text = "<STR_LIT>" + ipv4 + self . _printPrefix ( wantprefixlen ) <EOL> return text <EOL> hextets = [ int ( x , <NUM_LIT:16> ) for x in self . strFullsize ( <NUM_LIT:0> ) . split ( '<STR_LIT::>' ) ] <EOL> followingzeros = [ <NUM_LIT:0> ] * <NUM_LIT:8> <EOL> for i in xrange ( len ( hextets ) ) : <EOL> followingzeros [ i ] = _countFollowingZeros ( hextets [ i : ] ) <EOL> compressionpos = followingzeros . index ( max ( followingzeros ) ) <EOL> if max ( followingzeros ) > <NUM_LIT:1> : <EOL> hextets = [ x for x in self . strNormal ( <NUM_LIT:0> ) . split ( '<STR_LIT::>' ) ] <EOL> while compressionpos < len ( hextets ) and hextets [ compressionpos ] == '<STR_LIT:0>' : <EOL> del ( hextets [ compressionpos ] ) <EOL> hextets . insert ( compressionpos , '<STR_LIT>' ) <EOL> if compressionpos + <NUM_LIT:1> >= len ( hextets ) : <EOL> hextets . append ( '<STR_LIT>' ) <EOL> if compressionpos == <NUM_LIT:0> : <EOL> hextets = [ '<STR_LIT>' ] + hextets <EOL> return '<STR_LIT::>' . join ( hextets ) + self . _printPrefix ( wantprefixlen ) <EOL> else : <EOL> return self . strNormal ( <NUM_LIT:0> ) + self . _printPrefix ( wantprefixlen ) <EOL> def strNormal ( self , wantprefixlen = None ) : <EOL> """<STR_LIT>""" <EOL> if self . WantPrefixLen == None and wantprefixlen == None : <EOL> wantprefixlen = <NUM_LIT:1> <EOL> if self . _ipversion == <NUM_LIT:4> : <EOL> ret = self . strFullsize ( <NUM_LIT:0> ) <EOL> elif self . _ipversion == <NUM_LIT:6> : <EOL> ret = '<STR_LIT::>' . join ( [ hex ( x ) [ <NUM_LIT:2> : ] for x in [ int ( x , <NUM_LIT:16> ) for x in self . strFullsize ( <NUM_LIT:0> ) . split ( '<STR_LIT::>' ) ] ] ) <EOL> else : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> return ret + self . _printPrefix ( wantprefixlen ) <EOL> def strFullsize ( self , wantprefixlen = None ) : <EOL> """<STR_LIT>""" <EOL> if self . WantPrefixLen == None and wantprefixlen == None : <EOL> wantprefixlen = <NUM_LIT:1> <EOL> return intToIp ( self . ip , self . _ipversion ) . lower ( ) + self . _printPrefix ( wantprefixlen ) <EOL> def strHex ( self , wantprefixlen = None ) : <EOL> """<STR_LIT>""" <EOL> if self . WantPrefixLen == None and wantprefixlen == None : <EOL> wantprefixlen = <NUM_LIT:0> <EOL> x = hex ( self . ip ) <EOL> if x [ - <NUM_LIT:1> ] == '<STR_LIT:L>' : <EOL> x = x [ : - <NUM_LIT:1> ] <EOL> return x . lower ( ) + self . _printPrefix ( wantprefixlen ) <EOL> def strDec ( self , wantprefixlen = None ) : <EOL> """<STR_LIT>""" <EOL> if self . WantPrefixLen == None and wantprefixlen == None : <EOL> wantprefixlen = <NUM_LIT:0> <EOL> x = str ( self . ip ) <EOL> if x [ - <NUM_LIT:1> ] == '<STR_LIT:L>' : <EOL> x = x [ : - <NUM_LIT:1> ] <EOL> return x + self . _printPrefix ( wantprefixlen ) <EOL> def iptype ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . _ipversion == <NUM_LIT:4> : <EOL> iprange = IPv4ranges <EOL> elif self . _ipversion == <NUM_LIT:6> : <EOL> iprange = IPv6ranges <EOL> else : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> bits = self . strBin ( ) <EOL> for i in xrange ( len ( bits ) , <NUM_LIT:0> , - <NUM_LIT:1> ) : <EOL> if bits [ : i ] in iprange : <EOL> return iprange [ bits [ : i ] ] <EOL> return "<STR_LIT>" <EOL> def netmask ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . _ipversion == <NUM_LIT:4> : <EOL> locallen = <NUM_LIT:32> - self . _prefixlen <EOL> elif self . _ipversion == <NUM_LIT:6> : <EOL> locallen = <NUM_LIT> - self . _prefixlen <EOL> else : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> return ( ( <NUM_LIT:2> ** self . _prefixlen ) - <NUM_LIT:1> ) << locallen <EOL> def strNetmask ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . _ipversion == <NUM_LIT:4> : <EOL> locallen = <NUM_LIT:32> - self . _prefixlen <EOL> return intToIp ( ( ( <NUM_LIT:2> ** self . _prefixlen ) - <NUM_LIT:1> ) << locallen , <NUM_LIT:4> ) <EOL> elif self . _ipversion == <NUM_LIT:6> : <EOL> locallen = <NUM_LIT> - self . _prefixlen <EOL> return "<STR_LIT>" % self . _prefixlen <EOL> else : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> def len ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . _ipversion == <NUM_LIT:4> : <EOL> locallen = <NUM_LIT:32> - self . _prefixlen <EOL> elif self . _ipversion == <NUM_LIT:6> : <EOL> locallen = <NUM_LIT> - self . _prefixlen <EOL> else : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> return <NUM_LIT:2> ** locallen <EOL> def __nonzero__ ( self ) : <EOL> """<STR_LIT>""" <EOL> return True <EOL> def __len__ ( self ) : <EOL> """<STR_LIT>""" <EOL> return int ( self . len ( ) ) <EOL> def __getitem__ ( self , key ) : <EOL> """<STR_LIT>""" <EOL> if not isinstance ( key , ( int , long ) ) : <EOL> raise TypeError <EOL> if key < <NUM_LIT:0> : <EOL> if abs ( key ) <= self . len ( ) : <EOL> key = self . len ( ) - abs ( key ) <EOL> else : <EOL> raise IndexError <EOL> else : <EOL> if key >= self . len ( ) : <EOL> raise IndexError <EOL> return self . ip + long ( key ) <EOL> def __contains__ ( self , item ) : <EOL> """<STR_LIT>""" <EOL> item = IP ( item ) <EOL> if item . ip >= self . ip and item . ip < self . ip + self . len ( ) - item . len ( ) + <NUM_LIT:1> : <EOL> return True <EOL> else : <EOL> return False <EOL> def overlaps ( self , item ) : <EOL> """<STR_LIT>""" <EOL> item = IP ( item ) <EOL> if item . ip >= self . ip and item . ip < self . ip + self . len ( ) : <EOL> return <NUM_LIT:1> <EOL> elif self . ip >= item . ip and self . ip < item . ip + item . len ( ) : <EOL> return - <NUM_LIT:1> <EOL> else : <EOL> return <NUM_LIT:0> <EOL> def __str__ ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . strCompressed ( ) <EOL> def __repr__ ( self ) : <EOL> """<STR_LIT>""" <EOL> return ( "<STR_LIT>" % ( self . strCompressed ( <NUM_LIT:1> ) ) ) <EOL> def __cmp__ ( self , other ) : <EOL> """<STR_LIT>""" <EOL> if self . _prefixlen < other . prefixlen ( ) : <EOL> return ( other . prefixlen ( ) - self . _prefixlen ) <EOL> elif self . _prefixlen > other . prefixlen ( ) : <EOL> return other . prefixlen ( ) - self . _prefixlen <EOL> else : <EOL> if self . ip < other . ip : <EOL> return - <NUM_LIT:1> <EOL> elif self . ip > other . ip : <EOL> return <NUM_LIT:1> <EOL> elif self . _ipversion != other . _ipversion : <EOL> if self . _ipversion < other . _ipversion : <EOL> return - <NUM_LIT:1> <EOL> elif self . _ipversion > other . _ipversion : <EOL> return <NUM_LIT:1> <EOL> else : <EOL> return <NUM_LIT:0> <EOL> else : <EOL> return <NUM_LIT:0> <EOL> def __eq__ ( self , other ) : <EOL> return self . __cmp__ ( other ) == <NUM_LIT:0> <EOL> def __lt__ ( self , other ) : <EOL> return self . __cmp__ ( other ) < <NUM_LIT:0> <EOL> def __hash__ ( self ) : <EOL> """<STR_LIT>""" <EOL> thehash = int ( - <NUM_LIT:1> ) <EOL> ip = self . ip <EOL> while ip > <NUM_LIT:0> : <EOL> thehash = thehash ^ ( ip & <NUM_LIT> ) <EOL> ip = ip >> <NUM_LIT:32> <EOL> thehash = thehash ^ self . _prefixlen <EOL> return int ( thehash ) <EOL> class IP ( IPint ) : <EOL> """<STR_LIT>""" <EOL> def __json__ ( self ) : <EOL> return str ( self ) <EOL> def net ( self ) : <EOL> """<STR_LIT>""" <EOL> return IP ( IPint . net ( self ) , ipversion = self . _ipversion ) <EOL> def broadcast ( self ) : <EOL> """<STR_LIT>""" <EOL> return IP ( IPint . broadcast ( self ) ) <EOL> def netmask ( self ) : <EOL> """<STR_LIT>""" <EOL> return IP ( IPint . netmask ( self ) , ipversion = self . _ipversion ) <EOL> def _getIPv4Map ( self ) : <EOL> if self . _ipversion != <NUM_LIT:6> : <EOL> return None <EOL> if ( self . ip >> <NUM_LIT:32> ) != <NUM_LIT> : <EOL> return None <EOL> ipv4 = self . ip & <NUM_LIT> <EOL> if self . _prefixlen != <NUM_LIT> : <EOL> ipv4 = '<STR_LIT>' % ( ipv4 , <NUM_LIT:32> - ( <NUM_LIT> - self . _prefixlen ) ) <EOL> return IP ( ipv4 , ipversion = <NUM_LIT:4> ) <EOL> def reverseNames ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . _ipversion == <NUM_LIT:4> : <EOL> ret = [ ] <EOL> if self . len ( ) < <NUM_LIT:2> ** <NUM_LIT:8> : <EOL> for x in self : <EOL> ret . append ( x . reverseName ( ) ) <EOL> elif self . len ( ) < <NUM_LIT:2> ** <NUM_LIT:16> : <EOL> for i in xrange ( <NUM_LIT:0> , self . len ( ) , <NUM_LIT:2> ** <NUM_LIT:8> ) : <EOL> ret . append ( self [ i ] . reverseName ( ) [ <NUM_LIT:2> : ] ) <EOL> elif self . len ( ) < <NUM_LIT:2> ** <NUM_LIT> : <EOL> for i in xrange ( <NUM_LIT:0> , self . len ( ) , <NUM_LIT:2> ** <NUM_LIT:16> ) : <EOL> ret . append ( self [ i ] . reverseName ( ) [ <NUM_LIT:4> : ] ) <EOL> else : <EOL> for i in xrange ( <NUM_LIT:0> , self . len ( ) , <NUM_LIT:2> ** <NUM_LIT> ) : <EOL> ret . append ( self [ i ] . reverseName ( ) [ <NUM_LIT:6> : ] ) <EOL> return ret <EOL> elif self . _ipversion == <NUM_LIT:6> : <EOL> ipv4 = self . _getIPv4Map ( ) <EOL> if ipv4 is not None : <EOL> return ipv4 . reverseNames ( ) <EOL> s = hex ( self . ip ) [ <NUM_LIT:2> : ] . lower ( ) <EOL> if s [ - <NUM_LIT:1> ] == '<STR_LIT:l>' : <EOL> s = s [ : - <NUM_LIT:1> ] <EOL> if self . _prefixlen % <NUM_LIT:4> != <NUM_LIT:0> : <EOL> raise NotImplementedError ( "<STR_LIT>" ) <EOL> s = list ( s ) <EOL> s . reverse ( ) <EOL> s = '<STR_LIT:.>' . join ( s ) <EOL> first_nibble_index = int ( <NUM_LIT:32> - ( self . _prefixlen // <NUM_LIT:4> ) ) * <NUM_LIT:2> <EOL> return [ "<STR_LIT>" % s [ first_nibble_index : ] ] <EOL> else : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> def reverseName ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . _ipversion == <NUM_LIT:4> : <EOL> s = self . strFullsize ( <NUM_LIT:0> ) <EOL> s = s . split ( '<STR_LIT:.>' ) <EOL> s . reverse ( ) <EOL> first_byte_index = int ( <NUM_LIT:4> - ( self . _prefixlen // <NUM_LIT:8> ) ) <EOL> if self . _prefixlen % <NUM_LIT:8> != <NUM_LIT:0> : <EOL> nibblepart = "<STR_LIT>" % ( s [ <NUM_LIT:3> - ( self . _prefixlen // <NUM_LIT:8> ) ] , intToIp ( self . ip + self . len ( ) - <NUM_LIT:1> , <NUM_LIT:4> ) . split ( '<STR_LIT:.>' ) [ - <NUM_LIT:1> ] ) <EOL> if nibblepart [ - <NUM_LIT:1> ] == '<STR_LIT:l>' : <EOL> nibblepart = nibblepart [ : - <NUM_LIT:1> ] <EOL> nibblepart += '<STR_LIT:.>' <EOL> else : <EOL> nibblepart = "<STR_LIT>" <EOL> s = '<STR_LIT:.>' . join ( s [ first_byte_index : ] ) <EOL> return "<STR_LIT>" % ( nibblepart , s ) <EOL> elif self . _ipversion == <NUM_LIT:6> : <EOL> ipv4 = self . _getIPv4Map ( ) <EOL> if ipv4 is not None : <EOL> return ipv4 . reverseName ( ) <EOL> s = hex ( self . ip ) [ <NUM_LIT:2> : ] . lower ( ) <EOL> if s [ - <NUM_LIT:1> ] == '<STR_LIT:l>' : <EOL> s = s [ : - <NUM_LIT:1> ] <EOL> if self . _prefixlen % <NUM_LIT:4> != <NUM_LIT:0> : <EOL> nibblepart = "<STR_LIT>" % ( s [ self . _prefixlen : ] , hex ( self . ip + self . len ( ) - <NUM_LIT:1> ) [ <NUM_LIT:2> : ] . lower ( ) ) <EOL> if nibblepart [ - <NUM_LIT:1> ] == '<STR_LIT:l>' : <EOL> nibblepart = nibblepart [ : - <NUM_LIT:1> ] <EOL> nibblepart += '<STR_LIT:.>' <EOL> else : <EOL> nibblepart = "<STR_LIT>" <EOL> s = list ( s ) <EOL> s . reverse ( ) <EOL> s = '<STR_LIT:.>' . join ( s ) <EOL> first_nibble_index = int ( <NUM_LIT:32> - ( self . _prefixlen // <NUM_LIT:4> ) ) * <NUM_LIT:2> <EOL> return "<STR_LIT>" % ( nibblepart , s [ first_nibble_index : ] ) <EOL> else : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> def make_net ( self , netmask ) : <EOL> """<STR_LIT>""" <EOL> if '<STR_LIT:/>' in str ( netmask ) : <EOL> raise ValueError ( "<STR_LIT>" % netmask ) <EOL> return IP ( '<STR_LIT>' % ( self , netmask ) , make_net = True ) <EOL> def __getitem__ ( self , key ) : <EOL> """<STR_LIT>""" <EOL> return IP ( IPint . __getitem__ ( self , key ) ) <EOL> def __repr__ ( self ) : <EOL> """<STR_LIT>""" <EOL> return ( "<STR_LIT>" % ( self . strCompressed ( <NUM_LIT:1> ) ) ) <EOL> def __add__ ( self , other ) : <EOL> """<STR_LIT>""" <EOL> if self . prefixlen ( ) != other . prefixlen ( ) : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> if self . prefixlen ( ) < <NUM_LIT:1> : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> if self . version ( ) != other . version ( ) : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> if self > other : <EOL> return other . __add__ ( self ) <EOL> else : <EOL> ret = IP ( self . int ( ) ) <EOL> ret . _prefixlen = self . prefixlen ( ) - <NUM_LIT:1> <EOL> return ret <EOL> def _parseAddressIPv6 ( ipstr ) : <EOL> """<STR_LIT>""" <EOL> items = [ ] <EOL> index = <NUM_LIT:0> <EOL> fill_pos = None <EOL> while index < len ( ipstr ) : <EOL> text = ipstr [ index : ] <EOL> if text . startswith ( "<STR_LIT>" ) : <EOL> if fill_pos is not None : <EOL> raise ValueError ( "<STR_LIT>" % ipstr ) <EOL> fill_pos = len ( items ) <EOL> index += <NUM_LIT:2> <EOL> continue <EOL> pos = text . find ( '<STR_LIT::>' ) <EOL> if pos == <NUM_LIT:0> : <EOL> raise ValueError ( "<STR_LIT>" % ipstr ) <EOL> if pos != - <NUM_LIT:1> : <EOL> items . append ( text [ : pos ] ) <EOL> if text [ pos : pos + <NUM_LIT:2> ] == "<STR_LIT>" : <EOL> index += pos <EOL> else : <EOL> index += pos + <NUM_LIT:1> <EOL> if index == len ( ipstr ) : <EOL> raise ValueError ( "<STR_LIT>" % ipstr ) <EOL> else : <EOL> items . append ( text ) <EOL> break <EOL> if items and '<STR_LIT:.>' in items [ - <NUM_LIT:1> ] : <EOL> if ( fill_pos is not None ) and not ( fill_pos <= len ( items ) - <NUM_LIT:1> ) : <EOL> raise ValueError ( "<STR_LIT>" % ipstr ) <EOL> value = parseAddress ( items [ - <NUM_LIT:1> ] ) [ <NUM_LIT:0> ] <EOL> items = items [ : - <NUM_LIT:1> ] + [ "<STR_LIT>" % ( value >> <NUM_LIT:16> ) , "<STR_LIT>" % ( value & <NUM_LIT> ) ] <EOL> if fill_pos is not None : <EOL> diff = <NUM_LIT:8> - len ( items ) <EOL> if diff <= <NUM_LIT:0> : <EOL> raise ValueError ( "<STR_LIT>" % ipstr ) <EOL> items = items [ : fill_pos ] + [ '<STR_LIT:0>' ] * diff + items [ fill_pos : ] <EOL> if len ( items ) != <NUM_LIT:8> : <EOL> raise ValueError ( "<STR_LIT>" % ipstr ) <EOL> value = <NUM_LIT:0> <EOL> index = <NUM_LIT:0> <EOL> for item in items : <EOL> try : <EOL> item = int ( item , <NUM_LIT:16> ) <EOL> error = not ( <NUM_LIT:0> <= item <= <NUM_LIT> ) <EOL> except ValueError : <EOL> error = True <EOL> if error : <EOL> raise ValueError ( "<STR_LIT>" % ( ipstr , item ) ) <EOL> value = ( value << <NUM_LIT:16> ) + item <EOL> index += <NUM_LIT:1> <EOL> return value <EOL> def parseAddress ( ipstr ) : <EOL> """<STR_LIT>""" <EOL> if ipstr . startswith ( '<STR_LIT>' ) : <EOL> ret = long ( ipstr [ <NUM_LIT:2> : ] , <NUM_LIT:16> ) <EOL> if ret > <NUM_LIT> : <EOL> raise ValueError ( "<STR_LIT>" % ( ipstr ) ) <EOL> if ret < <NUM_LIT> : <EOL> return ( ret , <NUM_LIT:4> ) <EOL> else : <EOL> return ( ret , <NUM_LIT:6> ) <EOL> if ipstr . find ( '<STR_LIT::>' ) != - <NUM_LIT:1> : <EOL> return ( _parseAddressIPv6 ( ipstr ) , <NUM_LIT:6> ) <EOL> elif len ( ipstr ) == <NUM_LIT:32> : <EOL> return ( long ( ipstr , <NUM_LIT:16> ) , <NUM_LIT:6> ) <EOL> elif ipstr . find ( '<STR_LIT:.>' ) != - <NUM_LIT:1> or ( len ( ipstr ) < <NUM_LIT:4> and int ( ipstr ) < <NUM_LIT> ) : <EOL> bytes = ipstr . split ( '<STR_LIT:.>' ) <EOL> if len ( bytes ) > <NUM_LIT:4> : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> bytes += [ '<STR_LIT:0>' ] * ( <NUM_LIT:4> - len ( bytes ) ) <EOL> bytes = [ long ( x ) for x in bytes ] <EOL> for x in bytes : <EOL> if x > <NUM_LIT:255> or x < <NUM_LIT:0> : <EOL> raise ValueError ( "<STR_LIT>" % ( ipstr ) ) <EOL> return ( ( bytes [ <NUM_LIT:0> ] << <NUM_LIT> ) + ( bytes [ <NUM_LIT:1> ] << <NUM_LIT:16> ) + ( bytes [ <NUM_LIT:2> ] << <NUM_LIT:8> ) + bytes [ <NUM_LIT:3> ] , <NUM_LIT:4> ) <EOL> else : <EOL> ret = long ( ipstr , <NUM_LIT:10> ) <EOL> if ret > <NUM_LIT> : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> if ret <= <NUM_LIT> : <EOL> return ( ret , <NUM_LIT:4> ) <EOL> else : <EOL> return ( ret , <NUM_LIT:6> ) <EOL> def intToIp ( ip , version ) : <EOL> """<STR_LIT>""" <EOL> ip = long ( ip ) <EOL> if ip < <NUM_LIT:0> : <EOL> raise ValueError ( "<STR_LIT>" % ( ip ) ) <EOL> ret = '<STR_LIT>' <EOL> if version == <NUM_LIT:4> : <EOL> if ip > <NUM_LIT> : <EOL> raise ValueError ( "<STR_LIT>" % ( hex ( ip ) ) ) <EOL> for l in xrange ( <NUM_LIT:4> ) : <EOL> ret = str ( ip & <NUM_LIT> ) + '<STR_LIT:.>' + ret <EOL> ip = ip >> <NUM_LIT:8> <EOL> ret = ret [ : - <NUM_LIT:1> ] <EOL> elif version == <NUM_LIT:6> : <EOL> if ip > <NUM_LIT> : <EOL> raise ValueError ( "<STR_LIT>" % ( hex ( ip ) ) ) <EOL> if sys . hexversion >= <NUM_LIT> : <EOL> l = hex ( ip ) [ <NUM_LIT:2> : ] <EOL> else : <EOL> l = hex ( ip ) [ <NUM_LIT:2> : - <NUM_LIT:1> ] <EOL> l = l . zfill ( <NUM_LIT:32> ) <EOL> for x in xrange ( <NUM_LIT:1> , <NUM_LIT> ) : <EOL> ret = l [ - x ] + ret <EOL> if x % <NUM_LIT:4> == <NUM_LIT:0> : <EOL> ret = '<STR_LIT::>' + ret <EOL> ret = ret [ <NUM_LIT:1> : ] <EOL> else : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> return ret <EOL> def _ipVersionToLen ( version ) : <EOL> """<STR_LIT>""" <EOL> if version == <NUM_LIT:4> : <EOL> return <NUM_LIT:32> <EOL> elif version == <NUM_LIT:6> : <EOL> return <NUM_LIT> <EOL> else : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> def _countFollowingZeros ( l ) : <EOL> """<STR_LIT>""" <EOL> if len ( l ) == <NUM_LIT:0> : <EOL> return <NUM_LIT:0> <EOL> elif l [ <NUM_LIT:0> ] != <NUM_LIT:0> : <EOL> return <NUM_LIT:0> <EOL> else : <EOL> return <NUM_LIT:1> + _countFollowingZeros ( l [ <NUM_LIT:1> : ] ) <EOL> _BitTable = { '<STR_LIT:0>' : '<STR_LIT>' , '<STR_LIT:1>' : '<STR_LIT>' , '<STR_LIT:2>' : '<STR_LIT>' , '<STR_LIT:3>' : '<STR_LIT>' , <EOL> '<STR_LIT:4>' : '<STR_LIT>' , '<STR_LIT:5>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT:a>' : '<STR_LIT>' , '<STR_LIT:b>' : '<STR_LIT>' , <EOL> '<STR_LIT:c>' : '<STR_LIT>' , '<STR_LIT:d>' : '<STR_LIT>' , '<STR_LIT:e>' : '<STR_LIT>' , '<STR_LIT:f>' : '<STR_LIT>' } <EOL> def _intToBin ( val ) : <EOL> """<STR_LIT>""" <EOL> if val < <NUM_LIT:0> : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> s = hex ( val ) . lower ( ) <EOL> ret = '<STR_LIT>' <EOL> if s [ - <NUM_LIT:1> ] == '<STR_LIT:l>' : <EOL> s = s [ : - <NUM_LIT:1> ] <EOL> for x in s [ <NUM_LIT:2> : ] : <EOL> ret += _BitTable [ x ] <EOL> while ret [ <NUM_LIT:0> ] == '<STR_LIT:0>' and len ( ret ) > <NUM_LIT:1> : <EOL> ret = ret [ <NUM_LIT:1> : ] <EOL> return ret <EOL> def _count1Bits ( num ) : <EOL> """<STR_LIT>""" <EOL> ret = <NUM_LIT:0> <EOL> while num > <NUM_LIT:0> : <EOL> num = num >> <NUM_LIT:1> <EOL> ret += <NUM_LIT:1> <EOL> return ret <EOL> def _count0Bits ( num ) : <EOL> """<STR_LIT>""" <EOL> num = long ( num ) <EOL> if num < <NUM_LIT:0> : <EOL> raise ValueError ( "<STR_LIT>" % ( num ) ) <EOL> ret = <NUM_LIT:0> <EOL> while num > <NUM_LIT:0> : <EOL> if num & <NUM_LIT:1> == <NUM_LIT:1> : <EOL> break <EOL> num = num >> <NUM_LIT:1> <EOL> ret += <NUM_LIT:1> <EOL> return ret <EOL> def _checkPrefix ( ip , prefixlen , version ) : <EOL> """<STR_LIT>""" <EOL> bits = _ipVersionToLen ( version ) <EOL> if prefixlen < <NUM_LIT:0> or prefixlen > bits : <EOL> return None <EOL> if ip == <NUM_LIT:0> : <EOL> zbits = bits + <NUM_LIT:1> <EOL> else : <EOL> zbits = _count0Bits ( ip ) <EOL> if zbits < bits - prefixlen : <EOL> return <NUM_LIT:0> <EOL> else : <EOL> return <NUM_LIT:1> <EOL> def _checkNetmask ( netmask , masklen ) : <EOL> """<STR_LIT>""" <EOL> num = long ( netmask ) <EOL> bits = masklen <EOL> while ( num & <NUM_LIT:1> ) == <NUM_LIT:0> and bits != <NUM_LIT:0> : <EOL> num = num >> <NUM_LIT:1> <EOL> bits -= <NUM_LIT:1> <EOL> if bits == <NUM_LIT:0> : <EOL> break <EOL> while bits > <NUM_LIT:0> : <EOL> if ( num & <NUM_LIT:1> ) == <NUM_LIT:0> : <EOL> raise ValueError ( "<STR_LIT>" % ( hex ( netmask ) ) ) <EOL> num = num >> <NUM_LIT:1> <EOL> bits -= <NUM_LIT:1> <EOL> def _checkNetaddrWorksWithPrefixlen ( net , prefixlen , version ) : <EOL> """<STR_LIT>""" <EOL> return ( net & _prefixlenToNetmask ( prefixlen , version ) == net ) <EOL> def _netmaskToPrefixlen ( netmask ) : <EOL> """<STR_LIT>""" <EOL> netlen = _count0Bits ( netmask ) <EOL> masklen = _count1Bits ( netmask ) <EOL> _checkNetmask ( netmask , masklen ) <EOL> return masklen - netlen <EOL> def _prefixlenToNetmask ( prefixlen , version ) : <EOL> """<STR_LIT>""" <EOL> if prefixlen == <NUM_LIT:0> : <EOL> return <NUM_LIT:0> <EOL> elif prefixlen < <NUM_LIT:0> : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> return ( ( <NUM_LIT:2> << prefixlen - <NUM_LIT:1> ) - <NUM_LIT:1> ) << ( _ipVersionToLen ( version ) - prefixlen ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> import doctest <EOL> failure , nbtest = doctest . testmod ( ) <EOL> if failure : <EOL> import sys <EOL> sys . exit ( <NUM_LIT:1> ) </s>
