鱼C论坛

 找回密码
 立即注册
查看: 2540|回复: 3

有没有大佬帮我解释下这个代码

[复制链接]
发表于 2021-1-5 13:11:13 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
  1. from websocket import create_connection
  2. import gzip
  3. import time
  4. import json
  5. from concurrent.futures import ThreadPoolExecutor



  6. def get_ws_clinet():

  7.     while True:
  8.         try:
  9.             ws = create_connection("wss://api.huobipro.com/ws")
  10.             return ws
  11.         except Exception as e:
  12.             print('connect ws error,retry...')
  13.             time.sleep(5)


  14. def get_subscribe_data(trade):

  15.     name = trade['sub'].split(".")[1]
  16.     ws = get_ws_clinet()

  17.     trade_json = json.dumps(trade)

  18.     ws.send(trade_json)

  19.     while True:
  20.         compressData = ws.recv()
  21.         result_json = gzip.decompress(compressData).decode('utf-8')

  22.         result = json.loads(result_json)

  23.         # {"ch":"market.btcusdt.kline.1min","ts":1609249181634,"tick":{"id":1609249140,"open":26846.04,"close":26865.54,"low":26844.23,"high":26865.55,"amount":4.39722167701809,"vol":118064.40113187107,"count":280}}

  24.         data = result.get("tick", {})

  25.         # 如果没有数据,表示websocket服务主动断开了连接,所以要保持心跳
  26.         if not data:
  27.             # 获取服务器发过来的心跳时间戳
  28.             ts = result.get("pong", None) or result.get("ts", None)

  29.             if not ts:
  30.                 # 重新发起订阅请求
  31.                 ws.send(trade_json)
  32.                 continue
  33.             else:
  34.                 # 响应服务器心跳
  35.                 ws.send(json.dumps({"ping": result.get("ts")}))
  36.                 continue

  37.         # print(data)

  38.         bt_close = data.get("close")
  39.         #a = data[1]


  40.         print(name, '-----------',bt_close,type(bt_close))
  41.         a = open("G:\lianghua\lianghua1.txt", 'w')
  42.         a.write(name, '-----------',bt_close)
  43.         a.close()




  44. if __name__ == '__main__':

  45.     # ws_client = get_ws_clinet()
  46.     #
  47.     # get_subscribe_data(ws_client)
  48.     trades = [
  49.         {"sub": "market.btcusdt.kline.1min", "id": "id10"},
  50.         {"sub": "market.ethusdt.kline.1min", "id": "id10"},
  51.         {"sub": "market.linkusdt.kline.1min", "id": "id10"},
  52.         {"sub": "market.bchusdt.kline.1min", "id": "id10"},
  53.         {"sub": "market.ltcusdt.kline.1min", "id": "id10"},
  54.         {"sub": "market.bsvusdt.kline.1min", "id": "id10"},
  55.         {"sub": "market.bsvusdt.kline.1min", "id": "id10"},
  56.         {"sub": "market.adausdt.kline.1min", "id": "id10"},
  57.         {"sub": "market.eosusdt.kline.1min", "id": "id10"},
  58.     ]

  59.     excuter = ThreadPoolExecutor()

  60.     for trade in trades:
  61.         excuter.submit(get_subscribe_data, trade)

复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2021-1-5 13:12:03 | 显示全部楼层
最好是一步步解释下,谢谢!
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-1-5 15:50:08 From FishC Mobile | 显示全部楼层
大佬,出来帮我解答下,感谢,感谢!
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-1-5 20:43:33 From FishC Mobile | 显示全部楼层
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-29 08:15

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表