kiteconnect.ticker:Connection error: 1006 - connection was closed uncleanly (WebSocket connection up

parask

[*] Downloading instrument list...
[*] Instrument tokens: [738561]
[*] Connecting to WebSocket...
ERROR:kiteconnect.ticker:Connection error: 1006 - connection was closed uncleanly (WebSocket connection upgrade failed (403 - Forbidden))
[WS 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))
[WS CLOSED] 1006 connection was closed uncleanly (WebSocket connection upgrade failed (403 - Forbidden))
I have gone through other related queries regarding this similar issue. However this issue persists. I am pretty sure I am using the correct access_token for connecting. I am using the following login flow:

from kiteconnect import KiteConnect
from kiteconnect import KiteTicker
import pandas as pd
import datetime
import pdb

kws = ""
kite = ""

api_k = "" # api_key
api_s = "" # api_secret


def get_login(api_k, api_s): # log in to zerodha API panel
global kws, kite
kite = KiteConnect(api_key=api_k)

print("[*] Generate access Token : ", kite.login_url())
request_tkn = input("[*] Enter Your Request Token Here : ")
kite.generate_session(request_tkn, api_s)
kite.set_access_token(data["access_token"])
kws = KiteTicker(api_k, data["access_token"])
print(data['access_token'])

get_login(api_k, api_s)
And using the generated access_token in the web socket client framework as under:

from kiteconnect import KiteConnect, KiteTicker
import pandas as pd
import logging

# ---------------- CONFIG ----------------
API_KEY = ""
ACCESS_TOKEN = "".strip()

EXCHANGE = "NSE"
SYMBOLS = ["RELIANCE"] # NSE instrument
# ---------------------------------------

logging.basicConfig(level=logging.INFO)

# ---------- REST: get instrument tokens ----------
kite = KiteConnect(api_key=API_KEY)
kite.set_access_token(ACCESS_TOKEN)

print("[*] Downloading instrument list...")
instrument_dump = kite.instruments(EXCHANGE)
instrument_df = pd.DataFrame(instrument_dump)

def token_lookup(df, symbols):
tokens = []
for sym in symbols:
row = df[df.tradingsymbol == sym]
if row.empty:
raise ValueError(f"Symbol not found: {sym}")
tokens.append(int(row.instrument_token.values[0]))
return tokens

tokens = token_lookup(instrument_df, SYMBOLS)
print("[*] Instrument tokens:", tokens)

# ---------- WEBSOCKET ----------
kws = KiteTicker(ACCESS_TOKEN, API_KEY)

def on_connect(ws, response):
print("[WS] Connected")
ws.subscribe(tokens)
ws.set_mode(ws.MODE_FULL, tokens)

def on_ticks(ws, ticks):
print("[TICK]", ticks[0])

def on_error(ws, code, reason):
print("[WS ERROR]", code, reason)

def on_close(ws, code, reason):
print("[WS CLOSED]", code, reason)
ws.stop()

kws.on_connect = on_connect
kws.on_ticks = on_ticks
kws.on_error = on_error
kws.on_close = on_close

print("[*] Connecting to WebSocket...")
kws.connect()
  • salim_chisty
    Are you using the Kite personal app or the Kite Connect app? You may go to the developer's console and check the type of app. Make sure you use the Kite Connect app's api_key.
    The Kite personal app doesn't have access to the Websockets API, which also results in http status code 403. You may make any other API call and check if the access token is valid or not. 403 usually means the session expired.
Sign In or Register to comment.