Advertisement
Guest User

Untitled

a guest
Mar 28th, 2017
504
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.05 KB | None | 0 0
  1. import asyncio
  2. import signal
  3.  
  4.  
  5. # Only a server on a public IP will raise the AssertionError
  6. SERVER_HOST = '127.0.0.1'
  7. SERVER_PORT = 8888
  8. # Number of messages sent concurrently. You may need to increase this number.
  9. FLOOD = 10000
  10.  
  11.  
  12. async def flooding(writer):
  13.     writer.write(b'Flooding the world\n')
  14.     await writer.drain()
  15.  
  16.  
  17. async def reading(reader):
  18.     while True:
  19.         data = await reader.read(100)
  20.         if not data:
  21.             break
  22.  
  23.  
  24. async def run(loop):
  25.     reader, writer = await asyncio.open_connection(
  26.         SERVER_HOST, SERVER_PORT, loop=loop)
  27.  
  28.     for signame in ('SIGINT', 'SIGTERM'):
  29.         signum = getattr(signal, signame)
  30.         loop.add_signal_handler(signum, writer.close)
  31.  
  32.     reading_fut = loop.create_task(reading(reader))
  33.  
  34.     futs = [loop.create_task(flooding(writer)) for _ in range(FLOOD)]
  35.     await asyncio.wait(futs, loop=loop)
  36.     writer.close()
  37.  
  38.     await reading_fut
  39.  
  40.  
  41. if __name__ == '__main__':
  42.     loop = asyncio.get_event_loop()
  43.     loop.run_until_complete(run(loop))
  44.     loop.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement