summaryrefslogtreecommitdiff
path: root/project1/proj1_s4498062/webhttp/server.py
diff options
context:
space:
mode:
authorCamil Staps2016-03-16 22:41:42 +0100
committerCamil Staps2016-03-16 22:41:42 +0100
commit7ed2ab44d5328f6ac2053160d4d8abe847ef8dbc (patch)
tree6302df18cfad128ced8f9996550c7fc67386c63c /project1/proj1_s4498062/webhttp/server.py
parentStart assignment 3 (diff)
Project 1: started with persistent connections
Diffstat (limited to 'project1/proj1_s4498062/webhttp/server.py')
-rw-r--r--project1/proj1_s4498062/webhttp/server.py24
1 files changed, 17 insertions, 7 deletions
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: