2

I am working with the Binance API. I am connecting to their API and trying to assess if Binance has a list of assets on their platform or not. The list of assets is seen below:

assets = ['tribe', 'pax']

I pass this through to their API by inserting the name of the assets into the SOCKET link:

SOCKET = f"wss://stream.binance.com:9443/ws/{asset}usdt@ticker"

I know the asset does exist on their website if on_message is called, because then I have accomplished a consistent connection with their API and it will keep printing messages unless I close the connection (which I do). However, if no message is received in n time I know they do not have the asset I am looking for. In this case Binance does have tribe, but not pax. I want to close the connection if the asset is not on their website after n time, how do I do this?

import ssl
import websocket

def on_open(ws):
    print('connection: successful')


def on_close(ws, *args):
    print('connection: lost')
    print("---------------------------------------------------")
    ws.close()


def on_message(ws, message):
    print("message received")
    print()
    ws.close()


def on_error(ws, message):
    print(message)
    print()


assets = ['tribe', 'pax']
for asset in assets:
    print(asset)
    SOCKET = f"wss://stream.binance.com:9443/ws/{asset}usdt@ticker"


    ws = websocket.WebSocketApp(SOCKET, on_open=on_open, on_close=on_close, on_message=on_message,
                                on_error=on_error)

    ws.run_forever(sslopt={"cert_reqs": ssl.CERT_NONE})

I have attempted to write:

if on_message == False:
    ws.close()

however this does not work because on_message is not even being called as far as my knowledge goes.

Here is the Binance API documentation: https://github.com/binance/binance-spot-api-docs/blob/master/web-socket-streams.md#klinecandlestick-streams Here is the websocket-client documentation: https://websocket-client.readthedocs.io/en/latest/examples.html

4
  • There's nothing in the documentation to suggest that this is possible. You could probably run some kind of monitor thread but it would be a bit of a kludge
    – user2668284
    Sep 21, 2021 at 12:18
  • Do you know any other libraries that would allow me to do just that?
    – Arizona
    Sep 21, 2021 at 12:23
  • Yes - the threading module. Also, note that your for loop will not iterate as you might hope because if a successful connection is made then run_forever will effectively block
    – user2668284
    Sep 21, 2021 at 12:38
  • I just noticed something... Do you really want to close the websocket as soon as you've received one message?
    – user2668284
    Sep 21, 2021 at 16:06

3 Answers 3

1

Try this:-

import websocket
import ssl
import time
from threading import Thread


class Binance():
    def __init__(self, asset, timeout=5):
        self.url = f'wss://stream.binance.com:9443/ws/{asset}usdt@ticker'
        self.ws = None
        self.mr = False
        self.timeout = timeout

    def start(self):
        self.ws = websocket.WebSocketApp(self.url, on_message=self.on_message)
        Thread(target=self.monitor).start()
        self.ws.run_forever(sslopt={"cert_reqs": ssl.CERT_NONE})

    def on_message(self, ws, message):
        self.mr = True
        print(message)

    def stop(self):
        if self.ws:
            self.ws.close()
            self.ws = None

    def monitor(self):
        while True:
            time.sleep(self.timeout)
            if not self.mr:
                self.stop()
                break
            self.mr = False


def runner(asset):
    Binance(asset).start()


for asset in ['pax', 'tribe']:
    Thread(target=runner, args=(asset,)).start()
2
  • This works, however, if I add more items to the list it breaks :/
    – Arizona
    Sep 21, 2021 at 15:52
  • In what way does it break? What assets are you trying to add?
    – user2668284
    Sep 21, 2021 at 15:57
0

This is rough, but it works


import ssl
import websocket
import requests
import json
import pprint

def on_open(ws):
    print('connection: successful')


def on_close(ws, *args):
    print('connection: lost')
    print()
    ws.close()


def on_message(ws, message):
    json_message = json.loads(message)
    pprint.pprint(json_message)
    ws.close()


def on_error(ws, message):
    print("error")
    print()


assets = ['TRIBE', 'PAX', 'OGN', 'RAI', 'QNT', 'BTC']
for asset in assets:
    url = f"https://www.binance.com/en/trade/{asset}_USDT"
    soup = requests.get(url).text

    if "https://bin.bnbstatic.com/static/images/electron/404-error.png" in soup:
        print(f"{asset} not on coinbase")


    else:
        print(asset, "on coinbase")
        SOCKET = f"wss://stream.binance.com:9443/ws/{asset.lower()}usdt@ticker"
        print(SOCKET)

        ws = websocket.WebSocketApp(SOCKET, on_open=on_open, on_close=on_close, on_message=on_message, on_error=on_error)
        ws.run_forever(sslopt={"cert_reqs": ssl.CERT_NONE})

    print("------------------------------")
    print()


Also, this does not use the method you were thinking of but it does work like you want it too (I think).

3
  • 1
    This works exactly like I want it too, thank you :)
    – Arizona
    Sep 21, 2021 at 15:38
  • But that will get at most one message from any given websocket then stop processing!
    – user2668284
    Sep 21, 2021 at 15:59
  • 1
    That is what I wanted, I will now clarify my question. Sorry for wasting your time :/
    – Arizona
    Sep 21, 2021 at 16:17
0

I came across this thread while researching a ticker feed to an ML engine. I originally thought the example used thewebsocket package, which is really old (2010?) and not maintained anymore, but it probably uses the websock-client package, which is current. Both are named websock. I have not investigated either in depth but the newer package seems fruitful.

An alternative is to use the new websockets package. Here's a ticker feed from coinbase using a single thread of coroutines (asyncio). Note that the request is sent once on all the assets/products and then the ticker is received in a loop. If anything fails, the try-catch will be run.

def run_feed():
    '''
    simple test for pro.coinbase ticker feed
    See https://websockets.readthedocs.io/en/stable/    
    '''
    feed_done = False

    def sig_handler(signo, frame):
        nonlocal feed_done
        feed_done = True

    # url="wss://ws-feed.pro.coinbase.com"
    url="wss://ws-feed.pro.coinbase1.com"    
    prodlist = ['ETH-USD', 'DOT-USD']
    json_req = json.dumps(
        {
            "type": "subscribe",
            "product_ids": prodlist,
            "channels": ["matches"],
        }
    )
    
    async def call_api(request):
        nonlocal feed_done
        try:
            async with websockets.connect(url) as ws:
                await ws.send(request)
                while not feed_done:
                    recv = await ws.recv()
                    print_recv(json.loads(recv))
                    print(f'exit feed loop and close websocket')
                    await ws.close()
        except Exception as e:
            print(f'exception: doc={e.__doc__} str={e}')

    signal.signal(signal.SIGINT, sig_handler)                
    asyncio.get_event_loop().run_until_complete(call_api(json_req))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.