sssocket.py 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. #!/usr/bin/env python
  2. # -*- encoding: utf-8 -*-
  3. '''
  4. @File : socket.py
  5. @Time : 2019/05/28 02:14:15
  6. @Author : Liuyuqi
  7. @Version : 1.0
  8. @Contact : liuyuqi.gov@msn.cn
  9. @License : (C)Copyright 2019
  10. @Desc : None
  11. pip install asyncio
  12. pip install aiowebsocket
  13. '''
  14. import asyncio
  15. import logging
  16. from datetime import datetime
  17. from aiowebsocket.converses import AioWebSocket
  18. async def startup(uri):
  19. async with AioWebSocket(uri) as aws:
  20. converse = aws.manipulator
  21. # 客户端给服务端发送消息
  22. await converse.send('{"action":"subscribe","args":["QuoteBin5m:14"]}')
  23. while True:
  24. mes = await converse.receive()
  25. print('{time}-Client receive: {rec}'.format(
  26. time=datetime.now().strftime('%Y-%m-%d %H:%M:%S'), rec=mes))
  27. if __name__ == '__main__':
  28. url = 'wss://api.bbxapp.vip/v1/ifcontract/realTime'
  29. try:
  30. asyncio.get_event_loop().run_until_complete(startup(url))
  31. except KeyboardInterrupt as exc:
  32. logging.info('Quit.')