Frequent Connection Disconnection.

babansfolio
Hello Support Team,

Today there has been multiple times connection fail triggered with Python API,
Please check what should be possible reason,
How to avoid such problems.

=======================================================
ERROR:kiteconnect.ticker:Connection error: 1006 - connection was closed uncleanly (None)
ERROR:kiteconnect.ticker:Connection closed: 1006 - connection was closed uncleanly (None)
### Set mode to MODE_FULL for all tokens at 2020-06-10 10:51:03
ERROR:kiteconnect.ticker:Connection error: 1006 - connection was closed uncleanly (None)
ERROR:kiteconnect.ticker:Connection closed: 1006 - connection was closed uncleanly (None)
ERROR:kiteconnect.ticker:Connection error: 1006 - connection was closed uncleanly (WebSocket opening handshake timeout (peer did not finish the opening handshake in time))
ERROR:kiteconnect.ticker:Connection closed: 1006 - connection was closed uncleanly (WebSocket opening handshake timeout (peer did not finish the opening handshake in time))
Reconnecting: 1
Unhandled Error
Traceback (most recent call last):
File "/usr/lib/python3.5/threading.py", line 914, in _bootstrap_inner
self.run()
File "/usr/lib/python3.5/threading.py", line 862, in run
self._target(*self._args, **self._kwargs)
File "/home/ubuntu/my_folder/my_env/lib/python3.5/site-packages/twisted/internet/base.py", line 1283, in run
self.mainLoop()
File "/home/ubuntu/my_folder/my_env/lib/python3.5/site-packages/twisted/internet/base.py", line 1292, in mainLoop
self.runUntilCurrent()
--- ---
File "/home/ubuntu/my_folder/my_env/lib/python3.5/site-packages/twisted/internet/base.py", line 913, in runUntilCurrent
call.func(*call.args, **call.kw)
File "/home/ubuntu/my_folder/my_env/lib/python3.5/site-packages/twisted/internet/tcp.py", line 520, in connectionLost
self.connector.connectionLost(reason)
File "/home/ubuntu/my_folder/my_env/lib/python3.5/site-packages/twisted/internet/base.py", line 1171, in connectionLost
self.factory.clientConnectionLost(self, reason)
File "/home/ubuntu/my_folder/my_env/lib/python3.5/site-packages/twisted/protocols/policies.py", line 169, in clientConnectionLost
self.wrappedFactory.clientConnectionLost(connector, reason)
File "/home/ubuntu/my_folder/my_env/lib/python3.5/site-packages/kiteconnect-3.8.1-py3.5.egg/kiteconnect/ticker.py", line 189, in clientConnectionLost

File "/home/ubuntu/my_folder/my_env/lib/python3.5/site-packages/kiteconnect-3.8.1-py3.5.egg/kiteconnect/ticker.py", line 689, in _on_reconnect

File "collection_script_new_test_local.py", line 328, in on_reconnect
ws.subscribe(token_list)
File "/home/ubuntu/my_folder/my_env/lib/python3.5/site-packages/kiteconnect-3.8.1-py3.5.egg/kiteconnect/ticker.py", line 578, in subscribe

File "/home/ubuntu/my_folder/my_env/lib/python3.5/site-packages/kiteconnect-3.8.1-py3.5.egg/kiteconnect/ticker.py", line 544, in _close

File "/home/ubuntu/my_folder/my_env/lib/python3.5/site-packages/autobahn/websocket/protocol.py", line 1974, in sendClose
raise Exception("close reason without close code")
builtins.Exception: close reason without close code

====================================================================================
Tagged:
  • mnkartik
    Hi @babansfolio,

    Is this the real time api, you are facing issue.
  • babansfolio
    Yes, Its triggered 4-5 times today itself.
  • mnkartik
    Yes I am also facing the same from java client in websocket.
  • sujith
    We haven't made any changes to Websocket API. We don't see any issue from our end.
  • sujith
    PS: @babansfolio, we are not support executives here. We are all developers like you who hangout here to help fellow Kite Connect community developers.
  • babansfolio
    @sujith : My Apology .
    when Kite though below errors? do you know pls.

    ERROR:kiteconnect.ticker:Connection closed: 1006 - connection was closed uncleanly (WebSocket opening handshake timeout (peer did not finish the opening handshake in time))
    Reconnecting: 1
    Unhandled Error
    Traceback (most recent call last):
    File "/usr/lib/python3.5/threading.py", line 914, in _bootstrap_inner
    self.run()
  • rakeshr
    @babansfolio
    ERROR:kiteconnect.ticker:Connection closed: 1006 - connection was closed
    You can go through pykiteconnect section of FAQ.
  • sunilg1972
    sunilg1972 edited June 2020
    Traceback (most recent call last):
    File "zerodha_kite_banknifty.py", line 774, in
    main()
    File "zerodha_kite_banknifty.py", line 290, in main
    data = kite.generate_session(request_token, api_secret=api_secret)
    File "C:\ProgramData\Anaconda3\lib\site-packages\kiteconnect\connect.py", line 252, in generate_session
    "checksum": checksum
    File "C:\ProgramData\Anaconda3\lib\site-packages\kiteconnect\connect.py", line 821, in _post
    return self._request(route, "POST", params)
    File "C:\ProgramData\Anaconda3\lib\site-packages\kiteconnect\connect.py", line 886, in _request
    raise exp(data["message"], code=r.status_code)
    kiteconnect.exceptions.TokenException: Token is invalid or has expired.
    DEBUG:comtypes:Calling CoUnititialize()
    DEBUG:comtypes:CoUnititialize() done.

    above error coming out of following python code. But previously it was working fine

    def main():
    global api_key,api_secret,request_token,redirect_uri,kite

    logged_in = False

    stored_api_key = read_key_from_settings('api_key')
    stored_access_token = read_key_from_settings('access_token')

    if stored_access_token is not None and stored_api_key is not None:
    print('You already have a stored access token: [%s] paired with API key [%s]' % (stored_access_token, stored_api_key))
    print('Do you want to use the above credentials?')
    selection = input('Type N for no, any key for yes: ')
    if selection.lower() != 'n':
    try:
    logged_in = True
    except requests.HTTPError as e:
    print('Sorry, there was an error [%s]. Let''s start over\n\n' % e)

    if logged_in is False:
    stored_api_key = read_key_from_settings('api_key')
    if stored_api_key is not None:
    api_key = input('What is your app''s API key [%s]: ' % stored_api_key)
    if api_key == '':
    api_key = stored_api_key
    else:
    api_key = input('What is your app''s API key: ')
    write_key_to_settings('api_key', api_key)

    stored_api_secret = read_key_from_settings('api_secret')
    if stored_api_secret is not None:
    api_secret = input('What is your app''s API secret [%s]: ' % stored_api_secret)
    if api_secret == '':
    api_secret = stored_api_secret
    else:
    api_secret = input('What is your app''s API secret: ')
    write_key_to_settings('api_secret', api_secret)

    stored_redirect_uri = read_key_from_settings('redirect_uri')
    if stored_redirect_uri is not None:
    redirect_uri = input('What is your app''s redirect_uri [%s]: ' % stored_redirect_uri)
    if redirect_uri == '':
    redirect_uri = stored_redirect_uri
    else:
    redirect_uri = input('What is your app''s redirect_uri: ')
    write_key_to_settings('redirect_uri', redirect_uri)

    kite = KiteConnect(api_key= api_key)

    rquest_token = read_key_from_settings('request_token')
    access_token = read_key_from_settings('access_token')
    if access_token != "":
    url_to_open = "https://kite.trade/connect/login?api_key=" +api_key
    webbrowser.open(url_to_open,new=1)
    request_token = input('What is your app''s new request_token [%s]: ' % request_token)
    if request_token == '':
    try:
    kite.set_access_token(access_token)
    print("set")

    except:
    url_to_open = "https://kite.trade/connect/login?api_key=" +api_key
    webbrowser.open(url_to_open,new=1)
    request_token = input('What is your app''s new request code: ')
    data = kite.generate_session(request_token, api_secret=api_secret)
    access_token = data['access_token']
    kite.set_access_token(access_token)
    else:
    data = kite.generate_session(request_token, api_secret=api_secret)
    access_token = data['access_token']
    kite.set_access_token(access_token)
    else:
    print('Great! Now paste the following URL on your browser and type the code that you get in return')
    url_to_open = "https://kite.trade/connect/login?api_key="+api_key
    webbrowser.open(url_to_open,new=1)
    request_token = input('What is your app''s request code: ')
    data = kite.generate_session(request_token, api_secret=api_secret)
    access_token = data['access_token']
    kite.set_access_token(access_token)

    write_key_to_settings('request_token', request_token)
    write_key_to_settings('access_token', access_token)

    else:
    kite = KiteConnect(api_key= api_key)
    access_token = read_key_from_settings('access_token')
    kite.set_access_token(access_token)
Sign In or Register to comment.