BW3-Core/boswatch/network/client.py

113 lines
3.8 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
Create a new instance of an TCP Client.
And set the timeout"""
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)
@param port: Server Port (8080)
@return True or False"""
try:
self._sock = socket
self._sock.setdefaulttimeout(self._timeout)
self._sock = socket.create_connection((host, port))
logging.debug("connected to " + str(host) + ":" + str(port))
return True
except socket.timeout:
logging.warning("cannot connect to %s:%s - timeout after %s sec", str(host), str(port), self._timeout)
2018-01-07 21:50:41 +01:00
except ConnectionRefusedError:
logging.error("cannot connect to %s:%s - connection refused", str(host), str(port))
2018-01-07 21:50:41 +01:00
return False
except: # pragma: no cover
logging.exception("cannot connect to %s:%s", str(host), str(port))
2018-01-07 21:50:41 +01:00
return False
def disconnect(self):
"""!Disconnect from the server
@return True or False"""
try:
self._sock.close()
logging.debug("disconnected")
return True
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:
logging.debug("transmitting: " + data)
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: " + received)
return received
2018-01-12 09:02:07 +01:00
except socket.timeout:
logging.warning("cannot receive - timeout after %s sec", self._timeout)
2018-01-07 21:50:41 +01:00
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-01-07 21:50:41 +01:00
except: # pragma: no cover
logging.exception("error while receiving")
return False