因为 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. JDBC调用存储过程,进参出参

    今天做了一个数据表拷贝的功能,用到了存储过程,就写了一个java中用jdbc调用存储过程的代码,成功的实现了功能,晚上跑回家记录下 Connection conn = ConnectionUtil.g ...

  2. chapter1:using neural nets to recognize handwritten digits

    two important types of artificial neuron :the perceptron and the sigmoid neuron Perceptrons 感知机的输入个数 ...

  3. 【转】JavaScript错误处理和堆栈追踪

    原文: https://www.cnblogs.com/caoru/p/6699583.html --------------------------------------------------- ...

  4. CentOS 更改Apache默认网站目录

    http://www.osyunwei.com/archives/789.html引言:Apache默认的网站目录是在/var/www/html, 现在要把网站目录更改到/home/wwwroot/w ...

  5. [Angular] Modify User Provided UI with Angular Content Directives

    If we’re going to make our toggle accessible, we’ll need to apply certain aria attributes to the con ...

  6. android POI搜索,附近搜索,周边搜索定位介绍

    POI搜索有三种方式.依据范围和检索词发起范围检索poiSearchInbounds.城市poi检索poiSearchInCity,周边检索poiSearchNearBy. 下以周边检索为例介绍怎样进 ...

  7. udhcp详解源码(序)

    最近负责接入模块,包括dhcp.ipoe和pppoe等等.所以需要对dhcp和ppp这几个app的源代码进行一些分析.网上有比较好的文章,参考并补充自己的分析. 这篇udhcp详解是基于busybox ...

  8. jdbc 连 oracle 12c

    jdbc 连 oracle 12c,除了连接串要书写正确(如果用PDB,可插拔数据库),必要的JDBC包也是不可或缺的. 比如我,机器本身装了个oracle 10g,然后上面有个java项目,使用jd ...

  9. HDU 2457/POJ 3691 DNA repair AC自动机+DP

    DNA repair Problem Description   Biologists finally invent techniques of repairing DNA that contains ...

  10. myeclipse提示:Syntax error on tokens, delete these tokens怎么解决

    有中文字符或者符号,包括空格. 上次遇到一个问题,检查了一遍语法没错误, 后来发现是拷贝代码的时候有一部分中文空格没删除,就出现这个问题了. 一个个删除就OK了.