nos0s initial commit
This commit is contained in:
commit
94d9542a3d
13
groundwork/client.py
Normal file
13
groundwork/client.py
Normal file
|
@ -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}')
|
3
groundwork/config.yaml
Normal file
3
groundwork/config.yaml
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
server:
|
||||||
|
host: '127.0.0.1'
|
||||||
|
port: 35293
|
27
groundwork/config_loader.py
Normal file
27
groundwork/config_loader.py
Normal file
|
@ -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()
|
41
groundwork/log.py
Normal file
41
groundwork/log.py
Normal file
|
@ -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)
|
||||||
|
|
||||||
|
|
41
groundwork/srv.py
Normal file
41
groundwork/srv.py
Normal file
|
@ -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)
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user