1515class Ws :
1616 def __init__ (self , host : str , path : str , sub_str : dict , call_back_fun ,
1717 access_key : str = None , secret_key : str = None ,
18- auto_reconnect = True ):
18+ auto_reconnect = True , be_spot_account = False ):
1919 self .__host = host
2020 self .__path = path
2121 self .__url = 'wss://{}{}' .format (self .__host , self .__path )
@@ -24,6 +24,7 @@ def __init__(self, host: str, path: str, sub_str: dict, call_back_fun,
2424 self .__access_key = access_key
2525 self .__secret_key = secret_key
2626 self .__auto_reconnect = auto_reconnect
27+ self .__be_spot_account = be_spot_account
2728
2829 self .__ws = None
2930 self .__be_open = False
@@ -54,24 +55,42 @@ def __send_auth_data(self, method: str, host: str, path: str, access_key: str, s
5455 timestamp = datetime .utcnow ().strftime ("%Y-%m-%dT%H:%M:%S" )
5556
5657 # get Signature
57- suffix = 'AccessKeyId={}&SignatureMethod=HmacSHA256&SignatureVersion=2&Timestamp={}' .format (
58- access_key , parse .quote (timestamp ))
58+ if self .__be_spot_account :
59+ suffix = 'accessKey={}&signatureMethod=HmacSHA256&signatureVersion=2.1×tamp={}' .format (
60+ access_key , parse .quote (timestamp ))
61+ else :
62+ suffix = 'AccessKeyId={}&SignatureMethod=HmacSHA256&SignatureVersion=2&Timestamp={}' .format (
63+ access_key , parse .quote (timestamp ))
5964 payload = '{}\n {}\n {}\n {}' .format (method .upper (), host , path , suffix )
6065
6166 digest = hmac .new (secret_key .encode ('utf8' ), payload .encode (
6267 'utf8' ), digestmod = sha256 ).digest ()
6368 signature = base64 .b64encode (digest ).decode ()
6469
6570 # data
66- data = {
67- "op" : "auth" ,
68- "type" : "api" ,
69- "AccessKeyId" : access_key ,
70- "SignatureMethod" : "HmacSHA256" ,
71- "SignatureVersion" : "2" ,
72- "Timestamp" : timestamp ,
73- "Signature" : signature
74- }
71+ if self .__be_spot_account :
72+ data = {
73+ "action" : "req" ,
74+ "ch" : "auth" ,
75+ "params" : {
76+ "authType" : "api" ,
77+ "accessKey" : access_key ,
78+ "signatureMethod" : "HmacSHA256" ,
79+ "signatureVersion" : "2.1" ,
80+ "timestamp" : timestamp ,
81+ "signature" : signature
82+ }
83+ }
84+ else :
85+ data = {
86+ "op" : "auth" ,
87+ "type" : "api" ,
88+ "AccessKeyId" : access_key ,
89+ "SignatureMethod" : "HmacSHA256" ,
90+ "SignatureVersion" : "2" ,
91+ "Timestamp" : timestamp ,
92+ "Signature" : signature
93+ }
7594 data = json .dumps (data )
7695 self .__ws .send (data )
7796 logger .info ('send data: {}' .format (data ))
@@ -89,11 +108,35 @@ def __on_open(self, ws):
89108 logger .info ('send data: {}' .format (data ))
90109
91110 def __on_msg (self , ws , message ):
92- plain = gzip .decompress (message ).decode ()
111+ if self .__be_spot_account :
112+ plain = message
113+ else :
114+ plain = gzip .decompress (message ).decode ()
93115 jdata = json .loads (plain )
94116 if 'ping' in jdata :
95117 sdata = plain .replace ('ping' , 'pong' )
118+ # logger.info(sdata)
96119 self .__ws .send (sdata )
120+ elif 'action' in jdata :
121+ act = jdata ['action' ]
122+ if act == 'ping' :
123+ sdata = plain .replace ('ping' , 'pong' )
124+ # logger.info(sdata)
125+ self .__ws .send (sdata )
126+ elif act == 'req' :
127+ logger .info (plain )
128+ if 'ch' in jdata and jdata ['ch' ] == 'auth' and jdata ['code' ] == 200 :
129+ if self .__sub_str is not None :
130+ data = json .dumps (self .__sub_str )
131+ self .__ws .send (data )
132+ logger .info ('send data: {}' .format (data ))
133+ elif act == 'push' :
134+ if self .__call_back_fun is not None :
135+ self .__call_back_fun (jdata )
136+ elif act == 'sub' or act == 'unsub' :
137+ logger .info (plain )
138+ else :
139+ logger .error ('unknow data: {}' .format (jdata ))
97140 elif 'op' in jdata :
98141 opdata = jdata ['op' ]
99142 if opdata == 'ping' :
0 commit comments