[Python] Zerodha KiteConnect API producing 1006 Error

Discussão em 'Python' iniciado por Stack, Agosto 9, 2025.

  1. Stack

    Stack Membro Participativo

    I am building a dashboard for live tickers

    I am using zerodha Kiteconnect to fetch SE Options & Futures live data for that ... The client for whom I am building the dashboard has provided me the api key, and also provides me the access token daily .... The situation is he uses the same credentials in his own program (that he runs separately in his own laptop) to fetch live ticker data

    When I use KiteConnect to fetch instrument dump it works, below code executes successfully,


    from kiteconnect import KiteTicker, KiteConnect

    access_token = '*********' # changes every day
    api_key = '*********'

    kite = KiteConnect(api_key=key_secret, access_token = access_token)
    instrument_list = kite.instruments(exchange=kite.EXCHANGE_NFO)



    but when I use KiteTicker (WebSocket Streaming) with same credentials as shown in code below it produces 1006 Connection error:

    kws = KiteTicker(api_key, access_token=kite.access_token)

    ####### define the callbacks ############

    def on_connect(ws, response):
    # Callback on successful connect.
    # Subscribe to a list of instrument_tokens (RELIANCE and ACC here).
    ws.subscribe(instrument_tokens)

    # Set tick in `full` mode.
    ws.set_mode(ws.MODE_FULL, instrument_tokens)


    def on_ticks(ws, ticks):
    # Callback to receive ticks.
    # logging.debug("Ticks: {}".format(ticks))
    print(ticks)
    ticks_list.append(ticks)

    # close the connection after some time
    if (time.time() - begin_time) > 120: # run for 2 minutes
    write_json(ticks_list)
    print("close called")
    ws.close()


    def on_close(ws, code, reason):
    # On connection close stop the event loop.
    # Reconnection will not happen after executing `ws.stop()`
    print("-------- Stopping ------------")
    ws.stop()
    print("--------- Stopped ----------")


    ### define
    ticks_list = [] # will hold list of JSON objects
    ##### Assign the callbacks.
    kws.on_ticks = on_ticks
    kws.on_connect = on_connect
    kws.on_close = on_close

    begin_time = time.time()

    # Infinite loop on the main thread. Nothing after this will run.
    # You have to use the pre-defined callbacks to manage subscriptions.
    kws.connect()



    The exact error produced is:


    Connection error: 1006 - connection was closed uncleanly (I dropped the WebSocket TCP connection: close reason without close code)
    ​

    Can you please guide me as to why is this happening? Also is it possible to use the same credentials in parallel by two different programs from different IP to fetch live tick data...

    Thanks

    Continue reading...

Compartilhe esta Página