BW3-Core/boswatch/network/client.py

129 lines
4.3 KiB
Python
Raw Normal View History

2018-01-07 21:50:41 +01:00
#!/usr/bin/python
# -*- coding: utf-8 -*-
"""!
____ ____ ______ __ __ __ _____
/ __ )/ __ \/ ___/ | / /___ _/ /______/ /_ |__ /
/ __ / / / /\__ \| | /| / / __ `/ __/ ___/ __ \ /_ <
/ /_/ / /_/ /___/ /| |/ |/ / /_/ / /_/ /__/ / / / ___/ /
/_____/\____//____/ |__/|__/\__,_/\__/\___/_/ /_/ /____/
German BOS Information Script
by Bastian Schroll
@file: client.py
@date: 09.12.2017
@author: Bastian Schroll
@description: Class implementation for a TCP socket client
"""
import logging
import socket
logging.debug("- %s loaded", __name__)
class TCPClient:
"""!TCP client class"""
def __init__(self, timeout=3):
2018-01-07 22:34:40 +01:00
"""!Create a new instance
2018-09-21 22:09:30 +02:00
@param timeout: timeout for the client in sec. (3)"""
2018-01-07 21:50:41 +01:00
try:
self._sock = None
self._timeout = timeout
except: # pragma: no cover
logging.exception("cannot create a TCPClient")
def connect(self, host="localhost", port=8080):
"""!Connect to the server
@param host: Server IP address ("localhost")
2018-01-07 21:50:41 +01:00
@param port: Server Port (8080)
@return True or False"""
try:
if not self.isConnected:
self._sock = socket
self._sock.setdefaulttimeout(self._timeout)
self._sock = socket.create_connection((host, port))
logging.debug("connected to %s:%s", host, port)
return True
else:
logging.warning("client always connected")
return True
2018-01-07 21:50:41 +01:00
except ConnectionRefusedError:
2018-09-18 05:59:38 +02:00
logging.error("cannot connect to %s:%s - connection refused", host, port)
2018-01-07 21:50:41 +01:00
return False
2018-02-22 07:34:36 +01:00
except socket.timeout: # pragma: no cover
2018-09-18 05:59:38 +02:00
logging.warning("cannot connect to %s:%s - timeout after %s sec", host, port, self._timeout)
2018-02-22 07:34:36 +01:00
return False
2018-01-07 21:50:41 +01:00
except: # pragma: no cover
2018-09-18 05:59:38 +02:00
logging.exception("cannot connect to %s:%s", host, port)
2018-01-07 21:50:41 +01:00
return False
def disconnect(self):
"""!Disconnect from the server
@return True or False"""
try:
if self.isConnected:
self._sock.close()
self._sock = None
logging.debug("disconnected")
return True
else:
logging.warning("client not connected")
return True
2018-01-07 21:50:41 +01:00
except AttributeError:
logging.error("cannot disconnect - no connection established")
return False
except: # pragma: no cover
logging.exception("error while disconnecting")
return False
def transmit(self, data):
2018-01-07 22:34:40 +01:00
"""!Send a data packet to the server
2018-01-07 21:50:41 +01:00
@param data: data to send to the server
@return True or False"""
try:
2018-09-18 05:59:38 +02:00
logging.debug("transmitting: %s", data)
2018-01-07 21:50:41 +01:00
self._sock.sendall(bytes(data + "\n", "utf-8"))
logging.debug("transmitted...")
return True
except AttributeError:
logging.error("cannot transmit - no connection established")
return False
2018-01-11 13:01:27 +01:00
except ConnectionResetError:
logging.error("cannot transmit - host closed connection")
return False
2018-01-07 21:50:41 +01:00
except: # pragma: no cover
logging.exception("error while transmitting")
return False
def receive(self):
"""!Receive data from the server
@return received data"""
try:
received = str(self._sock.recv(1024), "utf-8")
logging.debug("received: %s", received)
2018-01-07 21:50:41 +01:00
return received
except AttributeError:
logging.error("cannot receive - no connection established")
return False
2018-01-13 09:56:46 +01:00
except ConnectionResetError:
logging.error("cannot receive - host closed connection")
return False
2018-02-22 07:34:36 +01:00
except socket.timeout: # pragma: no cover
logging.warning("cannot receive - timeout after %s sec", self._timeout)
return False
2018-01-07 21:50:41 +01:00
except: # pragma: no cover
logging.exception("error while receiving")
return False
@property
def isConnected(self):
"""!Property of client connected state"""
if self._sock:
return True
return False