· 6 years ago · Aug 12, 2019, 08:50 AM
1import hashlib
2import hmac
3import json
4import threading
5import time
6import traceback
7
8import websocket
9
10NAME = 'Bibox'
11
12secret_key = 'your_secret_key'
13api_key = 'your_api_key'
14
15
16def ping():
17 while True:
18 ws.send('{"ping": ' + str(int(time.time())) + '}')
19 time.sleep(45)
20
21
22def on_message(ws, message):
23 print(message)
24
25
26def on_error(ws, error):
27 print(f'ws{NAME}: {error}')
28
29
30def on_close(ws):
31 print("### closed ###")
32
33
34def on_open(ws):
35 print('### opened ###')
36
37 data = {
38 "apikey": api_key,
39 "channel": "bibox_sub_spot_ALL_ALL_login",
40 "event": "addChannel"
41 }
42 sign = str(hmac.new(secret_key.encode(),
43 json.dumps(data).replace(' ', '').encode('utf-8'),
44 hashlib.md5).hexdigest())
45 data.update({"sign": sign})
46 ws.send(json.dumps(data))
47
48 th = threading.Thread(target=ping)
49 th.start()
50
51
52if __name__ == "__main__":
53 try:
54 while True:
55 print(f'ws{NAME}Balance: start')
56
57 ws = websocket.WebSocketApp(
58 "wss://push.bibox.com/",
59 on_open=on_open,
60 on_message=on_message,
61 on_error=on_error,
62 on_close=on_close)
63
64 ws.run_forever()
65
66 print(f'ws{NAME}Balance: restart')
67 time.sleep(1)
68 except KeyboardInterrupt:
69 exit()
70 except:
71 print(traceback.format_exc())