From ae36cf8045e17f55449354cda40138f6d3487d22 Mon Sep 17 00:00:00 2001 From: Bastian Schroll Date: Sun, 20 Oct 2019 17:56:40 +0200 Subject: [PATCH] changes in client --- boswatch/network/client.py | 10 +++++----- test/boswatch/test_ServerClient.py | 7 +++++++ 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/boswatch/network/client.py b/boswatch/network/client.py index 33db246..9c3406a 100644 --- a/boswatch/network/client.py +++ b/boswatch/network/client.py @@ -76,13 +76,14 @@ class TCPClient: @param data: data to send to the server @return True or False""" try: + if not self._sock: # check if socket is still available + logging.error("cannot receive - no connection established") + return False logging.debug("transmitting: %s", data) header = str(len(data)).ljust(HEADERSIZE) self._sock.sendall(bytes(header + data, "utf-8")) logging.debug("transmitted...") return True - except AttributeError: - logging.error("cannot transmit - no connection established") except ConnectionResetError: logging.error("cannot transmit - host closed connection") return False @@ -93,6 +94,7 @@ class TCPClient: @return received data""" try: if not self._sock: # check if socket is still available + logging.error("cannot receive - no connection established") return False read, _, _ = select.select([self._sock], [], [], 1) if not read: # check if there is something to read @@ -104,8 +106,6 @@ class TCPClient: received = self._sock.recv(length).decode("utf-8") logging.debug("received %d bytes: %s", length, received) return received - except AttributeError: - logging.error("cannot receive - no connection established") except ConnectionResetError: logging.error("cannot receive - host closed connection") except socket.timeout: # pragma: no cover @@ -122,7 +122,7 @@ class TCPClient: self._sock.sendall(bytes(header + aliveMsg, "utf-8")) return True except (AttributeError, BrokenPipeError): - logging.exception("Unknown error: ") + logging.error("Unknown error: ") except ConnectionResetError: pass return False diff --git a/test/boswatch/test_ServerClient.py b/test/boswatch/test_ServerClient.py index 93f7b39..c6b4f80 100644 --- a/test/boswatch/test_ServerClient.py +++ b/test/boswatch/test_ServerClient.py @@ -82,6 +82,13 @@ def test_clientConnect(getClient, getRunningServer): assert getClient.disconnect() +def test_doubleConnect(getClient, getRunningServer): + """!Connect to a server twice""" + assert getClient.connect() + assert getClient.connect() + assert getClient.disconnect() + + def test_clientReconnect(getClient, getRunningServer): """!Try a reconnect after a established connection""" assert getClient.connect()