<s> from Queue import Queue <EOL> import struct <EOL> import threading <EOL> from SocketServer import TCPServer , BaseRequestHandler , ThreadingMixIn <EOL> from . import RPCRequestResponseServer <EOL> def _read_length_prefixed_msg ( sock , prefix_format = '<STR_LIT>' ) : <EOL> prefix_bytes = struct . calcsize ( prefix_format ) <EOL> sock . recv ( prefix_bytes ) <EOL> def _read_n_bytes ( sock , n ) : <EOL> buf = [ ] <EOL> while n > <NUM_LIT:0> : <EOL> data = sock . recv ( n ) <EOL> n -= len ( data ) <EOL> buf . append ( data ) <EOL> return '<STR_LIT>' . join ( buf ) <EOL> def create_length_prefixed_tcp_handler ( ) : <EOL> queue = Queue ( ) <EOL> class LengthPrefixedTcpHandler ( BaseRequestHandler ) : <EOL> def handle ( self ) : <EOL> self . request . send ( "<STR_LIT>" ) <EOL> while True : <EOL> b = _read_n_bytes ( self . request , <NUM_LIT:10> ) <EOL> self . request . send ( "<STR_LIT>" % b ) <EOL> queue . put ( b ) <EOL> return queue , LengthPrefixedTcpHandler <EOL> def tcp_test_main ( ) : <EOL> class Server ( ThreadingMixIn , TCPServer ) : <EOL> pass <EOL> queue , Handler = create_length_prefixed_tcp_handler ( ) <EOL> server = Server ( ( '<STR_LIT:localhost>' , <NUM_LIT> ) , Handler ) <EOL> server . allow_reuse_address = True <EOL> server . serve_forever ( ) </s>
