Package: binance-connector-python
Binance Public API connector PythonThis is a thin library that working as a connector to the Binance public API.aiming to support all endpointsthin layer, easy to usetest cases includedenable to change base urldisplay weight usage or whole response headerRESTful APIsfrombinance.spotimportSpotclient=Spot()print(client.time())client=Spot(key='xxx',secret='xxxxx')# get account informationprint(client.account())# post a new orderparams={'symbol':'BTCUSDT','side':'SELL','type':'LIMIT','timeInForce':'GTC','quantity':0.002,'price':9500}response=client.new_order(**params)print(response)please findexamplesfolder for more endpointsTestnetThespot testnetis available. In order to test on testnet:frombinance.spotimportSpotasClientclient=Client(base_url='https://testnet.binance.vision')print(client.time())Without providing the base url, this connector works onapi.binance.comby default.It's recommended to allow changing the base url even in production.In some rare case, Binance may offer a backup base url.RecvWindowFrom Binance API,recvWindowis available for all endpoints require signature. By default, it's 5000ms.
You are allowed to set this parameter to any value less than 60000, number beyond this limit will receive error from Binance server.frombinance.spotimportSpotasClientclient=Client(key,secret)response=client.get_order('BTCUSDT',orderId='11',recvWindow=10000)Optional parametersFor the optional parameters in the endpoint, pass exactly the field name from API document into method. e.g# correctresponse=client.cancel_oco_order('BTCUSDT',orderListId=1)# this is incorrectresponse=client.cancel_oco_order('BTCUSDT',order_list_id=1)PEP8 suggest method name as "lowercase with words separated by underscores", but not here. Let's follow the document, copy the name from there.Timeouttimeoutis supported and recommend to set a proper time as in second. Be sure you know this value as "no bytes have been received on the underlying socket for timeout seconds".
if not set, request do not time out.frombinance.spotimportSpotasClientclient=Client(timeout=1)ProxyProxy is supported.frombinance.spotimportSpotasClientproxies={'https':'http://1.2.3.4:8080'}client=Client(proxies=proxies)Display meta infoBinance API server returns weight usage in the header of each response. This is very useful to indentify the current usage.
To reveal this value, simpily intial the client withshow_weight_usage=Trueas:frombinance.spotimportSpotasClientclient=Client(show_weight_usage=True)print(client.time())the returns will be like:{'data':{'serverTime':1587990847650},'weight_usage':{'x-mbx-used-weight':'31','x-mbx-used-weight-1m':'31'}}It's also able to print out all headers, which may be very helpful for debug:client=Client(show_header=True)print(client.time())the returns will be like:{'data':{'serverTime':1587990847650},'header':{'Context-Type':'application/json;charset=utf-8',...}}When server returnsClientError, which include the whole header information. The weight usage can be extract from it.Display logsSet log level toDEBUG, it will show request url and payload, also the response text will be logged.
Known what parameters and the values sending to server is essential during debug.Websocketfrombinance.websocket.spot.websocket_clientimportSpotWebsocketClientasWebsocketClientdefmessage_handler(message):print(message)ws_client=WebsocketClient()ws_client.start()ws_client.mini_ticker(symbol='bnbusdt',id=1,callback=message_handler,)# combine selected streamsws_client.instant_subscribe(stream=['bnbusdt@bookTicker','ethusdt@bookTicker'],callback=message_handler,)ws_client.stop()please findexamplesfolder for more websocket usages.HeartbeatServer send ping frame every 3 minutes and require to response pong within 10 minutes. This package response automatically.Testnetfrombinance.websocket.spot.websocket_clientimportSpotWebsocketClientasWebsocketClientws_client=WebsocketClient(stream_url='wss://testnet.binance.vision')Test case# if you haven't installed the packages yetpipinstall-rrequirements-test.txtpytestPython version3.6+Limitationsupport/api/*only now, but will add more endpointsFound :bug:Please open an issueFound API issueOpen a topic atBinance Developer CommunityContributioncontribution is welcome, support endpoints from:https://binance-docs.github.io/apidocs/spot/enshould pass all test casespass pep8 check, ignore E501LicenseMIT