Search code examples
pythontornadosuper

Calling super.__init__() in a subclass of tornado TCPServer


I am trying to inherit from the TCPServer class in tornado, and I keep getting this error when I run my code. I am using python3.6

Traceback (most recent call last):
  File "tornado_collector.py", line 38, in <module>
    main()
  File "tornado_collector.py", line 30, in main
    server = TelemetryServer()
  File "tornado_collector.py", line 9, in __init__
    super.__init__()
TypeError: descriptor '__init__' of 'super' object needs an argument 

I have the following code:

from tornado.tcpserver import TCPServer
from tornado.iostream import StreamClosedError
from tornado import gen
from tornado.ioloop import IOLoop
from struct import Struct, unpack

class MyServer(TCPServer):
    def __init__(self):
        super.__init__()
        self.header_size = 12
        self.header_struct = Struct('>hhhhi')
        self._UNPACK_HEADER = self.header_struct.unpack

    @gen.coroutine
    def handle_stream(self, stream, address):
    print(f"Got connection from {address}")
        while True:
            try:
                header_data = yield stream.read_bytes(self.header_size)
                msg_type, encode_type, msg_version, flags, msg_length = self._UNPACK_HEADER(header_data)
                print(header_data)
            data = yield stream.read_until(b"\n")
                print(data)
                yield stream.write(data)
            except StreamClosedError:
                break

I have even tried adding arguments to the super.init()

Changed

super.__init__()

To

super.__init__(ssl_options=None, max_buffer_size=None, read_chunk_size=None)

Solution

  • super needs the information about the calling class. In Python 3, this information - along with the calling object - is automatically given once you call super. Your code super.__init__ refers to a slot on the generic super object.

    What you want is parentheses after super:

    super().__init__()