2023-03-21 21:52:27 +00:00
|
|
|
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):
|
2023-03-26 19:16:40 +00:00
|
|
|
self.server = None
|
|
|
|
|
|
|
|
def start(self):
|
2023-03-21 21:52:27 +00:00
|
|
|
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
|
2023-03-26 19:16:40 +00:00
|
|
|
while(True):
|
|
|
|
self.connect(s.accept())
|
2023-03-21 21:52:27 +00:00
|
|
|
|
|
|
|
|
2023-03-26 19:16:40 +00:00
|
|
|
def connect(self, c_a):
|
|
|
|
(connection, addr) = c_a
|
2023-03-21 21:52:27 +00:00
|
|
|
with connection:
|
|
|
|
log.debug(f'> Accepted connection from {addr}')
|
|
|
|
while True:
|
|
|
|
data = connection.recv(1024)
|
2023-03-26 19:16:40 +00:00
|
|
|
if not data or data == "q":
|
2023-03-21 21:52:27 +00:00
|
|
|
break
|
|
|
|
connection.sendall(data)
|
2023-03-26 19:16:40 +00:00
|
|
|
log.debug(f'> Connection lost from {addr}')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
log.set_lvl(log.LogLevel.DEBUG)
|
|
|
|
s = SocketServer()
|
|
|
|
s.start()
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|
2023-03-21 21:52:27 +00:00
|
|
|
|
|
|
|
|
|
|
|
|