aboutsummaryrefslogtreecommitdiffstats
path: root/server.py
diff options
context:
space:
mode:
Diffstat (limited to 'server.py')
-rwxr-xr-xserver.py16
1 files changed, 14 insertions, 2 deletions
diff --git a/server.py b/server.py
index 8123e88..d08be9d 100755
--- a/server.py
+++ b/server.py
@@ -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):
@@ -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()