SHARE
TWEET

basicasyncserver

Dmnd98yt Jun 12th, 2019 70 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import asyncio
  2. import aiohttp
  3.  
  4. async def handle_echo(reader, writer):
  5.     data = await reader.read(100)
  6.     message = data.decode()
  7.     addr = writer.get_extra_info('peername')
  8.     print("Received %r from %r" % (message, addr))
  9.  
  10.     print("Send: %r" % message)
  11.     writer.write(data)
  12.     await writer.drain()
  13.  
  14.     print("Close")
  15.     writer.close()
  16.  
  17. loop = asyncio.get_event_loop()
  18. coro = asyncio.start_server(handle_echo, '127.0.0.1', int(1234), loop=loop)
  19. server = loop.run_until_complete(coro)
  20.  
  21. print('Serving on {}'.format(server.sockets[0].getsockname()))
  22. try:
  23.     loop.run_forever()
  24. except KeyboardInterrupt:
  25.     pass
  26.  
  27. server.close()
  28. loop.run_until_complete(server.wait_closed())
  29. loop.close()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top