├── README.md └── WebsocketClient.py /README.md: -------------------------------------------------------------------------------- 1 | Simple example code for btcchina websocket,written in python, compiled in python 2.7.x 2 | 3 | ============================= 4 | -------------------------------------------------------------------------------- /WebsocketClient.py: -------------------------------------------------------------------------------- 1 | """ An example for Python Socket.io Client 2 | Requires: six,socketIO_client 3 | """ 4 | from socketIO_client import SocketIO, BaseNamespace 5 | import json 6 | import time 7 | import re 8 | import hmac 9 | import hashlib 10 | import base64 11 | 12 | import logging 13 | logging.getLogger('socketIO-client').setLevel(logging.DEBUG) 14 | 15 | 16 | access_key = "" 17 | secret_key = "" 18 | 19 | def get_tonce(): 20 | return int(time.time() * 1000000) 21 | 22 | def get_postdata(): 23 | post_data = {} 24 | tonce = get_tonce() 25 | post_data['tonce'] = tonce 26 | post_data['accesskey'] = access_key 27 | post_data['requestmethod'] = 'post' 28 | 29 | if 'id' not in post_data: 30 | post_data['id'] = tonce 31 | 32 | #modefy here to meet your requirement 33 | post_data['method'] = 'subscribe' 34 | post_data['params'] = ['order_cnybtc', 'order_cnyltc', 'account_info'] 35 | return post_data 36 | 37 | def get_sign(pdict): 38 | pstring = '' 39 | fields = ['tonce', 'accesskey', 'requestmethod', 'id', 'method', 'params'] 40 | for f in fields: 41 | if pdict[f]: 42 | if f == 'params': 43 | param_string=str(pdict[f]) 44 | param_string=param_string.replace('None', '') 45 | param_string=re.sub("[\[\] ]","",param_string) 46 | param_string=re.sub("'",'',param_string) 47 | pstring+=f+'='+param_string+'&' 48 | else: 49 | pstring+=f+'='+str(pdict[f])+'&' 50 | else: 51 | pstring+=f+'=&' 52 | pstring=pstring.strip('&') 53 | phash = hmac.new(secret_key, pstring, hashlib.sha1).hexdigest() 54 | 55 | return base64.b64encode(access_key + ':' + phash) 56 | 57 | class Namespace(BaseNamespace): 58 | 59 | def on_connect(self): 60 | print('[Connected]') 61 | 62 | def on_disconnect(self): 63 | print('[Disconnect]') 64 | 65 | def on_ticker(self, *args): 66 | print('ticker', args) 67 | 68 | def on_trade(self, *args): 69 | print('trade', args) 70 | 71 | def on_grouporder(self, *args): 72 | print('grouporder', args) 73 | 74 | def on_order(self, *args): 75 | print('order', args) 76 | 77 | def on_account_info(self, *args): 78 | print('account_info', args) 79 | 80 | def on_message(self, *args): 81 | print('message', args) 82 | 83 | def on_error(self, data): 84 | print(data) 85 | 86 | socketIO = SocketIO('websocket.btcc.com', 80) 87 | namespace = socketIO.define(Namespace) 88 | namespace.emit('subscribe', 'marketdata_cnybtc') 89 | namespace.emit('subscribe', 'marketdata_cnyltc') 90 | namespace.emit('subscribe', 'grouporder_cnybtc') 91 | namespace.emit('subscribe', 'grouporder_cnyltc') 92 | 93 | payload = get_postdata() 94 | arg = [json.dumps(payload), get_sign(payload)] 95 | namespace.emit('private', arg) 96 | socketIO.wait(seconds=2000000) 97 | namespace.disconnect() 98 | 99 | --------------------------------------------------------------------------------