Facebook
From asd, 2 Months ago, written in Python.
Embed
Download Paste or View Raw
Hits: 170
  1. import asyncio
  2. import websockets
  3.  
  4. async def send_commands(websocket, path):
  5.     print("Client connected.")
  6.     try:
  7.         while True:
  8.             command = input("Enter command (type 'exit' to quit): ")
  9.             await websocket.send(command)
  10.             if command == "exit":
  11.                 print("Exiting.")
  12.                 break
  13.  
  14.             response = await websocket.recv()
  15.             print("Received output:")
  16.             print(response)
  17.  
  18.     except websockets.ConnectionClosedOK:
  19.         print("Client disconnected.")
  20.  
  21. async def main():
  22.     server = await websockets.serve(send_commands, "localhost", 8080)
  23.     await server.wait_closed()
  24.  
  25. asyncio.run(main())
  26.