火幣網行情獲取的websocket客戶端

從驗證結果看應該是網絡關閉了,不過程序寫的不錯,能夠做爲其它websocket客戶端的測試程序python

 

# !/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)
相關文章
相關標籤/搜索