kuaishou_socket.py 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. #!/usr/bin/env python
  2. # -*- encoding: utf-8 -*-
  3. '''
  4. @File : kuaishou_socket.py
  5. @Time : 2019/05/28 03:00:17
  6. @Author : Liuyuqi
  7. @Version : 1.0
  8. @Contact : liuyuqi.gov@msn.cn
  9. @License : (C)Copyright 2019
  10. @Desc : None
  11. Request URL: wss://live-ws-pg-group2.kuaishou.com/websocket
  12. Request Method: GET
  13. Status Code: 101 Switching Protocols
  14. Accept-Encoding: gzip, deflate, br
  15. Accept-Language: zh-CN,zh;q=0.9,en;q=0.8
  16. Cache-Control: no-cache
  17. Connection: Upgrade
  18. Host: live-ws-pg-group2.kuaishou.com
  19. Origin: https://live.kuaishou.com
  20. Pragma: no-cache
  21. Sec-WebSocket-Extensions: permessage-deflate; client_max_window_bits
  22. Sec-WebSocket-Key: opUwBCN/VZdDkpQSokZJIQ==
  23. Sec-WebSocket-Version: 13
  24. Upgrade: websocket
  25. User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36
  26. '''
  27. import asyncio
  28. import logging
  29. from datetime import datetime
  30. from aiowebsocket.converses import AioWebSocket
  31. # url = 'wss://api.bbxapp.vip/v1/ifcontract/realTime'
  32. url = 'wss://live-ws-pg-group2.kuaishou.com/websocket'
  33. async def startup(uri):
  34. async with AioWebSocket(uri) as aws:
  35. converse = aws.manipulator
  36. # 客户端给服务端发送消息
  37. await converse.send('{"action":"subscribe","args":["QuoteBin5m:14"]}')
  38. while True:
  39. mes = await converse.receive()
  40. print('{time}-Client receive: {rec}'.format(
  41. time=datetime.now().strftime('%Y-%m-%d %H:%M:%S'), rec=mes))
  42. if __name__ == '__main__':
  43. try:
  44. asyncio.get_event_loop().run_until_complete(startup(url))
  45. except KeyboardInterrupt as exc:
  46. logging.info('Quit.')