facing a strange bug with kiteconnect

akraghavendran
akraghavendran edited August 13 in Python client
Hi, @sujith @rakeshr
i have been using anaconda navigator to setup an environment on which i am running the algos to trade. I am currently on python version 3.10.8. Every six months, i have a habit of updating the python's latest version by creating a new environment. This time when i have updated to python 3.12.4, I found a strange error only with historical api usage, and other things remain normal.
> Say, When i leave the console idle for 5 minutes and when i try to call kite.historical_data(instrument,......), it returns ReadTimeOutError, if i try it for the 2nd time, the connection is becoming successful. I have tried increasing the kiteconnect timeout up to 30 secs, till the same pattern follows. i have tried in python version 3.11.[decimals] , changed the laptops, ran from AWS, changed internet, installed new anaconda environment, and still the same result after almost 1week of testing with all my known ways to remove the error, and the same pattern is achieved
>leave the console, idle for 5 minutes, on first call of historical_data --> ReadTimeouterror, 2nd call -->successful connection
>Meanwhile the postback is working fine, streaming data and order updates are received without any disconnection


Traceback (most recent call last):

File ~\anaconda3\envs\py3124\Lib\site-packages\urllib3\connectionpool.py:536 in _make_request
response = conn.getresponse()

File ~\anaconda3\envs\py3124\Lib\site-packages\urllib3\connection.py:464 in getresponse
httplib_response = super().getresponse()

File ~\anaconda3\envs\py3124\Lib\http\client.py:1428 in getresponse
response.begin()

File ~\anaconda3\envs\py3124\Lib\http\client.py:331 in begin
version, status, reason = self._read_status()

File ~\anaconda3\envs\py3124\Lib\http\client.py:292 in _read_status
line = str(self.fp.readline(_MAXLINE + 1), "iso-8859-1")

File ~\anaconda3\envs\py3124\Lib\socket.py:708 in readinto
return self._sock.recv_into(b)

File ~\anaconda3\envs\py3124\Lib\ssl.py:1252 in recv_into
return self.read(nbytes, buffer)

File ~\anaconda3\envs\py3124\Lib\ssl.py:1104 in read
return self._sslobj.read(len, buffer)

TimeoutError: The read operation timed out


The above exception was the direct cause of the following exception:

Traceback (most recent call last):

File ~\anaconda3\envs\py3124\Lib\site-packages\requests\adapters.py:667 in send
resp = conn.urlopen(

File ~\anaconda3\envs\py3124\Lib\site-packages\urllib3\connectionpool.py:843 in urlopen
retries = retries.increment(

File ~\anaconda3\envs\py3124\Lib\site-packages\urllib3\util\retry.py:474 in increment
raise reraise(type(error), error, _stacktrace)

File ~\anaconda3\envs\py3124\Lib\site-packages\urllib3\util\util.py:39 in reraise
raise value

File ~\anaconda3\envs\py3124\Lib\site-packages\urllib3\connectionpool.py:789 in urlopen
response = self._make_request(

File ~\anaconda3\envs\py3124\Lib\site-packages\urllib3\connectionpool.py:538 in _make_request
self._raise_timeout(err=e, url=url, timeout_value=read_timeout)

File ~\anaconda3\envs\py3124\Lib\site-packages\urllib3\connectionpool.py:369 in _raise_timeout
raise ReadTimeoutError(

ReadTimeoutError: HTTPSConnectionPool(host='api.kite.trade', port=443): Read timed out. (read timeout=7)


During handling of the above exception, another exception occurred:

Traceback (most recent call last):

Cell In[7], line 1
dwnld_nifty_wkly_op(week_start = "2024-08-09",weekly_expiry = "2024-08-14")

Cell In[6], line 4 in dwnld_nifty_wkly_op
ins_df = pd.DataFrame(tf.akr_kite.instruments())

File ~\anaconda3\envs\py3124\Lib\site-packages\kiteconnect\connect.py:568 in instruments
return self._parse_instruments(self._get("market.instruments.all"))

File ~\anaconda3\envs\py3124\Lib\site-packages\kiteconnect\connect.py:861 in _get
return self._request(route, "GET", url_args=url_args, params=params, is_json=is_json)

File ~\anaconda3\envs\py3124\Lib\site-packages\kiteconnect\connect.py:916 in _request
raise e

File ~\anaconda3\envs\py3124\Lib\site-packages\kiteconnect\connect.py:904 in _request
r = self.reqsession.request(method,

File ~\anaconda3\envs\py3124\Lib\site-packages\requests\sessions.py:589 in request
resp = self.send(prep, **send_kwargs)

File ~\anaconda3\envs\py3124\Lib\site-packages\requests\sessions.py:703 in send
r = adapter.send(request, **kwargs)

File ~\anaconda3\envs\py3124\Lib\site-packages\requests\adapters.py:713 in send
raise ReadTimeout(e, request=request)

ReadTimeout: HTTPSConnectionPool(host='api.kite.trade', port=443): Read timed out. (read timeout=7)
  • rakeshr
    ReadTimeout: HTTPSConnectionPool(host='api.kite.trade', port=443): Read timed out. (read timeout=7)
    Is timeout coming only for specific tokens? Can you paste here those token/tokens.
    Maybe try it out with some active tokens.
  • akraghavendran
    akraghavendran edited August 13
    ReadTimeOut error is not token specific in my case, it appears for equity, index and option strikes. At the time of writing it happened with token "14066946", which corresponds to "BANKNIFTY2481450400CE".
  • akraghavendran
    In [14]:niftybank_call_strike_token
    Out[14]: 14066946
    niftybank_call_strike_df = pd.DataFrame(hist_kite.historical_data(niftybank_call_strike_token,from_date,to_date,interval))
    Traceback (most recent call last):

    File ~\anaconda3\envs\py3124\Lib\site-packages\urllib3\connectionpool.py:536 in _make_request
    response = conn.getresponse()

    File ~\anaconda3\envs\py3124\Lib\site-packages\urllib3\connection.py:464 in getresponse
    httplib_response = super().getresponse()

    File ~\anaconda3\envs\py3124\Lib\http\client.py:1428 in getresponse
    response.begin()

    File ~\anaconda3\envs\py3124\Lib\http\client.py:331 in begin
    version, status, reason = self._read_status()

    File ~\anaconda3\envs\py3124\Lib\http\client.py:292 in _read_status
    line = str(self.fp.readline(_MAXLINE + 1), "iso-8859-1")

    File ~\anaconda3\envs\py3124\Lib\socket.py:708 in readinto
    return self._sock.recv_into(b)

    File ~\anaconda3\envs\py3124\Lib\ssl.py:1252 in recv_into
    return self.read(nbytes, buffer)

    File ~\anaconda3\envs\py3124\Lib\ssl.py:1104 in read
    return self._sslobj.read(len, buffer)

    TimeoutError: The read operation timed out


    The above exception was the direct cause of the following exception:

    Traceback (most recent call last):

    File ~\anaconda3\envs\py3124\Lib\site-packages\requests\adapters.py:667 in send
    resp = conn.urlopen(

    File ~\anaconda3\envs\py3124\Lib\site-packages\urllib3\connectionpool.py:843 in urlopen
    retries = retries.increment(

    File ~\anaconda3\envs\py3124\Lib\site-packages\urllib3\util\retry.py:474 in increment
    raise reraise(type(error), error, _stacktrace)

    File ~\anaconda3\envs\py3124\Lib\site-packages\urllib3\util\util.py:39 in reraise
    raise value

    File ~\anaconda3\envs\py3124\Lib\site-packages\urllib3\connectionpool.py:789 in urlopen
    response = self._make_request(

    File ~\anaconda3\envs\py3124\Lib\site-packages\urllib3\connectionpool.py:538 in _make_request
    self._raise_timeout(err=e, url=url, timeout_value=read_timeout)

    File ~\anaconda3\envs\py3124\Lib\site-packages\urllib3\connectionpool.py:369 in _raise_timeout
    raise ReadTimeoutError(

    ReadTimeoutError: HTTPSConnectionPool(host='api.kite.trade', port=443): Read timed out. (read timeout=7)


    During handling of the above exception, another exception occurred:

    Traceback (most recent call last):

    Cell In[19], line 1
    niftybank_call_strike_df = pd.DataFrame(hist_kite.historical_data(niftybank_call_strike_token,from_date,to_date,interval))

    File ~\anaconda3\envs\py3124\Lib\site-packages\kiteconnect\connect.py:632 in historical_data
    data = self._get("market.historical",

    File ~\anaconda3\envs\py3124\Lib\site-packages\kiteconnect\connect.py:861 in _get
    return self._request(route, "GET", url_args=url_args, params=params, is_json=is_json)

    File ~\anaconda3\envs\py3124\Lib\site-packages\kiteconnect\connect.py:916 in _request
    raise e

    File ~\anaconda3\envs\py3124\Lib\site-packages\kiteconnect\connect.py:904 in _request
    r = self.reqsession.request(method,

    File ~\anaconda3\envs\py3124\Lib\site-packages\requests\sessions.py:589 in request
    resp = self.send(prep, **send_kwargs)

    File ~\anaconda3\envs\py3124\Lib\site-packages\requests\sessions.py:703 in send
    r = adapter.send(request, **kwargs)

    File ~\anaconda3\envs\py3124\Lib\site-packages\requests\adapters.py:713 in send
    raise ReadTimeout(e, request=request)

    ReadTimeout: HTTPSConnectionPool(host='api.kite.trade', port=443): Read timed out. (read timeout=7)
  • akraghavendran
    akraghavendran edited August 13
    For me this error is python version specific, any versions below 3.11 works fine. On versions of python above 3.11, idling of more than 5 minutes reproduces ReadTimeout error on 1st call, 2nd and subsequent calls are fine, provided the console is not kept idle for more than 5 minutes, tested with IDE's - spyder, jupyternotebook, vs code
  • akraghavendran
    hi, just bumping, any updates on this?
  • rakeshr
    On versions of python above 3.11, idling of more than 5 minutes reproduces ReadTimeout error on 1st call, 2nd and subsequent calls are fine
    Hey, this doesn't seem to be related to the Kiteconnect API error. Maybe something to do with the underlying network or IDE.
Sign In or Register to comment.