RE env for inspecting APKs
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

89 lines
2.5 KiB

  1. import asyncio
  2. class NGServer:
  3. def __init__(self):
  4. self.incoming_queue = asyncio.Queue()
  5. self.outgoing_queue = asyncio.Queue()
  6. self.client_reader = None
  7. self.client_writer = None
  8. async def handle_client(self, reader, writer):
  9. self.client_reader = reader
  10. self.client_writer = writer
  11. while True:
  12. data = await reader.read(1024)
  13. if not data:
  14. writer.close()
  15. break
  16. await self.incoming_queue.put(data)
  17. async def handle_incoming(self, reader, writer):
  18. while True:
  19. data = await self.incoming_queue.get()
  20. writer.write(data)
  21. await writer.drain()
  22. async def handle_outgoing(self, reader, writer):
  23. while True:
  24. data = await self.outgoing_queue.get()
  25. writer.write(data)
  26. await writer.drain()
  27. async def read_from_server(self):
  28. while True:
  29. data = await self.client_reader.read(1024)
  30. if not data:
  31. self.client_writer.close()
  32. break
  33. await self.outgoing_queue.put(data)
  34. async def start_server(self):
  35. server = await asyncio.start_server(self.handle_client, host='localhost', port=8000)
  36. async with server:
  37. await server.serve_forever()
  38. async def start_incoming_server(self):
  39. server = await asyncio.start_server(self.handle_incoming, host='localhost', port=8001)
  40. async with server:
  41. await server.serve_forever()
  42. async def start_outgoing_server(self):
  43. server = await asyncio.start_server(self.handle_outgoing, host='localhost', port=8002)
  44. async with server:
  45. await server.serve_forever()
  46. async def do_async_stuff(ng):
  47. await asyncio.gather(ng.start_server(),ng.start_queue_server())
  48. async def main():
  49. """
  50. In this code, we're creating and running four separate coroutines:
  51. 1. start_server
  52. 2. start_incoming_server
  53. 3. start_outgoing_server
  54. 4. read_from_server.
  55. We're also creating separate tasks for handling incoming and
  56. outgoing data and adding them to the tasks list.
  57. """
  58. ng = NGServer()
  59. await asyncio.gather(
  60. ng.start_server(),
  61. ng.start_incoming_server(),
  62. ng.start_outgoing_server(),
  63. )
  64. """
  65. tasks = [
  66. ng.read_from_server(),
  67. ng.handle_incoming(),
  68. ng.handle_outgoing(),
  69. ]
  70. await asyncio.gather(*tasks)
  71. """
  72. def example():
  73. """ """
  74. # asyncio.run(main())