""" An example for Python Socket.io Client
Requires: six,socketIO_client
"""
from socketIO_client import SocketIO, BaseNamespace
import json
import time
import re
import hmac
import hashlib
import base64
import logging
logging.getLogger('socketIO-client').setLevel(logging.DEBUG)
logging.basicConfig()
access_key = ""
secret_key = “"
def get_tonce():
return int(time.time() * 1000000)
def get_postdata():
post_data = {}
tonce = get_tonce()
post_data['tonce'] = tonce
post_data['accesskey'] = access_key
post_data['requestmethod'] = 'post'
if 'id' not in post_data:
post_data['id'] = tonce
#modefy here to meet your requirement
post_data['method'] = 'subscribe'
post_data['params'] = ['order_cnybtc', 'order_cnyltc', 'account_info']
return post_data
def get_sign(pdict):
pstring = ''
fields = ['tonce', 'accesskey', 'requestmethod', 'id', 'method', 'params']
for f in fields:
if pdict[f]:
if f == 'params':
param_string=str(pdict[f])
param_string=param_string.replace('None', '')
param_string=re.sub("[\[\] ]","",param_string)
param_string=re.sub("'",'',param_string)
pstring+=f+'='+param_string+'&'
else:
pstring+=f+'='+str(pdict[f])+'&'
else:
pstring+=f+'=&'
pstring=pstring.strip('&')
phash = hmac.new(secret_key, pstring, hashlib.sha1).hexdigest()
return base64.b64encode(access_key + ':' + phash)
class Namespace(BaseNamespace):
def on_connect(self):
print('[Connected]')
def on_disconnect(self):
print('[Disconnect]')
def on_ticker(self, *args):
print('ticker', args)
def on_trade(self, *args):
print('trade', args)
def on_grouporder(self, *args):
print('grouporder', args)
def on_order(self, *args):
print('order', args)
def on_account_info(self, *args):
print('account_info', args)
def on_message(self, *args):
print('message', args)
def on_error(self, data):
print(data)
socketIO = SocketIO('https://websocket.btcchina.com')
namespace = socketIO.define(Namespace)
namespace.emit('subscribe', 'marketdata_cnybtc')
namespace.emit('subscribe', 'marketdata_cnyltc')
namespace.emit('subscribe', 'grouporder_cnybtc')
namespace.emit('subscribe', 'grouporder_cnyltc')
payload = get_postdata()
arg = [json.dumps(payload), get_sign(payload)]
namespace.emit('private', arg)
socketIO.wait(seconds=1)
namespace.disconnect()
当我运行 namespace.emit('subscribe', 'marketdata_cnybtc')
namespace.emit('subscribe', 'marketdata_cnyltc')
namespace.emit('subscribe', 'grouporder_cnybtc')
namespace.emit('subscribe', 'grouporder_cnyltc')
这个后,发现根本没有返回东西
/home/daneel/anaconda3/envs/btchina/bin/python /home/daneel/PycharmProjects/btchina/WebsocketClient.py
DEBUG:socketIO-client:websocket.btcchina.com:443/socket.io [engine.io transport selected] websocket
DEBUG:socketIO-client:websocket.btcchina.com:443/socket.io [engine.io heartbeat reset]
DEBUG:socketIO-client:websocket.btcchina.com:443/socket.io [socket.io packet sent] 2["subscribe", "marketdata_cnybtc"]
DEBUG:socketIO-client:websocket.btcchina.com:443/socket.io [socket.io packet sent] 2["subscribe", "marketdata_cnyltc"]
DEBUG:socketIO-client:websocket.btcchina.com:443/socket.io [socket.io packet sent] 2["subscribe", "grouporder_cnybtc"]
DEBUG:socketIO-client:websocket.btcchina.com:443/socket.io [socket.io packet sent] 2["subscribe", "grouporder_cnyltc"]
DEBUG:socketIO-client:websocket.btcchina.com:443/socket.io [socket.io packet sent] 2["private", ["{\"accesskey\": \"safadfasafa\", \"id\": 1495691413689407, \"requestmethod\": \"post\", \"tonce\": 1495691413689407, \"params\": [\"order_cnybtc\", \"order_cnyltc\", \"account_info\"], \"method\": \"subscribe\"}", "fdsafdsdafdfafdasfsafasdfas="]]
DEBUG:socketIO-client:websocket.btcchina.com:443/socket.io [socket.io packet received] 0
DEBUG:socketIO-client:websocket.btcchina.com:443/socket.io [socket.io connected]
('message', ('0',))
[Connected]
[Disconnect]
这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。
V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。
V2EX is a community of developers, designers and creative people.