python 调用okex api_OKEX websocket API 连接Python范例

因为 websocket-client 新版的各种大脑降级设计 很多功能无法使用

需要安装老版本websocket-client的包才能正常使用 pip3 install websocket-client==0.46.0

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)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值