Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import asyncio
- import signal
- # Only a server on a public IP will raise the AssertionError
- SERVER_HOST = '127.0.0.1'
- SERVER_PORT = 8888
- # Number of messages sent concurrently. You may need to increase this number.
- FLOOD = 10000
- async def flooding(writer):
- writer.write(b'Flooding the world\n')
- await writer.drain()
- async def reading(reader):
- while True:
- data = await reader.read(100)
- if not data:
- break
- async def run(loop):
- reader, writer = await asyncio.open_connection(
- SERVER_HOST, SERVER_PORT, loop=loop)
- for signame in ('SIGINT', 'SIGTERM'):
- signum = getattr(signal, signame)
- loop.add_signal_handler(signum, writer.close)
- reading_fut = loop.create_task(reading(reader))
- futs = [loop.create_task(flooding(writer)) for _ in range(FLOOD)]
- await asyncio.wait(futs, loop=loop)
- writer.close()
- await reading_fut
- if __name__ == '__main__':
- loop = asyncio.get_event_loop()
- loop.run_until_complete(run(loop))
- loop.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement