火币网行情获取的websocket客户端

从验证结果看应该是网络关闭了,不过程序写的不错,可以作为其它websocket客户端的测试程序

# !/usr/bin/env python
# -*- coding: utf-8 -*-
# author: luhx
# https://pypi.org/project/websocket-client/
# pip install websocket-client
# 火币数据调试, 感觉这个websocket客户端很好用

from websocket import create_connection
import gzip
import time

if __name__ == '__main__':
    while(1):
        try:
            ws = create_connection("wss://api.huobipro.com/ws")
            break
        except:
            print('connect ws error,retry...')
            time.sleep(5)

    # 订阅 KLine 数据
    tradeStr="""{"sub": "market.ethusdt.kline.1min","id": "id10"}"""

    ws.send(tradeStr)
    while(1):
        compressData=ws.recv()
        result=gzip.decompress(compressData).decode('utf-8')
        if result[:7] == '{"ping"':
            ts=result[8:21]
            pong='{"pong":'+ts+'}'
            ws.send(pong)
            ws.send(tradeStr)
        else:
            print(result)

猜你喜欢

转载自www.cnblogs.com/luhouxiang/p/8982787.html