OKEX websocket API 连接Python范例
因为 websocket-client 新版的各种大脑降级设计 很多功能无法使用
需要安装老版本websocket-client的包才能正常使用 pip3 install websocket-client==0.46.0
代码复制地址:https://www.fmz.com/strategy/143457
Python源码:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# encoding: utf-8 import time
import ssl
import sys
import code
import json
import hashlib
import hmac
import urllib
import threading
import websocket
import zlib
import string try:
import readline
except ImportError:
pass pong = time.time() class WSSubscription: def __init__(self, instrument_id='BTC-USD-190517', market='futures', on_message=None):
self.__iid = instrument_id
self.__market = market
self.__Depth = {} if on_message is not None:
self.__callbackEnabled = True
self.__callback = on_message
else:
self.__callbackEnabled = False thread = threading.Thread(target=self.sub, args=())
thread.daemon = True
thread.start() def GetDepth(self):
return self.__Depth def subscribe(self, ws): def operator(op, args):
message = {
'op': op,
'args': args
}
ws.send(json.dumps(message)) def run(*args):
operator('subscribe', ['%s/depth5:%s' % (self.__market, self.__iid)])
operator('subscribe', ['%s/trade:%s' % (self.__market, self.__iid)]) while True:
ws.send("ping")
time.sleep(30) threading.Thread(target=run).start() def sub(self): websocket.enableTrace(False)
URL = "wss://real.okex.com:10442/ws/v3"
ws = websocket.WebSocketApp(URL,
on_message=self.incoming,
on_error=self.error_handling,
on_close=self.closing) ws.on_open = self.subscribe while True:
try:
ws.run_forever()
except:
pass pass def incoming(self,ws,message):
message = zlib.decompress(message, -zlib.MAX_WBITS)
message = message.decode('utf-8')
global pong
if 'pong' in message:
pong = time.time()
if 'asks' in message and 'bids' in message:
d = json.loads(message)
self.__Depth = d['data'][0] if self.__callbackEnabled:
self.__callback(message) def error_handling(self,ws,error):
print(str(error)) def closing(self,ws):
print("WebSocket Closing...") ext.OkEXWS = WSSubscription # 模块测试
def main():
OkEX = ext.OkEXWS('BTC-USD-190517', 'futures')
while (True):
Log(OkEX.GetDepth())
time.sleep(1)
OKEX websocket API 连接Python范例的更多相关文章
- Deribit交易所 websocket API 连接范例
Deribit websocket API 连接范例,使用JavaScript语言,策略运行在FMZ发明者量化平台. 源码地址:https://www.fmz.com/strategy/147765 ...
- WebSocket 介绍(二)-WebSocket API
这一章介绍如何用WebSocket API来控制协议和创建应用,运用http://websocket.org 提供的现有WebSocket服务器,我们可以收发消息.创建一些简单的WebSocket应用 ...
- WebSocket API简介
WebSocket是html5新增加的一种通信协议,目前流行的浏览器都支持这个协议,例如Chrome,Safari,Firefox,Opera,IE等等,对该协议支持最早的应该是chrome,从chr ...
- WebSocket API
WebSocket API 这一章介绍如何用WebSocket API来控制协议和创建应用,运用http://websocket.org 提供的现有WebSocket服务器,我们可以收发消息.创建一些 ...
- Jetty开发指导:Jetty Websocket API
Jetty WebSocket API使用 Jetty提供了功能更强的WebSocket API,使用一个公共的核心API供WebSockets的服务端和client使用. 他是一个基于WebSock ...
- Django websocket 长连接使用
下载 pip install dwebsocket WebSocket是一种在单个TCP连接上进行全双工通信的协议 WebSocket使得客户端和服务器之间的数据交换变得更加简单,允许服务端主动向客 ...
- 火币网API文档——WebSocket API简介
WebSocket API简介 WebSocket协议是基于TCP的一种新的网络协议.它实现了客户端与服务器之间在单个 tcp 连接上的全双工通信,由服务器主动发送信息给客户端,减少了频繁的身份验证等 ...
- 火币网API文档——WebSocket API Reference
订阅 KLine 数据 market.$symbol.kline.$period 成功建立和 WebSocket API 的连接之后,向 Server 发送如下格式的数据来订阅数据: { " ...
- WebSocket API使用篇检测浏览器是否支持WebSocket(4)
WebSocket API是下一代客户端-服务器的异步通信方法.前面有三篇文章已经对WebSocket有了一些介绍,这里我总结了一下.我在使用WebSockets API过程中遇到的问题. 1.检测浏 ...
随机推荐
- POJ 1094 Sorting It All Out【拓扑排序】
题目链接: http://poj.org/problem?id=1094 题意: 给定前n个字母的大小关系,问你是否 根据前xxx个关系得到上升序列 所有关系都无法确定唯一的一个序列 第xxx个关系导 ...
- Spring MVC表单实例
以下内容引用自http://wiki.jikexueyuan.com/project/spring/mvc-framework/spring-mvc-form-handling-example.htm ...
- JAVA_the user operation is waiting怎么办
彻底解决 MyEclipse出现the user operation is waiting的问题 2011-05-31 10:32:30| 分类: 软件编程 | 标签:java myecli ...
- nyoj473 A^B Problem (高速幂)
题目473 题目信息 执行结果 pid=473" style="text-decoration:none; color:rgb(55,119,188)">本题排行 ...
- python3 base64模块代码分析
#! /usr/bin/env python3 """Base16, Base32, Base64 (RFC 3548), Base85 and Ascii85 data ...
- C项目实践之通讯录管理案例
1.功能需求分析 通讯录管理案例主要实现对联系人的信息进行添加.显示.查找.删除.更新和保存功能.主要功能需求描述如下: (1)系统主控平台: 充许用户选择想要进行的操作,包括添加联系人信息,显示.查 ...
- ps -ef | grep
ps -ef | grep java 查看所有关于java的进程 root 17540 1 0 2009 ? 01:42:27 /usr/java/jdk1.5. ...
- 【剑指offer】面试题42:单词翻转顺序&左右旋转字符串
这里尽可能的不去用语言本身提供的函数. 将string逆置 def reverse(string): #return string[::-1] reversedStr = '' for i in xr ...
- oracle安装登录sqlplus / as sysdba然后报错ERROR: ORA-01031 insufficient privileges
解决办法: 一般情况下检查操作系统的登录用户是否包含在ORA_DBA组中. 控制面板->管理工具->计算机管理->系统工具->本地用户和组->ORA_DBA组. 如果OR ...
- XMU 1606 nc与滴水问题 【模拟】
1606: nc与滴水问题 Time Limit: 1000 MS Memory Limit: 64 MBSubmit: 85 Solved: 27[Submit][Status][Web Boa ...