WebSocket connection upgrade failed (429 - TooManyRequests)

D11458
D11458 edited May 2021 in Python client
Hello @rakeshr @sujith ,

I am running a code with a new thread but getting below error after working fine for few minutes:

Connection closed: 1006 - connection was closed uncleanly (WebSocket connection upgrade failed (429 - TooManyRequests))
=================================
def on_ticks(ws,ticks):
insert_tick=insert_ticks(ticks)


def on_connect(ws,response):
ws.subscribe(tokens)
ws.set_mode(ws.MODE_QUOTE,tokens)

while True:
def insert_ticks(ticks):
for one_tick in ticks:
**my workings**
# pdb.set_trace()
time.sleep(0.5)
if condition meets:
kite.modify_order(trd_list[one_tick_token]['m_id'],order_type="MARKET")

now = datetime.datetime.now()
if (now.hour >= 11 and now.minute >= 22 ):
kws.on_ticks=on_ticks
kws.on_connect=on_connect
kws.connect()
if (now.hour >= 15 and now.minute >= 35):
sys.exit()
time.sleep(0.5)
================================
Initially I was using the main ticks thread for strategy workings and placing order and same thing happened, worked fine for a few minutes then threw error of Toomanyrequests so I tried to move to another thread as above but still same issue.

FYI - Despite this error the code resumes functioning properly in some time but we miss the right price and poses big risk.
  • rakeshr
    WebSocket connection upgrade failed (429 - TooManyRequests)
    You are exceeding the maximum limit for WebSocket connection(it's fixed to 3). So, make sure you are not creating multiple instances at the same time.
  • D11458
    I am running just the above code that too for only 4 instruments. I was earlier placing 4 separate modify orders in this code, so thought that might be >3 connections issue so clubbed that in one modify order as shown in above code. Still same issue.
    Note: After posting this query I commented out "if condition meets:
    kite.modify_order(trd_list[one_tick_token]['m_id'],order_type="MARKET")" and used print command just to check, and it is running without any problem since more than 2 hours.
    FYI - I have gone through multiple threads no help.
  • sujith
    Abrupt disconnections don't get registered at the server. You need to make sure you are not suspending the script which might result in hanging connection at the server and check all the edge-cases in your code wherein disconnect is not called.
  • D11458
    ok...Am I doing this efficient way on establishing connection? could you please see if any changes I can do:

    api_key=open('D:/Data/python/zerodha_k.txt','r').read()
    access_token=open('D:/Data/python/zerodha_t.txt','r').read()
    kite=KiteConnect(api_key=api_key)
    kite.set_access_token(access_token)
    kws = KiteTicker(api_key, access_token)
  • D11458
    Have sorted this out - the thread can be closed.
This discussion has been closed.