From 57e604a294f13c2ccd4b9946d6e5700ccb81fb70 Mon Sep 17 00:00:00 2001 From: Michel van de Wetering Date: Sat, 30 Mar 2024 16:44:59 +0100 Subject: [PATCH] Add timeout --- ynca/server.py | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/ynca/server.py b/ynca/server.py index 96d98ec..36562e9 100644 --- a/ynca/server.py +++ b/ynca/server.py @@ -149,6 +149,8 @@ class YncaCommandHandler(socketserver.StreamRequestHandler): client. """ + timeout = 40 # Receiver disconnects after 40 seconds of no traffic + def __init__(self, request, client_address, server: YncaServer): self.store = server.store self.disconnect_after_receiving_num_commands = ( @@ -283,9 +285,14 @@ def handle(self): print(f"--- Client connected from: {self.client_address[0]}") while True: - bytes_line = self.rfile.readline() - if bytes_line == b"": - print("--- Client disconnected") + try: + bytes_line = self.rfile.readline() + if bytes_line == b"": + print("--- Client disconnected") + print("--- Waiting for connections") + return + except TimeoutError: + print("--- Disconnecting client because of timeout") print("--- Waiting for connections") return