t2.py 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. #!/usr/bin/env python
  2. # -*- encoding: utf-8 -*-
  3. '''
  4. @File : t2.py
  5. @Time : 2019/05/28 03:11:04
  6. @Author : Liuyuqi
  7. @Version : 1.0
  8. @Contact : liuyuqi.gov@msn.cn
  9. @License : (C)Copyright 2019
  10. @Desc : None
  11. '''
  12. import asyncio
  13. import logging
  14. from datetime import datetime
  15. from aiowebsocket.converses import AioWebSocket
  16. async def startup(uri):
  17. async with AioWebSocket(uri) as aws:
  18. converse = aws.manipulator
  19. message = b'AioWebSocket - Async WebSocket Client'
  20. while True:
  21. await converse.send(message)
  22. print('{time}-Client send: {message}'.format(
  23. time=datetime.now().strftime('%Y-%m-%d %H:%M:%S'),
  24. message=message))
  25. mes = await converse.receive()
  26. print('{time}-Client receive: {rec}'.format(
  27. time=datetime.now().strftime('%Y-%m-%d %H:%M:%S'), rec=mes))
  28. if __name__ == '__main__':
  29. remote = 'ws://echo.websocket.org'
  30. try:
  31. asyncio.get_event_loop().run_until_complete(startup(remote))
  32. except KeyboardInterrupt as exc:
  33. logging.info('Quit.')