1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. Anuncie Aqui
    Anuncie aqui você Também: fdantas@4each.com.br

[Python] Difference between websocket and websockets

Discussão em 'Python' iniciado por Stack, Setembro 16, 2025.

  1. Stack

    Stack Membro Participativo

    What's the difference between websocket (websocket client) and websockets?

    I have a code that seems to do the same thing.

    import websockets
    import asyncio
    import json

    async def capture_data():
    uri = "wss://www.bitmex.com/realtime?subscribe=instrument:XBTUSD"
    #uri = "wss://www.bitmex.com/realtime"
    async with websockets.connect(uri) as websocket:
    while True:
    data = await websocket.recv()
    data = json.loads(data)
    try :
    #print('\n', data['data'][0]["lastPrice"])
    print('\n', data)
    except KeyError:
    continue


    asyncio.get_event_loop().run_until_complete(capture_data())


    import websocket
    import json

    def on_open(ws):
    print("opened")

    auth_data = {
    'op': 'subscribe',
    'args': ['instrument:XBTUSD']
    }
    ws.send(json.dumps(auth_data))

    def on_message(ws, message):
    message = json.loads(message)
    print(message)

    def on_close(ws):
    print("closed connection")


    socket = "wss://www.bitmex.com/realtime"
    ws = websocket.WebSocketApp(socket,
    on_open=on_open,
    on_message=on_message,
    on_close=on_close)
    ws.run_forever()


    EDIT:

    Is Python powerful enough to receive a lot of data via websocket? In times of high traffic, I could have thousands of messages per second and even tens of thousands for short periods of time.

    I've seen a lot of things about Nodejs that might be better than python but I don't know js and even if I just copy code from the internet I'm not sure I wouldn't waste time if I have to send the data to my python script and then process it.

    Continue reading...

Compartilhe esta Página