ERROR:kiteconnect.ticker:Connection error: 1006 - .....uncleanly(WebSocket connection upgrade failed

suhas_parray
Hello Developers,

Could someone please help me out with some errors.

Below is the code followed by the error response.

import time
import logging
from kiteconnect import KiteTicker

logging.basicConfig(level=logging.DEBUG)

kws = KiteTicker(api_key, public_token)

tokens = [341249]

# Callback for tick reception.
def on_ticks(ws, ticks):
if len(ticks) > 0:
logging.info("Current mode: {}".format(ticks[0]["mode"]))

# Callback for successful connection.
def on_connect(ws, response):
logging.info("Successfully connected. Response: {}".format(response))
ws.subscribe(tokens)
ws.set_mode(ws.MODE_FULL, tokens)
logging.info("Subscribe to tokens in Full mode: {}".format(tokens))

# Callback when current connection is closed.
def on_close(ws, code, reason):
logging.info("Connection closed: {code} - {reason}".format(code=code, reason=reason))

# Callback when connection closed with error.
def on_error(ws, code, reason):
logging.info("Connection error: {code} - {reason}".format(code=code, reason=reason))

# Callback when reconnect is on progress
def on_reconnect(ws, attempts_count):
logging.info("Reconnecting: {}".format(attempts_count))

# Callback when all reconnect failed (exhausted max retries)
def on_noreconnect(ws):
logging.info("Reconnect failed.")

# Assign the callbacks.
kws.on_ticks = on_ticks
kws.on_close = on_close
kws.on_error = on_error
kws.on_connect = on_connect
kws.on_reconnect = on_reconnect
kws.on_noreconnect = on_noreconnect

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

# Block main thread
logging.info("This is main thread. Will change webosocket mode every 5 seconds.")

count = 0
while True:
count += 1
if count % 2 == 0:
if kws.is_connected():
logging.info("### Set mode to LTP for all tokens")
kws.set_mode(kws.MODE_LTP, tokens)
else:
if kws.is_connected():
logging.info("### Set mode to quote for all tokens")
kws.set_mode(kws.MODE_QUOTE, tokens)

time.sleep(2)


The ERROR Response is mentioned below:

INFO:root:This is main thread. Will change webosocket mode every 5 seconds.
ERROR:kiteconnect.ticker:Connection error: 1006 - connection was closed uncleanly (WebSocket connection upgrade failed (403 - Forbidden))
INFO:root:Connection error: 1006 - connection was closed uncleanly (WebSocket connection upgrade failed (403 - Forbidden))
ERROR:kiteconnect.ticker:Connection closed: 1006 - connection was closed uncleanly (WebSocket connection upgrade failed (403 - Forbidden))


Thank You.
  • ksmaheshkumar
    I am also getting same error, can someone pls share their working code for ticks
  • sujith
    A 403 means your session is expired, you need to login and generate a new access token and use that.
Sign In or Register to comment.