network programming and sockets read incoming request data def handle_read(self)chunk self recv( self request_data +chunk if '\ \ \ \nin self request_dataself handle_request(handle an incoming request def handle_request(self)self got_request true header_data self request_data[:self request_data find( '\ \ \ \ ')header_text header_data decode('latin- 'header_lines header_text splitlines(request header_lines[ split(op request[ url request[ ][ :self process_request(op,urlprocess the request def process_request(self,op,url)self responding true if op ="get"if not os path exists(url)self send_error( ,"file % not found\ \nurlelsetypeencoding mimetypes guess_type(urlsize os path getsize(urlself push_text('http/ ok\ \ 'self push_text('content-length% \ \nsizeself push_text('content-type% \ \ntypeself push_text('\ \ 'self push(open(url,"rb"read()elseself send_error( ,"% method not implementedself operror handling def send_error(self,code,message)self push_text('http/ % % \ \ (coderesponses[code])self push_text('content-typetext/plain\ \ 'self push_text('\ \ 'self push_text(messageadd binary data to the output queue def push(self,data)self write_queue append(dataadd text data to the output queue def push_text(self,text)self push(text encode('latin- ')only writable if response is ready def writable(self)return self responding and self write_queue write response data def handle_write(self)chunk self write_queue popleft(bytes_sent self send(chunkif bytes_sent !len(chunk)self write_queue appendleft(chunk[bytes_sent:]if not self write_queueself close( lib fl ff