<s> """<STR_LIT>""" <EOL> import asyncore <EOL> import logging <EOL> import socket <EOL> import threading <EOL> import urlparse <EOL> import weakref <EOL> from wsgiref import headers <EOL> class CaseInsensitiveDict ( dict ) : <EOL> """<STR_LIT>""" <EOL> def __setitem__ ( self , key , value ) : <EOL> dict . __setitem__ ( self , key . lower ( ) , value ) <EOL> def __getitem__ ( self , key ) : <EOL> return dict . __getitem__ ( self , key . lower ( ) ) <EOL> class Request ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , request , remote_addr ) : <EOL> self . remote_addr = remote_addr <EOL> lines = request . split ( '<STR_LIT:\r\n>' ) <EOL> verb_line = lines . pop ( <NUM_LIT:0> ) <EOL> method , path , unused_protocol = verb_line . split ( '<STR_LIT:U+0020>' , <NUM_LIT:2> ) <EOL> self . headers = CaseInsensitiveDict ( ) <EOL> for line in lines : <EOL> key , value = line . split ( '<STR_LIT>' , <NUM_LIT:1> ) <EOL> self . headers [ key ] = value <EOL> self . url = '<STR_LIT>' % ( self . headers [ '<STR_LIT:host>' ] , path . lstrip ( '<STR_LIT:/>' ) ) <EOL> self . method = method . upper ( ) <EOL> self . path = path <EOL> self . query_string = '<STR_LIT>' <EOL> if '<STR_LIT:?>' in self . path : <EOL> self . path , self . query_string = self . path . split ( '<STR_LIT:?>' , <NUM_LIT:1> ) <EOL> self . _qs = urlparse . parse_qs ( self . query_string ) <EOL> def get ( self , argument_name , default_value = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> return self . _qs . get ( argument_name , [ default_value ] ) [ <NUM_LIT:0> ] <EOL> def get_all ( self , argument_name ) : <EOL> """<STR_LIT>""" <EOL> return self . _qs . get ( argument_name , [ ] ) <EOL> class OutFile ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , connection ) : <EOL> self . _connection = connection <EOL> def write ( self , value ) : <EOL> try : <EOL> self . _connection . send ( '<STR_LIT>' % len ( value ) ) <EOL> self . _connection . send ( value ) <EOL> logging . debug ( '<STR_LIT>' , len ( value ) ) <EOL> self . _connection . send ( '<STR_LIT:\r\n>' ) <EOL> except IOError : <EOL> self . _connection . close ( ) <EOL> class Response ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , connection ) : <EOL> self . _connection = connection <EOL> self . headers = headers . Headers ( [ ( '<STR_LIT>' , '<STR_LIT>' ) ] ) <EOL> self . out = OutFile ( self . _connection ) <EOL> def start_response ( self , code ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> self . _connection . send ( '<STR_LIT>' % <EOL> ( code , ResponseCode . GetReasonPhrase ( code ) ) ) <EOL> self . _connection . send ( str ( self . headers ) ) <EOL> except IOError : <EOL> self . _connection . close ( ) <EOL> def end_response ( self ) : <EOL> """<STR_LIT>""" <EOL> self . out . write ( '<STR_LIT>' ) <EOL> self . _connection . ResponseComplete ( ) <EOL> class ResponseCode ( object ) : <EOL> """<STR_LIT>""" <EOL> _RESPONSE_CODES = { <EOL> <NUM_LIT:100> : '<STR_LIT>' , <EOL> <NUM_LIT> : '<STR_LIT>' , <EOL> <NUM_LIT> : '<STR_LIT>' , <EOL> <NUM_LIT:200> : '<STR_LIT:OK>' , <EOL> <NUM_LIT> : '<STR_LIT>' , <EOL> <NUM_LIT> : '<STR_LIT>' , <EOL> <NUM_LIT> : '<STR_LIT>' , <EOL> <NUM_LIT> : '<STR_LIT>' , <EOL> <NUM_LIT> : '<STR_LIT>' , <EOL> <NUM_LIT> : '<STR_LIT>' , <EOL> <NUM_LIT> : '<STR_LIT>' , <EOL> <NUM_LIT> : '<STR_LIT>' , <EOL> <NUM_LIT> : '<STR_LIT>' , <EOL> <NUM_LIT> : '<STR_LIT>' , <EOL> <NUM_LIT> : '<STR_LIT>' , <EOL> <NUM_LIT> : '<STR_LIT>' , <EOL> <NUM_LIT> : '<STR_LIT>' , <EOL> <NUM_LIT> : '<STR_LIT>' , <EOL> <NUM_LIT> : '<STR_LIT>' , <EOL> <NUM_LIT> : '<STR_LIT>' , <EOL> <NUM_LIT> : '<STR_LIT>' , <EOL> <NUM_LIT> : '<STR_LIT>' , <EOL> <NUM_LIT> : '<STR_LIT>' , <EOL> <NUM_LIT> : '<STR_LIT>' , <EOL> <NUM_LIT> : '<STR_LIT>' , <EOL> <NUM_LIT> : '<STR_LIT>' , <EOL> <NUM_LIT> : '<STR_LIT>' , <EOL> <NUM_LIT> : '<STR_LIT>' , <EOL> <NUM_LIT> : '<STR_LIT>' , <EOL> <NUM_LIT> : '<STR_LIT>' , <EOL> <NUM_LIT> : '<STR_LIT>' , <EOL> <NUM_LIT> : '<STR_LIT>' , <EOL> <NUM_LIT> : '<STR_LIT>' , <EOL> <NUM_LIT> : '<STR_LIT>' , <EOL> <NUM_LIT> : '<STR_LIT>' , <EOL> <NUM_LIT> : '<STR_LIT>' , <EOL> <NUM_LIT> : '<STR_LIT>' , <EOL> <NUM_LIT> : '<STR_LIT>' , <EOL> <NUM_LIT> : '<STR_LIT>' , <EOL> <NUM_LIT> : '<STR_LIT>' , <EOL> <NUM_LIT> : '<STR_LIT>' , <EOL> <NUM_LIT> : '<STR_LIT>' , <EOL> <NUM_LIT> : '<STR_LIT>' , <EOL> <NUM_LIT> : '<STR_LIT>' , <EOL> <NUM_LIT> : '<STR_LIT>' , <EOL> <NUM_LIT> : '<STR_LIT>' , <EOL> <NUM_LIT> : '<STR_LIT>' , <EOL> <NUM_LIT> : '<STR_LIT>' , <EOL> <NUM_LIT> : '<STR_LIT>' , <EOL> } <EOL> @ classmethod <EOL> def GetReasonPhrase ( cls , code ) : <EOL> """<STR_LIT>""" <EOL> if not isinstance ( code , int ) : <EOL> raise TypeError ( '<STR_LIT>' % code ) <EOL> if code not in cls . _RESPONSE_CODES : <EOL> logging . warn ( '<STR_LIT>' , code ) <EOL> return cls . _RESPONSE_CODES . get ( code , '<STR_LIT>' ) <EOL> class Connection ( asyncore . dispatcher_with_send ) : <EOL> """<STR_LIT>""" <EOL> _blocked = False <EOL> def __init__ ( self , sock , addr , callback ) : <EOL> asyncore . dispatcher_with_send . __init__ ( self , sock ) <EOL> self . _buffer = '<STR_LIT>' <EOL> self . _remote_addr = addr <EOL> self . _callback = callback <EOL> def handle_read ( self ) : <EOL> data = self . recv ( <NUM_LIT> ) <EOL> if not data : <EOL> self . close ( ) <EOL> return <EOL> self . _buffer += data <EOL> self . _CheckForNewRequest ( ) <EOL> def _CheckForNewRequest ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . _blocked : <EOL> return <EOL> if '<STR_LIT>' in self . _buffer : <EOL> request , new_buffer = self . _buffer . split ( '<STR_LIT>' , <NUM_LIT:1> ) <EOL> self . _buffer = new_buffer <EOL> response = Response ( self ) <EOL> try : <EOL> request = Request ( request , self . _remote_addr ) <EOL> except ( KeyError , ValueError ) : <EOL> response . headers . add_header ( '<STR_LIT:Content-Type>' , '<STR_LIT>' ) <EOL> response . start_response ( <NUM_LIT> ) <EOL> response . out . write ( '<STR_LIT>' ) <EOL> response . end_response ( ) <EOL> return <EOL> if request . path != '<STR_LIT>' : <EOL> logging . info ( '<STR_LIT>' , self . _remote_addr , request . method , <EOL> request . path ) <EOL> self . _blocked = True <EOL> self . _callback ( request , response ) <EOL> def ResponseComplete ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _blocked = False <EOL> self . _CheckForNewRequest ( ) <EOL> class Server ( asyncore . dispatcher ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , host , port , callback ) : <EOL> asyncore . dispatcher . __init__ ( self ) <EOL> self . _callback = callback <EOL> self . _started = threading . Event ( ) <EOL> self . create_socket ( socket . AF_INET , socket . SOCK_STREAM ) <EOL> self . set_reuse_addr ( ) <EOL> self . bind ( ( host , port ) ) <EOL> self . listen ( <NUM_LIT:5> ) <EOL> self . _connections = weakref . WeakValueDictionary ( ) <EOL> def handle_accept ( self ) : <EOL> pair = self . accept ( ) <EOL> if pair is None : <EOL> return <EOL> sock , addr = pair <EOL> logging . debug ( '<STR_LIT>' % addr ) <EOL> self . _connections [ sock ] = Connection ( sock , addr [ <NUM_LIT:0> ] , self . _callback ) <EOL> def WaitUntilHTTPServerIsRunning ( self ) : <EOL> self . _started . wait ( ) <EOL> def Loop ( self ) : <EOL> self . _started . set ( ) <EOL> asyncore . loop ( ) <EOL> def Shutdown ( self ) : <EOL> for conn in self . _connections . values ( ) : <EOL> if conn is not None : <EOL> conn . close ( ) <EOL> self . close ( ) </s>
