From 7ed2ab44d5328f6ac2053160d4d8abe847ef8dbc Mon Sep 17 00:00:00 2001 From: Camil Staps Date: Wed, 16 Mar 2016 22:41:42 +0100 Subject: Project 1: started with persistent connections --- project1/proj1_s4498062/webhttp/server.py | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) (limited to 'project1/proj1_s4498062/webhttp/server.py') diff --git a/project1/proj1_s4498062/webhttp/server.py b/project1/proj1_s4498062/webhttp/server.py index 7eeec27..f400978 100644 --- a/project1/proj1_s4498062/webhttp/server.py +++ b/project1/proj1_s4498062/webhttp/server.py @@ -33,13 +33,22 @@ class ConnectionHandler(threading.Thread): self.rp = RequestParser() self.rc = ResponseComposer(timeout=self.timeout) - while True: - data = self.conn_socket.recv(4096) - if len(data) == 0: - break - self.handle_data(data) + self.conn_socket.settimeout(self.timeout) - self.conn_socket.close() + # TODO: should not be persistent when client sends Connection: close + # or can we safely assume that in this case the client will close the + # transport channel, so that recv() returns something empty? + try: + while True: + data = self.conn_socket.recv(4096) + if len(data) == 0: + break + self.handle_data(data) + except socket.timeout: + logging.debug('Connection to %s timed out.' % self.addr[0]) + finally: + self.conn_socket.close() + logging.debug('Connection to %s closed.' % self.addr[0]) def handle_data(self, data): for req in self.rp.parse_requests(data): @@ -58,7 +67,8 @@ class ConnectionHandler(threading.Thread): try: self.handle_connection() except socket.error, e: - print('ERR ' + str(e)) + logging.error('Error in handling connection with %s: %s' % + (self.addr[0], str(e))) class Server: -- cgit v1.2.3