This commit is contained in:
hornet 2023-03-26 15:16:40 -04:00
parent 67d4c90ed1
commit 7438ea9349
2 changed files with 33 additions and 8 deletions

View File

@ -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()

View File

@ -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()