<s> import datetime <EOL> import io <EOL> import socket <EOL> import sys <EOL> import time <EOL> import urllib <EOL> from . utils import to_text , to_bytes <EOL> class WSGIServer : <EOL> address_family = socket . AF_INET <EOL> socket_type = socket . SOCK_STREAM <EOL> request_queue_size = <NUM_LIT:5> <EOL> allow_reuse_address = True <EOL> default_request_version = '<STR_LIT>' <EOL> server_version = '<STR_LIT>' <EOL> weekdayname = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> monthname = [ None , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> def __init__ ( self , server_address ) : <EOL> self . socket = socket . socket ( self . address_family , self . socket_type ) <EOL> self . server_bind ( server_address ) <EOL> self . server_activate ( ) <EOL> self . setup_environ ( ) <EOL> self . headers_set = [ ] <EOL> def server_bind ( self , server_address ) : <EOL> if self . allow_reuse_address : <EOL> self . socket . setsockopt ( socket . SOL_SOCKET , socket . SO_REUSEADDR , <NUM_LIT:1> ) <EOL> self . socket . bind ( server_address ) <EOL> self . server_address = self . socket . getsockname ( ) <EOL> host , port = self . socket . getsockname ( ) [ : <NUM_LIT:2> ] <EOL> self . server_name = socket . getfqdn ( host ) <EOL> self . server_port = port <EOL> def server_activate ( self ) : <EOL> self . socket . listen ( self . request_queue_size ) <EOL> def setup_environ ( self ) : <EOL> """<STR_LIT>""" <EOL> env = self . base_environ = { } <EOL> env [ '<STR_LIT>' ] = self . server_name <EOL> env [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> env [ '<STR_LIT>' ] = str ( self . server_port ) <EOL> env [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> env [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> env [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> def get_app ( self ) : <EOL> return self . application <EOL> def set_app ( self , application ) : <EOL> self . application = application <EOL> def serve_forever ( self ) : <EOL> while <NUM_LIT:1> : <EOL> self . handle_one_request ( ) <EOL> self . client_connection . close ( ) <EOL> def handle_one_request ( self ) : <EOL> self . client_connection , self . client_address = self . socket . accept ( ) <EOL> init_read = <NUM_LIT> <EOL> self . raw_request = raw_request = self . client_connection . recv ( init_read ) <EOL> self . parse_request ( raw_request ) <EOL> self . parse_headers ( raw_request ) <EOL> length = int ( self . headers . get ( '<STR_LIT>' , '<STR_LIT:0>' ) ) <EOL> while len ( self . raw_request ) == length : <EOL> self . raw_request += self . client_connection . recv ( init_read ) <EOL> env = self . get_environ ( ) <EOL> print ( '<STR_LIT>' % ( <EOL> datetime . datetime . now ( ) , env [ '<STR_LIT>' ] , <EOL> env [ '<STR_LIT>' ] , env [ '<STR_LIT>' ] , <EOL> ) ) <EOL> result = self . application ( env , self . start_response ) <EOL> self . finish_response ( result ) <EOL> def parse_request ( self , raw_request ) : <EOL> first_line = to_text ( raw_request . split ( b'<STR_LIT:\r\n>' , <NUM_LIT:1> ) [ <NUM_LIT:0> ] . strip ( ) ) <EOL> ( self . request_method , <EOL> self . path , <EOL> self . request_version <EOL> ) = first_line . split ( ) <EOL> def parse_headers ( self , raw_request ) : <EOL> header_string = to_text ( raw_request . split ( b'<STR_LIT>' , <NUM_LIT:1> ) [ <NUM_LIT:0> ] ) <EOL> self . headers = headers = { } <EOL> for header in header_string . splitlines ( ) [ <NUM_LIT:1> : ] : <EOL> k , v = header . split ( '<STR_LIT::>' , <NUM_LIT:1> ) <EOL> if headers . get ( k ) : <EOL> headers [ k ] += '<STR_LIT:U+002CU+0020>' + v . strip ( ) <EOL> else : <EOL> headers [ k ] = v . strip ( ) <EOL> def get_environ ( self ) : <EOL> """<STR_LIT>""" <EOL> env = self . base_environ . copy ( ) <EOL> env [ '<STR_LIT>' ] = self . request_method <EOL> if '<STR_LIT:?>' in self . path : <EOL> path , query = self . path . split ( '<STR_LIT:?>' , <NUM_LIT:1> ) <EOL> else : <EOL> path , query = self . path , '<STR_LIT>' <EOL> env [ '<STR_LIT>' ] = urllib . parse . unquote ( path ) <EOL> env [ '<STR_LIT>' ] = query <EOL> env [ '<STR_LIT>' ] = self . headers . get ( '<STR_LIT:Content-Type>' , '<STR_LIT>' ) <EOL> env [ '<STR_LIT>' ] = self . headers . get ( '<STR_LIT>' , '<STR_LIT:0>' ) <EOL> env [ '<STR_LIT>' ] = self . request_version <EOL> env [ '<STR_LIT>' ] = self . client_address [ <NUM_LIT:0> ] <EOL> env [ '<STR_LIT>' ] = self . client_address [ <NUM_LIT:1> ] <EOL> env [ '<STR_LIT>' ] = ( <NUM_LIT:1> , <NUM_LIT:0> ) <EOL> env [ '<STR_LIT>' ] = '<STR_LIT:http>' <EOL> env [ '<STR_LIT>' ] = io . BytesIO ( self . raw_request ) <EOL> env [ '<STR_LIT>' ] = sys . stderr <EOL> env [ '<STR_LIT>' ] = False <EOL> env [ '<STR_LIT>' ] = True <EOL> env [ '<STR_LIT>' ] = False <EOL> for k , v in self . headers . items ( ) : <EOL> k = k . replace ( '<STR_LIT:->' , '<STR_LIT:_>' ) . upper ( ) <EOL> if k in env : <EOL> continue <EOL> env [ '<STR_LIT>' + k ] = v <EOL> return env <EOL> def start_response ( self , status , headers , exc_info = None ) : <EOL> server_headers = [ <EOL> ( '<STR_LIT>' , self . date_time_string ( ) ) , <EOL> ( '<STR_LIT>' , self . version_string ( ) ) , <EOL> ] <EOL> headers = list ( headers ) + server_headers <EOL> if exc_info : <EOL> try : <EOL> if self . headers_set : <EOL> raise ( exc_info [ <NUM_LIT:0> ] , exc_info [ <NUM_LIT:1> ] , exc_info [ <NUM_LIT:2> ] ) <EOL> finally : <EOL> exc_info = None <EOL> self . headers_set [ : ] = [ status , headers ] <EOL> def finish_response ( self , body ) : <EOL> try : <EOL> status , headers = self . headers_set <EOL> response = ( <EOL> to_bytes ( self . default_request_version ) + <EOL> b'<STR_LIT:U+0020>' + <EOL> to_bytes ( status ) + <EOL> b'<STR_LIT:\r\n>' <EOL> ) <EOL> response += b'<STR_LIT:\r\n>' . join ( [ to_bytes ( '<STR_LIT>' . join ( x ) ) for x in headers ] ) <EOL> response += b'<STR_LIT>' <EOL> for d in body : <EOL> response += d <EOL> self . client_connection . sendall ( response ) <EOL> finally : <EOL> self . client_connection . close ( ) <EOL> def version_string ( self ) : <EOL> return self . server_version <EOL> def date_time_string ( self , timestamp = None ) : <EOL> if timestamp is None : <EOL> timestamp = time . time ( ) <EOL> year , month , day , hh , mm , ss , wd , y , z = time . gmtime ( timestamp ) <EOL> s = '<STR_LIT>' % ( <EOL> self . weekdayname [ wd ] , <EOL> day , self . monthname [ month ] , year , <EOL> hh , mm , ss <EOL> ) <EOL> return s <EOL> def make_server ( server_address , application ) : <EOL> server = WSGIServer ( server_address ) <EOL> server . set_app ( application ) <EOL> return server <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> if len ( sys . argv ) < <NUM_LIT:2> : <EOL> sys . exit ( '<STR_LIT>' ) <EOL> app_path = sys . argv [ <NUM_LIT:1> ] <EOL> module , application = app_path . split ( '<STR_LIT::>' ) <EOL> module = __import__ ( module ) <EOL> application = getattr ( module , application ) <EOL> SERVER_ADDRESS = ( HOST , PORT ) = '<STR_LIT>' , <NUM_LIT> <EOL> httpd = make_server ( SERVER_ADDRESS , application ) <EOL> print ( '<STR_LIT>' . format ( port = PORT ) ) <EOL> httpd . serve_forever ( ) </s>
