<s> import socket <EOL> import response <EOL> from http_parser . parser import HttpParser <EOL> import thread <EOL> def start ( routes , host = "<STR_LIT:127.0.0.1>" , port = <NUM_LIT> , threads = <NUM_LIT:5> ) : <EOL> sock = socket . socket ( ) <EOL> sock . setsockopt ( socket . SOL_SOCKET , socket . SO_REUSEADDR , <NUM_LIT:1> ) <EOL> sock . bind ( ( host , port ) ) <EOL> sock . listen ( <NUM_LIT:5> ) <EOL> for t in range ( threads ) : <EOL> print "<STR_LIT>" . format ( t ) <EOL> thread . start_new_thread ( thread_func , ( sock , routes ) ) <EOL> print "<STR_LIT>" . format ( host , port ) <EOL> while True : pass <EOL> def thread_func ( sock , routes ) : <EOL> for request in http ( sock ) : <EOL> request . router ( routes ) <EOL> def http ( sock ) : <EOL> while True : <EOL> obj , conn = sock . accept ( ) <EOL> body = [ ] <EOL> p = HttpParser ( ) <EOL> while True : <EOL> data = obj . recv ( <NUM_LIT> ) <EOL> if not data : <EOL> break <EOL> recved = len ( data ) <EOL> nparsed = p . execute ( data , recved ) <EOL> assert nparsed == recved <EOL> if p . is_partial_body ( ) : <EOL> body . append ( p . recv_body ( ) ) <EOL> if p . is_message_complete ( ) : <EOL> break <EOL> yield response . Response ( obj , p , '<STR_LIT>' . join ( body ) , conn [ <NUM_LIT:0> ] ) </s>
