因为 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. PAT (Advanced Level) 1038. Recover the Smallest Number (30)

    注意前导零的消去. #include <iostream> #include <string> #include <sstream> #include <al ...

  2. 洛谷 P3879 [TJOI2010]阅读理解

    P3879 [TJOI2010]阅读理解 题目描述 英语老师留了N篇阅读理解作业,但是每篇英文短文都有很多生词需要查字典,为了节约时间,现在要做个统计,算一算某些生词都在哪几篇短文中出现过. 输入输出 ...

  3. HttpUtils 用于进行网络请求的工具类

    原文:http://www.open-open.com/code/view/1437537162631 import java.io.BufferedReader; import java.io.By ...

  4. Meteor计时器

    Meteor有提供它自己的setTimeout和setInterval方法.这些方法被用于确保所有全局变量都具有正确的值.它们就像普通 JavaScript 中的setTimeout 和 setInt ...

  5. [Javascript] Use JavaScript's for-in Loop on Objects with Prototypes

    Loops can behave differently when objects have chained prototype objects. Let's see the difference w ...

  6. 九度OJ1004 Median

    题目描写叙述: Given an increasing sequence S of N integers, the median is the number at the middle positio ...

  7. soapUI系列之—-06 testrunner实现自动化测试

    TestRunner为soapUI自带------testrunner.bat/testrunner.sh 实现步骤: 1. 使用soapUI,针对接口文件创建测试用例. 2. 将测试用例保存至本地, ...

  8. AnkhSVN介绍

    AnkhSVN介绍 Posted on 2012-11-15 23:24 ArRan 阅读(3120) 评论(1) 编辑 收藏 AnkhSVN是一款在VS中管理Subversion的插件,您可以在VS ...

  9. Android 性能优化探究

    使用ViewStub动态载入布局.避免一些不常常的视图长期握住引用: ViewStub的一些特点: 1. ViewStub仅仅能Inflate一次,之后ViewStub对象被置空:某个被ViewStu ...

  10. thinkphp3.2.3 数据库写入add 方法的一些问题。

    最近在做项目中遇到的一个数据操作add()方法,在不开启debug的模式下会漏掉一些字段没写入数据库. 当时并不知道是这个原因,明明在开发的时候都是没问题的,怎么突然出现这个问题,找了好久都没有头绪, ...