mirror of
https://github.com/BOSWatch/BW3-Core.git
synced 2026-04-21 06:03:50 +00:00
add server/client class
This commit is contained in:
parent
0f1a6f7b96
commit
73c0541629
4 changed files with 275 additions and 0 deletions
99
boswatch/network/client.py
Normal file
99
boswatch/network/client.py
Normal file
|
|
@ -0,0 +1,99 @@
|
|||
#!/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):
|
||||
"""!Create a new instance"""
|
||||
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 ConnectionRefusedError:
|
||||
logging.error("cannot connect - connection refused")
|
||||
return False
|
||||
except: # pragma: no cover
|
||||
logging.exception("cannot connect to " + str(host) + ":" + str(port))
|
||||
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):
|
||||
"""!Send a data packet.py to the server
|
||||
|
||||
@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
|
||||
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
|
||||
except AttributeError:
|
||||
logging.error("cannot receive - no connection established")
|
||||
return False
|
||||
except: # pragma: no cover
|
||||
logging.exception("error while receiving")
|
||||
return False
|
||||
Loading…
Add table
Add a link
Reference in a new issue