common
This commit is contained in:
parent
67d4c90ed1
commit
7438ea9349
|
@ -2,12 +2,23 @@ import config_loader
|
||||||
import socket
|
import socket
|
||||||
import log
|
import log
|
||||||
|
|
||||||
class Client:
|
class SocketClient:
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.config = config_loader.get_config()
|
self.config = config_loader.get_config()
|
||||||
|
|
||||||
|
def start(self):
|
||||||
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
|
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
|
||||||
s.connect((self.config['server']['host'], self.config['server']['port']))
|
s.connect((self.config['server']['host'], self.config['server']['port']))
|
||||||
s.sendall(b'Hello world')
|
s.sendall(b'Hello world')
|
||||||
data = s.recv(1024)
|
data = s.recv(1024)
|
||||||
|
|
||||||
log.error(f'> Received {data!r}')
|
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()
|
||||||
|
|
|
@ -15,6 +15,9 @@ class SocketServer:
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
|
self.server = None
|
||||||
|
|
||||||
|
def start(self):
|
||||||
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
|
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
|
||||||
self.server = s
|
self.server = s
|
||||||
s.bind((config['server']['host'], config['server']['port']))
|
s.bind((config['server']['host'], config['server']['port']))
|
||||||
|
@ -22,20 +25,31 @@ class SocketServer:
|
||||||
log.debug(f'* Server listening on {config["server"]["port"]}')
|
log.debug(f'* Server listening on {config["server"]["port"]}')
|
||||||
|
|
||||||
# find a way to spin up a thread or some coprocessor to deal with this
|
# find a way to spin up a thread or some coprocessor to deal with this
|
||||||
|
while(True):
|
||||||
self.connect(s.accept())
|
self.connect(s.accept())
|
||||||
|
|
||||||
|
|
||||||
|
def connect(self, c_a):
|
||||||
def connect(self, connection, addr):
|
(connection, addr) = c_a
|
||||||
with connection:
|
with connection:
|
||||||
log.debug(f'> Accepted connection from {addr}')
|
log.debug(f'> Accepted connection from {addr}')
|
||||||
while True:
|
while True:
|
||||||
data = connection.recv(1024)
|
data = connection.recv(1024)
|
||||||
if not data:
|
if not data or data == "q":
|
||||||
break
|
break
|
||||||
# erm... what
|
|
||||||
# why are we mass pinging?
|
|
||||||
connection.sendall(data)
|
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()
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user