nos0s/common/srv.py
2023-03-26 15:16:40 -04:00

56 lines
1.2 KiB
Python

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):
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']))
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
while(True):
self.connect(s.accept())
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 or data == "q":
break
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()