diff options
| author | T <T@192.168.0.100> | 2017-03-30 19:02:07 +0300 |
|---|---|---|
| committer | T <T@192.168.0.100> | 2017-03-30 19:02:07 +0300 |
| commit | c74b1793f121bb331cbe02275d9682aa57a89e17 (patch) | |
| tree | 174573b8df788decd8ffa370f1bf07bc0165d3f1 /server.py | |
| parent | 5b08d3894c743791bfc0707cd6556ccb42fb1ffc (diff) | |
| parent | 439cd4fd5bffb9b5e74cf2840a185688dd275d99 (diff) | |
Merge branch 'master' of https://github.com/jantuomi/chatserver-homework
Diffstat (limited to 'server.py')
| -rwxr-xr-x | server.py | 18 |
1 files changed, 15 insertions, 3 deletions
@@ -2,16 +2,28 @@ import socket import threading +import signal +import sys from functools import partial from datetime import datetime HOST = "0.0.0.0" PORT = 5000 DEBUG = True +running = True + def debug(string): if (DEBUG): print("{} DEBUG: {}".format(datetime.now(), string)) +def signal_handler(signal, frame): + print("Closing sockets and shutting down...") + global running + running = False + sys.exit(0) + +signal.signal(signal.SIGINT, signal_handler) + def parse_message(text): rows = text.split('\n') if (len(rows) < 3): @@ -42,7 +54,7 @@ def do_command(command, username, body, bc): elif command == "DISCONNECT": bc("User {} has disconnected".format(username), username) elif command == "MESSAGE": - bc("{}: {}".format(username, body), username) + bc("{}".format(body), username) elif command == "KEEPALIVE": pass else: @@ -52,7 +64,7 @@ def keep_connection(conn, addr, connections): debug("Connection from: {}".format(addr)) broadcast_fun = partial(broadcast, recipients=connections) - while True: + while running: data = conn.recv(1024).decode() if not data: @@ -77,9 +89,9 @@ def main(): connections = [] threads = [] sock.listen(1) - running = True print("Chat server running on {}:{}".format(HOST, PORT)) + print("Press CTRL-C to quit") while running: conn, addr = sock.accept() |
