Getting readtimeout error

ZTD798
from turtle import up
from kiteconnect import KiteConnect
from kiteconnect import KiteTicker
import pandas as pd
import mysql.connector
from datetime import datetime,timedelta
import numpy as np
import os
import time
import requests
import json

if __name__=='__main__':

api_key = "120kffyh82s3jyte"
api_secret = "zvmvok3qmztn4cinm8xlg399aa3a42sd"
kite = KiteConnect(api_key=api_key)

access_token=""

now = datetime.now()
c_date= now.strftime("%Y-%m-%d")
f_date=""
with open('access_token.txt','r') as ak:
f_date=ak.readline()[0:10]

if f_date==c_date:
with open('access_token.txt') as ak:
access_token=ak.readline()[10:42]
print(access_token)
else:
print(kite.login_url())
request_toke=input("Enter request token")
data = kite.generate_session(request_toke, api_secret=api_secret)
with open('access_token.txt','w') as ak:
ak.write(c_date)
ak.write(data['access_token'])
access_token=data['access_token']

kite.set_access_token(access_token)

mydb = mysql.connector.connect(
host="LAPTOP-GU5757RD",
user="a",
password="1234",
database="stock"
)

while True:

mycursor = mydb.cursor()
prev_time= datetime.strftime(datetime.now()-timedelta(minutes=2),'%Y-%m-%d %H:%M')+":00"
c_time= datetime.strftime(datetime.now()-timedelta(minutes=1),'%Y-%m-%d %H:%M')+":00"


ltp=(kite.ltp('NSE:'+str("NIFTY BANK"))).get('NSE:'+str("NIFTY BANK"),{}).get('last_price')
update_ltp="update price set ltp="+str(ltp)
mycursor.execute(update_ltp)
time.sleep(0.1)
data_cc=kite.historical_data(260105,from_date=c_time,to_date='2022-09-30 15:15:00',interval='minute')
time.sleep(0.1)
data_pc=kite.historical_data(260105,from_date=prev_time,to_date='2022-09-30 15:15:00',interval='minute')
time.sleep(0.1)



data_cc_ohlc=pd.DataFrame(data_cc)
data_pc_ohlc=pd.DataFrame(data_pc)


ch=0.0
ph=0.0
cl=0.0
pl=0.0

try:
c_h=data_cc_ohlc['high']
ch=c_h[0]

c_l=data_cc_ohlc['low']
cl=c_l[0]

p_h=data_pc_ohlc['high']
ph=p_h[0]

p_l=data_pc_ohlc['low']
pl=p_l[0]

except KeyError as e:
ch=0.0
cl=0.0
ph=0.0
pl=0.0


update_ch="update ob set ch="+str(ch)
update_cl="update ob set cl="+str(cl)
update_ph="update ob set ph="+str(ph)
update_pl="update ob set pl="+str(pl)


mycursor.execute(update_ch)
mycursor.execute(update_cl)
mycursor.execute(update_ph)
mycursor.execute(update_pl)
mydb.commit()
print("ok")


Can anyone help me i am constantly getting kite readtimeout error at fetching ltp.
I tried adding sleep(0.1) but still sometimes i am facing this issue
Tagged:
  • sujith
    You might be getting blocked by CDN thinking you are trying to DDOS, since you are exceeding rate limits.
    You can know more about rate limits on the FAQ thread.

    If you need ltp every second then I would suggest using websocket API.
Sign In or Register to comment.