因为 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范例的更多相关文章

  1. Deribit交易所 websocket API 连接范例

    Deribit websocket API 连接范例,使用JavaScript语言,策略运行在FMZ发明者量化平台. 源码地址:https://www.fmz.com/strategy/147765 ...

  2. WebSocket 介绍(二)-WebSocket API

    这一章介绍如何用WebSocket API来控制协议和创建应用,运用http://websocket.org 提供的现有WebSocket服务器,我们可以收发消息.创建一些简单的WebSocket应用 ...

  3. WebSocket API简介

    WebSocket是html5新增加的一种通信协议,目前流行的浏览器都支持这个协议,例如Chrome,Safari,Firefox,Opera,IE等等,对该协议支持最早的应该是chrome,从chr ...

  4. WebSocket API

    WebSocket API 这一章介绍如何用WebSocket API来控制协议和创建应用,运用http://websocket.org 提供的现有WebSocket服务器,我们可以收发消息.创建一些 ...

  5. Jetty开发指导:Jetty Websocket API

    Jetty WebSocket API使用 Jetty提供了功能更强的WebSocket API,使用一个公共的核心API供WebSockets的服务端和client使用. 他是一个基于WebSock ...

  6. Django websocket 长连接使用

    下载  pip install dwebsocket WebSocket是一种在单个TCP连接上进行全双工通信的协议 WebSocket使得客户端和服务器之间的数据交换变得更加简单,允许服务端主动向客 ...

  7. 火币网API文档——WebSocket API简介

    WebSocket API简介 WebSocket协议是基于TCP的一种新的网络协议.它实现了客户端与服务器之间在单个 tcp 连接上的全双工通信,由服务器主动发送信息给客户端,减少了频繁的身份验证等 ...

  8. 火币网API文档——WebSocket API Reference

    订阅 KLine 数据 market.$symbol.kline.$period 成功建立和 WebSocket API 的连接之后,向 Server 发送如下格式的数据来订阅数据: { " ...

  9. WebSocket API使用篇检测浏览器是否支持WebSocket(4)

    WebSocket API是下一代客户端-服务器的异步通信方法.前面有三篇文章已经对WebSocket有了一些介绍,这里我总结了一下.我在使用WebSockets API过程中遇到的问题. 1.检测浏 ...

随机推荐

  1. P1427 小鱼的数字游戏 洛谷

    https://www.luogu.org/problem/show?pid=1427 题目描述 小鱼最近被要求参加一个数字游戏,要求它把看到的一串数字(长度不一定,以0结束,最多不超过100个,数字 ...

  2. Java 等额本金等额本息工具类

    原文:http://www.open-open.com/code/view/1449034309983 等额本息: /** * Description:等额本息工具类 * Copyright: Cop ...

  3. SD/MMC的Commands和Responses的总结

    SD总线通信是基于指令和数据比特流,起始位開始和停止位结束. SD总线通信有三个元素:1.Command:由host发送到卡设备.使用CMD线发送. 2.Response:从card端发送到host端 ...

  4. 基于51单片机的CAN通讯协议C语言程序

      //-----------------------函数声明,变量定义-------------------------------------------------------- #includ ...

  5. 【Nginx】Hello world程序

    模块如何在运行中生效 配置文件中的location块决定了匹配某种URL的请求将会由相应的HTTP模块处理,因此,运行时HTTP框架会在接收完毕HTTP请求的头部后,将请求的URL与配置文件中的所有l ...

  6. uva558 Wormholes SPFA 求是否存在负环

    J - Wormholes Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Stat ...

  7. 理解Android ANR的触发原理(转)

    一.概述 ANR(Application Not responding),是指应用程序未响应,Android系统对于一些事件需要在一定的时间范围内完成,如果超过预定时间能未能得到有效响应或者响应时间过 ...

  8. LoadRunner中存储表格参数------关联数组

    主要用到 web_reg_save_param_ex函数("Scope=All",), sprintf( CProdNo,"{CProdNo_%d}",i ); ...

  9. Android经常使用设计模式(二)

    继上一篇 Android经常使用设计模式(一)里认识了观察者.适配器.代理等三种模式,这一篇将会解说下面三种模式: 工厂模式 单例模式 命令模式 1.工厂模式(Factory Pattern) 工厂模 ...

  10. mysql11---主键普通全文索引

    .1主键索引添加 当一张表,把某个列设为主键的时候,则该列就是主键索引 create table aaa (id int unsigned primary key auto_increment , n ...