works except exec

Signed-off-by: Frank Villaro-Dixon <frank@villaro-dixon.eu>
This commit is contained in:
Frank Villaro-Dixon 2024-08-27 22:42:31 +02:00
parent 170adf5436
commit c80f43b5f5

54
pod.py
View file

@ -45,14 +45,62 @@ async def handle_client(tcpreader, tcpwriter, ws):
except Exception as e: except Exception as e:
print(f"ASYNCIOD Error: {e}") print(f"ASYNCIOD Error: {e}")
async def handle_socket_read(socketid, tcpreader, ws):
try:
print(f"New socket: {socketid}. Waiting on recv")
print(tcpreader)
while True:
data = await tcpreader.read(2024)
print('GOT DATA', data)
if data == b'':
print(f"Connection closed: {socketid}")
break
print(f'TCP@{socketid}>WS: RAW', data)
c = conn.Conn(socketid, data)
print(f'TCP@{socketid}>WS: ', hashlib.md5(data).hexdigest())
await ws.send(c.to_ws_bytes())
except Exception as e:
print(f"Error: {e}")
import traceback
traceback.print_exc()
async def handle_ws_incoming(ws, sockets):
data = await ws.recv()
c = conn.Conn.from_ws_bytes(data)
socketid = c.socketid
if socketid not in sockets:
print(f"New socket: {socketid}")
tcpreader, tcpwriter = await asyncio.open_connection(HOST, PORT)
sockets[socketid] = (tcpreader, tcpwriter)
print(f'TCPR: {tcpreader}')
asyncio.create_task(handle_socket_read(socketid, tcpreader, ws))
else:
tcpreader, tcpwriter = sockets[socketid]
print(f'WS@{socketid}>TCP: ', hashlib.md5(data).hexdigest())
print(tcpwriter)
tcpwriter.write(c.data)
async def main(): async def main():
tcpreader, tcpwriter = await asyncio.open_connection(HOST, PORT) tcpreader, tcpwriter = await asyncio.open_connection(HOST, PORT)
ws = await websockets.connect(WEBSOCKET_URL) ws = await websockets.connect(WEBSOCKET_URL)
await handle_client(tcpreader, tcpwriter, ws)
sockets = {}
while True:
await handle_ws_incoming(ws, sockets)
#await handle_client(tcpreader, tcpwriter, ws)
if __name__ == "__main__": if __name__ == "__main__":
asyncio.run(main()) asyncio.run(main())