<s> import socket <EOL> import struct <EOL> import dns . ipv4 <EOL> import dns . rdata <EOL> _proto_tcp = socket . getprotobyname ( '<STR_LIT>' ) <EOL> _proto_udp = socket . getprotobyname ( '<STR_LIT>' ) <EOL> class WKS ( dns . rdata . Rdata ) : <EOL> """<STR_LIT>""" <EOL> __slots__ = [ '<STR_LIT:address>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> def __init__ ( self , rdclass , rdtype , address , protocol , bitmap ) : <EOL> super ( WKS , self ) . __init__ ( rdclass , rdtype ) <EOL> self . address = address <EOL> self . protocol = protocol <EOL> self . bitmap = bitmap <EOL> def to_text ( self , origin = None , relativize = True , ** kw ) : <EOL> bits = [ ] <EOL> for i in xrange ( <NUM_LIT:0> , len ( self . bitmap ) ) : <EOL> byte = ord ( self . bitmap [ i ] ) <EOL> for j in xrange ( <NUM_LIT:0> , <NUM_LIT:8> ) : <EOL> if byte & ( <NUM_LIT> >> j ) : <EOL> bits . append ( str ( i * <NUM_LIT:8> + j ) ) <EOL> text = '<STR_LIT:U+0020>' . join ( bits ) <EOL> return '<STR_LIT>' % ( self . address , self . protocol , text ) <EOL> def from_text ( cls , rdclass , rdtype , tok , origin = None , relativize = True ) : <EOL> address = tok . get_string ( ) <EOL> protocol = tok . get_string ( ) <EOL> if protocol . isdigit ( ) : <EOL> protocol = int ( protocol ) <EOL> else : <EOL> protocol = socket . getprotobyname ( protocol ) <EOL> bitmap = [ ] <EOL> while <NUM_LIT:1> : <EOL> token = tok . get ( ) . unescape ( ) <EOL> if token . is_eol_or_eof ( ) : <EOL> break <EOL> if token . value . isdigit ( ) : <EOL> serv = int ( token . value ) <EOL> else : <EOL> if protocol != _proto_udp and protocol != _proto_tcp : <EOL> raise NotImplementedError ( "<STR_LIT>" ) <EOL> if protocol == _proto_udp : <EOL> protocol_text = "<STR_LIT>" <EOL> else : <EOL> protocol_text = "<STR_LIT>" <EOL> serv = socket . getservbyname ( token . value , protocol_text ) <EOL> i = serv // <NUM_LIT:8> <EOL> l = len ( bitmap ) <EOL> if l < i + <NUM_LIT:1> : <EOL> for j in xrange ( l , i + <NUM_LIT:1> ) : <EOL> bitmap . append ( '<STR_LIT:\x00>' ) <EOL> bitmap [ i ] = chr ( ord ( bitmap [ i ] ) | ( <NUM_LIT> >> ( serv % <NUM_LIT:8> ) ) ) <EOL> bitmap = dns . rdata . _truncate_bitmap ( bitmap ) <EOL> return cls ( rdclass , rdtype , address , protocol , bitmap ) <EOL> from_text = classmethod ( from_text ) <EOL> def to_wire ( self , file , compress = None , origin = None ) : <EOL> file . write ( dns . ipv4 . inet_aton ( self . address ) ) <EOL> protocol = struct . pack ( '<STR_LIT>' , self . protocol ) <EOL> file . write ( protocol ) <EOL> file . write ( self . bitmap ) <EOL> def from_wire ( cls , rdclass , rdtype , wire , current , rdlen , origin = None ) : <EOL> address = dns . ipv4 . inet_ntoa ( wire [ current : current + <NUM_LIT:4> ] ) <EOL> protocol , = struct . unpack ( '<STR_LIT>' , wire [ current + <NUM_LIT:4> : current + <NUM_LIT:5> ] ) <EOL> current += <NUM_LIT:5> <EOL> rdlen -= <NUM_LIT:5> <EOL> bitmap = wire [ current : current + rdlen ] . unwrap ( ) <EOL> return cls ( rdclass , rdtype , address , protocol , bitmap ) <EOL> from_wire = classmethod ( from_wire ) </s>
