From 7438ea93499381e6a452981b95cb41eceaefce66 Mon Sep 17 00:00:00 2001 From: hornet Date: Sun, 26 Mar 2023 15:16:40 -0400 Subject: [PATCH] common --- common/client.py | 15 +++++++++++++-- common/srv.py | 26 ++++++++++++++++++++------ 2 files changed, 33 insertions(+), 8 deletions(-) diff --git a/common/client.py b/common/client.py index 2957683..16566d9 100644 --- a/common/client.py +++ b/common/client.py @@ -2,12 +2,23 @@ import config_loader import socket import log -class Client: +class SocketClient: def __init__(self): self.config = config_loader.get_config() + + def start(self): with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: s.connect((self.config['server']['host'], self.config['server']['port'])) s.sendall(b'Hello world') data = s.recv(1024) - log.error(f'> Received {data!r}') + #s.sendall(b'q') + + +def main(): + log.set_lvl(log.LogLevel.DEBUG) + c = SocketClient() + c.start() + +if __name__ == '__main__': + main() diff --git a/common/srv.py b/common/srv.py index 730e1ce..322df54 100644 --- a/common/srv.py +++ b/common/srv.py @@ -15,6 +15,9 @@ class SocketServer: """ def __init__(self): + self.server = None + + def start(self): with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: self.server = s s.bind((config['server']['host'], config['server']['port'])) @@ -22,20 +25,31 @@ class SocketServer: log.debug(f'* Server listening on {config["server"]["port"]}') # find a way to spin up a thread or some coprocessor to deal with this - self.connect(s.accept()) + while(True): + self.connect(s.accept()) - - def connect(self, connection, addr): + def connect(self, c_a): + (connection, addr) = c_a with connection: log.debug(f'> Accepted connection from {addr}') while True: data = connection.recv(1024) - if not data: + if not data or data == "q": break - # erm... what - # why are we mass pinging? connection.sendall(data) + log.debug(f'> Connection lost from {addr}') + + + +def main(): + log.set_lvl(log.LogLevel.DEBUG) + s = SocketServer() + s.start() + + +if __name__ == '__main__': + main()