Advertisement
Guest User

Untitled

a guest
Jun 15th, 2019
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.85 KB | None | 0 0
  1. import asyncio
  2.  
  3. async def connect():
  4. print('connecting...')
  5. reader, writer = await asyncio.open_connection('127.0.0.1', 8888)
  6. writer.write(b'hello world')
  7. data = await reader.read(100)
  8. assert data == b'hello world'
  9. writer.close()
  10. await writer.wait_closed()
  11. print('closed connection')
  12. return data
  13.  
  14. async def handle_client(reader, writer):
  15. print('incoming connection')
  16. try:
  17. while True:
  18. data = await reader.read(100)
  19. if data == b'':
  20. return
  21. writer.write(data)
  22. await writer.drain()
  23. finally:
  24. print('incoming connection closed')
  25.  
  26. async def main():
  27. server = await asyncio.start_server(handle_client, '127.0.0.1', 8888)
  28. print('server now set up')
  29. await connect()
  30. server.close()
  31. await server.wait_closed()
  32.  
  33. asyncio.run(main())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement