From 3c5c1690459f555a74e917bd0b0aae0ffdca8da8 Mon Sep 17 00:00:00 2001 From: Fred Z Date: Thu, 1 Mar 2018 10:56:35 +0100 Subject: [PATCH] Clean the main-loop from useless instructions Remove: - try/catch for received case, seems to be useless - useless `continue` - **move closing connection at the script's end** from TODO, do not find how to exit while from handler() --- chat/TODO.md | 2 +- chat/server.py | 23 ++++++----------------- 2 files changed, 7 insertions(+), 18 deletions(-) diff --git a/chat/TODO.md b/chat/TODO.md index ba21008..dcfc1e5 100644 --- a/chat/TODO.md +++ b/chat/TODO.md @@ -8,8 +8,8 @@ - [x] ~~crash after 2 in client~~ - [x] ~~sending welcome message only at 1st client connection FIX #20~~ - [x] ~~client freeze when sending empty string or spaces~~ +- [x] ~~move closing connection at the script's end~~ - [ ] asking/using client-nickname - [ ] clean the prompt and std.out a bit messy since broadcasting -- [ ] move closing connection at the script's end - [ ] using wlist with select.select for hardening the script - [ ] … diff --git a/chat/server.py b/chat/server.py index 96eddc4..2650b3e 100755 --- a/chat/server.py +++ b/chat/server.py @@ -71,24 +71,13 @@ while 1: socket_object.send(MSG_WELCOME) else: # receiving data - try: - data = socket.recv(BUFFER).decode().strip() - if data.upper() == "QUIT": - print(MSG_CLIENT_DISCONNECTED.format(socket.getpeername())) - broadcast(socket, MSG_DISCONNECTED) - inputs.remove(socket) - socket.close() - continue - - elif data: - print(MSG_CLIENT_ID.format(socket.getpeername(), data)) - broadcast(socket, data) - except Exception as except_detail: - print("Exception: «{}»".format(except_detail)) + data = socket.recv(BUFFER).decode().strip() + if data.upper() == "QUIT": print(MSG_CLIENT_DISCONNECTED.format(socket.getpeername())) - import pdb; pdb.set_trace() + broadcast(socket, MSG_DISCONNECTED) inputs.remove(socket) socket.close() - continue -MAIN_CONNECTION.close() + elif data: + print(MSG_CLIENT_ID.format(socket.getpeername(), data)) + broadcast(socket, data)