AttributeError: 'NoneType' object has no attribute 'sendMessage'

raptron
DEBUG:urllib3.connectionpool:Starting new HTTPS connection (1): api.kite.trade:443
DEBUG:urllib3.connectionpool:https://api.kite.trade:443 "POST /session/token HTTP/1.1" 200 None
DEBUG:urllib3.connectionpool:Starting new HTTPS connection (1): api.kite.trade:443
DEBUG:urllib3.connectionpool:https://api.kite.trade:443 "GET /user/margins/equity HTTP/1.1" 200 None
Traceback (most recent call last):
File "C:\Users\surya\Downloads\Documents\HFT\ticker.py", line 26, in
websocket.subscribe([keyvalue['TRIDENT'][0], keyvalue['UNIONBANK'][0], keyvalue['IDBI'][0],
File "C:\Users\surya\AppData\Local\Programs\Python\Python310\lib\site-packages\kiteconnect\ticker.py", line 572, in subscribe
self.ws.sendMessage(
AttributeError: 'NoneType' object has no attribute 'sendMessage'

I am getting this error message, what is the problem exactly?
  • raptron
    I am getting this error from the Subscribe Method.
  • rakeshr
    websocket.subscribe([keyvalue['TRIDENT'][0], keyvalue['UNIONBANK'][0], keyvalue['IDBI'][0],
    AttributeError: 'NoneType' object has no attribute 'sendMessage'
    Have you initialised websocket properly? Make sure, you are assigning callbacks at end. Refer the sample websocket code here.
  • raptron
    Yes, I have initialized it properly and I have assigned callbacks, and yet I have this error.
  • SRIJAN
    Post your code. Maybe then we can find the error.
  • raptron
    import logging
    from kiteconnect import KiteTicker
    from Connect import data, kite, loginflow
    logging.basicConfig(level = logging.DEBUG)
    websocket = KiteTicker(api_key = loginflow['api_key'], access_token = data['access_token'], reconnect_max_delay = 5)
    websocket.connect(threaded = True)
    def on_connect(ws, response):
    logging.info(f'Connection Established: {response}')
    def on_order_update(ws, data):
    logging.info(f"Order I.D.: {data['order_id']}")
    def on_error(ws, reason):
    logging.error(f'Error: {reason}')
    def on_reconnect(ws, attempts_count):
    logging.warn(f'Reconnecting: {attempts_count}.')
    def on_noreconnect(ws):
    logging.error(f'Error 404.')
    def on_close(ws):
    logging.info(f'Session Terminated')
    websocket.on_connect = on_connect
    websocket.on_order_update = on_order_update
    websocket.on_error = on_error
    websocket.on_reconnect = on_reconnect
    websocket.on_noreconnect = on_noreconnect
    websocket.on_close = on_close
    keyvalue = {'TRIDENT': [2479361], 'UNIONBANK':[2752769], 'IDBI': [377857], 'BHANSALI':[107265], 'IEX': [56321]}
    websocket.subscribe([keyvalue['TRIDENT'][0], keyvalue['UNIONBANK'][0], keyvalue['IDBI'][0], keyvalue['BHANSALI'][0], keyvalue['IEX'][0]])

    This is the code
  • raptron
    @sujith @SRIJAN Any solutions whatsoever?
  • SRIJAN
    SRIJAN edited December 2021
    You are not doing it correctly. ws.susbcribe should be inside on_connect function.
  • SRIJAN
    SRIJAN edited December 2021
    Also, don't use threaded=True when connecting websocket if you don't need it. Otherwise,you have to use a infinite while loop to re-define the functions and assign callbacks. And websocket.connect() should be after assigning the callbacks not just after initializing websocket.
  • rakeshr
    The issue is you have created another daemon thread using threaded = True, but that thread is not handled properly using initialization and callbacks. If you don't need another ticker thread, you can avoid assigning threaded=True and use main ticker thread as shown in example.
    Refer to FAQs here for all python WebSocket-related queries.
  • raptron
    I am using a while loop, and I have inserted all the relevant pieces of code in it. But the websocket KiteTicker class can be defined outside the while loop right?
  • raptron
    raptron edited December 2021
    File "C:\Users\surya\Downloads\Documents\trade\trade.py", line 10, in computation
    if ticks['instrument_token'] == nse[i]:
    builtins.TypeError: list indices must be integers or slices, not str

    ERROR:kiteconnect.ticker:Connection closed: 1006 - connection was closed uncleanly (peer dropped the TCP connection without previous WebSocket closing handshake)
    INFO:root:Session Terminated: connection was closed uncleanly (peer dropped the TCP connection without previous WebSocket closing handshake)

    Currently I am getting this now, and seems to be a combination of things. Any directives so as to what is happening exactly?
    Am I passing the retrieval of the instrument_token value from ticks dictionary in the wrong manner?
  • SRIJAN
    SRIJAN edited December 2021
    You are not processing the ticks correctly. I told you that ticks is a list of dictionaries. So,it should be not ticks['instrument_token']==nse[i].
    It should be like:

    for tick in ticks:
    if tick['instrument_token']==nse[i]:
    pass
  • raptron
    Try that it doesn't work
  • SRIJAN
    @raptron Post your code again.
  • raptron
    Dictionaries cannot be called like this.
    for tick in ticks:
    if tick['instrument_token']==nse[i]:
    pass
    It has to be called like this:
    for key, value in ticks.items():
    pass
  • SRIJAN
    SRIJAN edited December 2021
    for k,v in dict.items() is used when you have to iterate over all the keys and values of a dictionary. In your case,you only need the instrument_token from every dictionary(tick) in the ticks. So,it will be called like i told in my above comment. The ticks received from websocket is a list of dictionaries,not a dictionary.
  • raptron
    Yeah you're right, thanks. @rakeshr you can close this thread.
This discussion has been closed.