Connection error: 1006 - connection was closed uncleanly (WebSocket connection upgrade failed (403)


I just bought an app today, and am trying to login to the Kite Python API now; I have followed the documentation and youtube videos of my code and process to login, so I'm sure im following the right login flow to connect to the API.

However I am getting the above error whenever I connect to KiteTicker and attempt to run .connect(). I know there have been a few threads citing the same error, and I have been through all of them, and tried the approaches suggested on those, but found no satisfactory.

Please assist me with the same as soon as able, because I am unable to retrieve market tick data as of now from Kite API. Thanks!

  • sujith
    Can you paste your code here?
  • Kshitij
    Kshitij edited May 2020
    from kiteconnect import KiteConnect
    from kiteconnect import KiteTicker
    import pandas as pd
    import numpy as np
    import os
    from pprint import pprint
    import logging
    import xlwings as xw
    import datetime
    import pdb
    import time

    global kws,kite




    wb = xw.Book('Ticks_data.xlsx')
    sheet = wb.sheets['Cash']

    row_no = 1

    stocks=[<list of stock symbols here>]

    kws = KiteTicker(api_key, access_token)

    def on_ticks(ws, ticks): # noqa"Ticks: {}".format(ticks))
    global row_no
    for tick in ticks:
    ltp = tick['last_price']
    #time = ticks[0]['timestamp']
    times = time.time()

    sheet.range('A' + str(row_no)).value = times
    sheet.range('B' + str(row_no)).value = token
    sheet.range('C' + str(row_no)).value = ltp
    row_no = row_no + 1


    def on_connect(ws, response): # noqa
    # Subscribe to a list of instrument_tokens (RELIANCE and ACC here).

    # Set RELIANCE to tick in `full` mode.
    ws.set_mode(ws.MODE_LTP, stocks)

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

    # Assign the callbacks.
    kws.on_ticks = on_ticks
    kws.on_connect = on_connect

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

  • Kshitij
    I can get data for a couple of ticks, but then I come across the error mentioned in the title and the API disconnects and program stops
  • sujith
    Authentication for Kite Ticker happens only when one is trying to connect. If the connection drops and tries the client tries to reconnect then this could happen when your access token is invalid. You will have to re-login and get new access token and use that.

    The reason for disconnection could be because of the main thread overload.
    You need to make sure, you don't do anything inside on_ticks, you need to offload all the tasks inside that to another thread. You need to make sure that the main thread is always available to receive ticks. It shouldn't do any task inside on_ticks.
Sign In or Register to comment.