1006 - connection was closed uncleanly

Kapil123
Kapil123 edited June 28 in Python client
Hi,

I am running below code for web-socket streaming code from your github doc.
I have replaced my api key and token from browser just for security concern.
I am getting below output. Could you please help me what could be the reason for 1006 error?
I have update python client and tried with switching off my windows firewall.


===================================== OUTPUT ===========================

(base) D:\kiteconnect_workspace>python buy_strategy.py
https://kite.zerodha.com/connect/login?api_key=MY_API_KEY&v=3
Enter access tokenTOKEN_FROM_BROWSER
ERROR:kiteconnect.ticker:Connection error: 1006 - connection was closed uncleanly (I dropped the WebSocket TCP connection: close reason without close code)
ERROR:kiteconnect.ticker:Connection closed: 1006 - connection was closed uncleanly (I dropped the WebSocket TCP connection: close reason without close code)

===================================== CODE ====================

import logging
import pandas as pd
import numpy as np
from kiteconnect import KiteConnect, KiteTicker
import time
import datetime as dt
import sys

logging.basicConfig(level=logging.INFO)

api_key = "MY_API_KEY"
kite = KiteConnect(api_key=api_key)
# Initialise.

# Redirect the user to the login url obtained
# from kite.login_url(), and receive the request_token
# from the registered redirect url after the login flow.
# Once you have the request_token, obtain the access_token
# as follows.

print(kite.login_url())
request_token = input("Enter access token")

data = kite.generate_session(request_token, api_secret="t4vztb2lk98upfaq6vzr2taredna17rv")
kite.set_access_token(data["access_token"])

kws = KiteTicker(api_key, data["access_token"])

def instrumentLookup(instrument_df,symbol):
"""Looks up instrument token for a given script from instrument dump"""
try:
return instrument_df[instrument_df.tradingsymbol==symbol].instrument_token.values[0]
except:
return -1

# get dump
instrument_dump = kite.instruments("NFO")
instrument_df = pd.DataFrame(instrument_dump)
ticker = "NIFTY2470424050PE"
tokens = [instrumentLookup(instrument_df, ticker)]

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

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

# Set RELIANCE to tick in `full` mode.
ws.set_mode(ws.MODE_FULL, tokens)

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

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

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

  • rakeshr
    Connection error: 1006 - connection was closed uncleanly (I dropped the WebSocket TCP connection: close reason without close code)
    Go through the Python websocket FAQs here.
Sign In or Register to comment.