您的位置:首页 > 其它

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

2018-05-02 22:07 316 查看

从验证结果看应该是网络关闭了,不过程序写的不错,可以作为其它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)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: