commit 94d9542a3d6d11f5f84941f35837f8c1ab28bd8d Author: hornet Date: Tue Mar 21 17:52:27 2023 -0400 nos0s initial commit diff --git a/groundwork/client.py b/groundwork/client.py new file mode 100644 index 0000000..2957683 --- /dev/null +++ b/groundwork/client.py @@ -0,0 +1,13 @@ +import config_loader +import socket +import log + +class Client: + def __init__(self): + self.config = config_loader.get_config() + 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}') diff --git a/groundwork/config.yaml b/groundwork/config.yaml new file mode 100644 index 0000000..0c8c2ed --- /dev/null +++ b/groundwork/config.yaml @@ -0,0 +1,3 @@ +server: + host: '127.0.0.1' + port: 35293 \ No newline at end of file diff --git a/groundwork/config_loader.py b/groundwork/config_loader.py new file mode 100644 index 0000000..3ecee79 --- /dev/null +++ b/groundwork/config_loader.py @@ -0,0 +1,27 @@ +import yaml + +config = None + +def read_config(): + global config + with open ("config.yaml", "r") as conf_file: + try: + config = yaml.safe_load(conf_file) + except: + print("Error reading config") + config = None + +def get_config(): + global config + if config == None: + read_config() + return config + + +def main(): + print(get_config()) + +read_config() + +if __name__ == "__main__": + main() \ No newline at end of file diff --git a/groundwork/log.py b/groundwork/log.py new file mode 100644 index 0000000..90937f3 --- /dev/null +++ b/groundwork/log.py @@ -0,0 +1,41 @@ + +class LogLevel: + DEBUG = 0 + WARNING = 1 + ERROR = 2 + CRITICAL = 3 + +log_lvl = LogLevel.CRITICAL + + +def set_lvl(lvl): + global log_lvl + if lvl < LogLevel.DEBUG: + lvl = LogLevel.DEBUG + if lvl > LogLevel.CRITICAL: + lvl = LogLevel.CRITICAL + + log_lvl = lvl + + +def debug(message): + global log_lvl + if log_lvl <= LogLevel.DEBUG: + print(message) + +def warning(message): + global log_lvl + if log_lvl <= LogLevel.WARNING: + print(message) + +def error(message): + global log_lvl + if log_lvl <= LogLevel.ERROR: + print(message) + +def critical(message): + global log_lvl + if log_lvl <= LogLevel.CRITICAL: + print(message) + + diff --git a/groundwork/srv.py b/groundwork/srv.py new file mode 100644 index 0000000..730e1ce --- /dev/null +++ b/groundwork/srv.py @@ -0,0 +1,41 @@ +import config_loader +import socket +import log + +""" +https://realpython.com/python-sockets/#multi-connection-client-and-server +""" + +config = config_loader.get_config() + + +class SocketServer: + """ + groundwork for internet comms + """ + + def __init__(self): + with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: + self.server = s + s.bind((config['server']['host'], config['server']['port'])) + s.listen() + 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()) + + + + def connect(self, connection, addr): + with connection: + log.debug(f'> Accepted connection from {addr}') + while True: + data = connection.recv(1024) + if not data: + break + # erm... what + # why are we mass pinging? + connection.sendall(data) + + +