WEBSOCKET STREAMING ERROR - ReactorNotRestartable

nickmanan
@sujith
kws = KiteTicker(api_key,api_secret, "USERID")

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([738561])

# Set RELIANCE to tick in `full` mode.

# ws.set_mode(ws.MODE_QUOTE)

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

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


################ ERROR ###################################

2018-01-29 09:33:55+0530 [-] Starting factory <kiteconnect.ticker.KiteTickerClientFactory object at 0x0000004505303710>
Traceback (most recent call last):

File "<ipython-input-25-d02bc3af8194>", line 23, in <module>
kws.connect()

File "C:\Anaconda3\lib\site-packages\kiteconnect\ticker.py", line 522, in connect
reactor.run(**opts)

File "C:\Anaconda3\lib\site-packages\twisted\internet\base.py", line 1242, in run
self.startRunning(installSignalHandlers=installSignalHandlers)

File "C:\Anaconda3\lib\site-packages\twisted\internet\base.py", line 1222, in startRunning
ReactorBase.startRunning(self)

File "C:\Anaconda3\lib\site-packages\twisted\internet\base.py", line 730, in startRunning
raise error.ReactorNotRestartable()

ReactorNotRestartable
Tagged:
  • nickmanan
    @sujith please help !!!
    below is the error that i get when i run the exact websocket code as per documentation

    2018-01-30 09:18:08+0530 [-] Log opened.
    2018-01-30 09:18:10+0530 [-] failing WebSocket opening handshake ('WebSocket connection upgrade failed (403 - Forbidden)')
    2018-01-30 09:18:10+0530 [-] dropping connection to peer tcp4:52.66.33.111:443 with abort=True: WebSocket connection upgrade failed (403 - Forbidden)
    2018-01-30 09:18:10+0530 [-] will retry in 3 seconds
  • sujith
    Hi @nickmanan,
    I think you are looking at old ticker example. Check out new example here. You need to send api_key and access_token.
  • Vivek
    @nickmanan Seems like you are trying to run it twice but ideally once you run it just blocks the current thread. Can you please try the following example and check if it runs?
    import json
    import logging
    from kiteconnect import KiteTicker

    logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')

    api_key = "your api key"
    access_token = "access token"


    # Initialise.
    kws = KiteTicker(api_key, access_token, debug=True)
    tokens = [53501959]
    print("Tokens length", len(tokens))


    # Callback for tick reception.
    def on_ticks(ws, ticks):
    print(ticks)


    # Callback for successful connection.
    def on_connect(ws, response):
    logging.debug("on connect: {}".format(response))
    ws.subscribe(tokens)
    ws.set_mode(ws.MODE_FULL, tokens)


    def on_close(ws, code, reason):
    logging.error("closed connection on close: {} {}".format(code, reason))


    def on_error(ws, code, reason):
    logging.error("closed connection on error: {} {}".format(code, reason))


    def on_noreconnect(ws):
    logging.error("Reconnecting the websocket failed")


    def on_reconnect(ws, attempt_count):
    logging.debug("Reconnecting the websocket: {}".format(attempt_count))


    def on_order_update(ws, data):
    print("order update: ", data)


    # Assign the callbacks.
    kws.on_ticks = on_ticks
    kws.on_connect = on_connect
    kws.on_close = on_close
    kws.on_error = on_error
    kws.on_noreconnect = on_noreconnect
    kws.on_reconnect = on_reconnect
    kws.on_order_update = on_order_update

    # kws.enable_reconnect(reconnect_interval=5, reconnect_tries=50)

    # Infinite loop on the main thread. Nothing after this will run.
    # You have to use the pre-defined callbacks to manage subscriptions.
    # kws.connect(disable_ssl_verification=True)
    kws.connect()
  • nickmanan
    @sujith @vivek thanks for the help. Please update the same at
    https://kite.trade/docs/pykiteconnect/v3/#kiteconnect.KiteConnect.quote.

    Even in v3 docs old ticker example is used
  • nickmanan

    @sujith @vivek .. finally got connected. With every tick i get this error along with the tick data.
    how can i resolve this ?

    [KiteTickerClientProtocol (TLSMemoryBIOProtocol),client] will retry in 2 seconds
    2018-01-30 21:18:04+0530 [-] Stopping factory
    2018-01-30 21:18:06+0530 [-] Starting factory
  • Vivek
    Vivek edited January 2018
    Are you doing something in on_tick callback? If yes then can you please share the logic. Seems like you are closing the connection everytime you receive tick. Also please share the full logs with debug flag True.
  • Dileep
    @vivek ,@sujith I am using the code mention in the https://github.com/zerodhatech/pykiteconnect/blob/kite3/examples/threaded_ticker.py .still getting the error as twisted.internet.error.ReactorNotRestartable.Please help me.
  • rajeshthattikonda
    rajeshthattikonda edited October 2019
    Hi @sujith ,

    Tried using the code presented in https://github.com/zerodhatech/pykiteconnect/tree/kite3

    for accessing webscokets. Unable to execute because of the following error. Can you please help me


    #####Error
    kws.connect()
    Traceback (most recent call last):

    File "", line 1, in
    kws.connect()

    File "C:\Users\LENOVO\Anaconda3\lib\site-packages\kiteconnect-3.7.7-py3.7.egg\kiteconnect\ticker.py", line 534, in connect
    reactor.run(**opts)

    File "C:\Users\LENOVO\Anaconda3\lib\site-packages\twisted\internet\base.py", line 1260, in run
    self.startRunning(installSignalHandlers=installSignalHandlers)

    File "C:\Users\LENOVO\Anaconda3\lib\site-packages\twisted\internet\base.py", line 1240, in startRunning
    ReactorBase.startRunning(self)

    File "C:\Users\LENOVO\Anaconda3\lib\site-packages\twisted\internet\base.py", line 748, in startRunning
    raise error.ReactorNotRestartable()

    ReactorNotRestartable
  • rakeshr
    rakeshr edited October 2019
    @rajeshthattikonda
    You need to remove kws.is_connected() before kws.connect() and use it in different thread to know the websocket connection status, it returns boolean( True or False).
    You can check an example here.
  • sunpy123
    sunpy123 edited March 2020
    Hi All,
    Please share the correct sample ticker example of python as given links are not working
    https://github.com/zerodhatech/pykiteconnect/blob/kite3/examples/threaded_ticker.py

    @sujith @Vivek
  • sujith
    Can you paste the error stacktrace here? It is a working example. You may have to remove disconnect/stop call and unsubscribe calls to see the output.
    I would suggest checking out the API documentation and python documentation here.
  • Quant
    @sujith Please help me in extracting the live stream data using websocket.
  • Quant
    I've already posted the code and the following error which is pending for approval. Hoping for your valuable response at the earliest.
  • bposane
    @Vivek @sujith I am getting below error for web socket streaming please help.

    I am using the code given by @Vivek in above comment
    2020-10-05 13:01:15,187 - kiteconnect.ticker - DEBUG - Start WebSocket connection.
    2020-10-05 13:01:15+0530 [-] Log opened.
    2020-10-05 13:01:15+0530 [-] failing WebSocket opening handshake ('WebSocket connection upgrade failed (403 - Forbidden)')
    2020-10-05 13:01:15+0530 [-] dropping connection to peer tcp4:13.126.82.19:443 with abort=True: WebSocket connection upgrade failed (403 - Forbidden)
    2020-10-05 13:01:15,658 - kiteconnect.ticker - ERROR - Connection error: 1006 - connection was closed uncleanly (WebSocket connection upgrade failed (403 - Forbidden))
    2020-10-05 13:01:15,661 - root - ERROR - closed connection on error: 1006 connection was closed uncleanly (WebSocket connection upgrade failed (403 - Forbidden))
    2020-10-05 13:01:15,661 - kiteconnect.ticker - ERROR - Connection closed: 1006 - connection was closed uncleanly (WebSocket connection upgrade failed (403 - Forbidden))
    2020-10-05 13:01:15,661 - root - ERROR - closed connection on close: 1006 connection was closed uncleanly (WebSocket connection upgrade failed (403 - Forbidden))
    2020-10-05 13:01:15+0530 [-] <twisted.internet.tcp.Connector instance at 0x1df6844da90 disconnected IPv4Address(type='TCP', host='ws.kite.trade', port=443)> will retry in 2 seconds
    2020-10-05 13:01:15+0530 [-] Stopping factory <kiteconnect.ticker.KiteTickerClientFactory object at 0x000001DF66A93130>
    2020-10-05 13:01:18+0530 [-] Starting factory <kiteconnect.ticker.KiteTickerClientFactory object at 0x000001DF66A93130>
    2020-10-05 13:01:18+0530 [-] failing WebSocket opening handshake ('WebSocket connection upgrade failed (403 - Forbidden)')
    2020-10-05 13:01:18+0530 [-] dropping connection to peer tcp4:13.126.82.19:443 with abort=True: WebSocket connection upgrade failed (403 - Forbidden)
  • bposane
    from kiteconnect import KiteTicker
    import json
    import logging
    logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')

    api_key = ""
    access_token = ""

    # Initialise.
    kws = KiteTicker(api_key, access_token, debug=True)
    tokens = [53501959]
    print("Tokens length", len(tokens))

    # Callback for tick reception.
    def on_ticks(ws, ticks):
    print(ticks)


    # Callback for successful connection.
    def on_connect(ws, response):
    logging.debug("on connect: {}".format(response))
    ws.subscribe(tokens)
    ws.set_mode(ws.MODE_FULL, tokens)


    def on_close(ws, code, reason):
    logging.error("closed connection on close: {} {}".format(code, reason))

    def on_error(ws, code, reason):
    logging.error("closed connection on error: {} {}".format(code, reason))

    def on_noreconnect(ws):
    logging.error("Reconnecting the websocket failed")

    def on_reconnect(ws, attempt_count):
    logging.debug("Reconnecting the websocket: {}".format(attempt_count))

    def on_order_update(ws, data):
    print("order update: ", data)

    # Assign the callbacks.
    kws.on_ticks = on_ticks
    kws.on_connect = on_connect
    kws.on_close = on_close
    kws.on_error = on_error
    kws.on_noreconnect = on_noreconnect
    kws.on_reconnect = on_reconnect
    kws.on_order_update = on_order_update

    # kws.enable_reconnect(reconnect_interval=5, reconnect_tries=50)

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

    I am using above code for websocket streming but getting below error
    2020-10-06 10:50:29,386 - kiteconnect.ticker - DEBUG - Start WebSocket connection.
    2020-10-06 10:50:29+0530 [-] Log opened.
    2020-10-06 10:50:29+0530 [-] failing WebSocket opening handshake ('WebSocket connection upgrade failed (403 - Forbidden)')
    2020-10-06 10:50:29+0530 [-] dropping connection to peer tcp4:13.126.82.19:443 with abort=True: WebSocket connection upgrade failed (403 - Forbidden)
    2020-10-06 10:50:29,994 - kiteconnect.ticker - ERROR - Connection error: 1006 - connection was closed uncleanly (WebSocket connection upgrade failed (403 - Forbidden))
    2020-10-06 10:50:29,996 - root - ERROR - closed connection on error: 1006 connection was closed uncleanly (WebSocket connection upgrade failed (403 - Forbidden))
    2020-10-06 10:50:29,997 - kiteconnect.ticker - ERROR - Connection closed: 1006 - connection was closed uncleanly (WebSocket connection upgrade failed (403 - Forbidden))
    2020-10-06 10:50:29,998 - root - ERROR - closed connection on close: 1006 connection was closed uncleanly (WebSocket connection upgrade failed (403 - Forbidden))
    2020-10-06 10:50:30+0530 [-] <twisted.internet.tcp.Connector instance at 0x15e66eb7100 disconnected IPv4Address(type='TCP', host='ws.kite.trade', port=443)> will retry in 2 seconds
    2020-10-06 10:50:30+0530 [-] Stopping factory <kiteconnect.ticker.KiteTickerClientFactory object at 0x0000015E68AB4550>
    2020-10-06 10:50:32+0530 [-] Starting factory <kiteconnect.ticker.KiteTickerClientFactory object at 0x0000015E68AB4550>
    2020-10-06 10:50:32+0530 [-] failing WebSocket opening handshake ('WebSocket connection upgrade failed (403 - Forbidden)')
    2020-10-06 10:50:32+0530 [-] dropping connection to peer tcp4:13.126.82.19:443 with abort=True: WebSocket connection upgrade failed (403 - Forbidden)
    2020-10-06 10:50:32,529 - kiteconnect.ticker - ERROR - Connection error: 1006 - connection was closed uncleanly (WebSocket connection upgrade failed (403 - Forbidden))
    2020-10-06 10:50:32,533 - root - ERROR - closed connection on error: 1006 connection was closed uncleanly (WebSocket connection upgrade failed (403 - Forbidden))
    2020-10-06 10:50:32,536 - kiteconnect.ticker - ERROR - Connection closed: 1006 - connection was closed uncleanly (WebSocket connection upgrade failed (403 - Forbidden))
    2020-10-06 10:50:32,539 - root - ERROR - closed connection on close: 1006 connection was closed uncleanly (WebSocket connection upgrade failed (403 - Forbidden))
    2020-10-06 10:50:32,541 - root - DEBUG - Reconnecting the websocket: 1
    @rakeshr can you please help me out here.
  • rakeshr
    (403 - Forbidden)
    You are getting 403 status here. Either your access_token or API key is incorrect. Re-check on that part. Refer to this documentation to understand login flow.
  • bposane
    I am using below code to get access token
    from kiteconnect import KiteConnect 
    kite = KiteConnect(api_key="xxxxxxxx")

    print(kite.login_url())
    from this I am getting access token and using it in above provided code, still no luck
  • rakeshr
    The above initial flow seems to be correct, but are you assigning access_token correctly?
    You can check a python example here.
Sign In or Register to comment.