-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathWebsocketClient.py
More file actions
98 lines (76 loc) · 2.75 KB
/
WebsocketClient.py
File metadata and controls
98 lines (76 loc) · 2.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
""" 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)
access_key = "<YOUR-ACCESS-KEY>"
secret_key = "<YOUE-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('websocket.btcc.com', 80)
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=2000000)
namespace.disconnect()