在tornado TCPServer的子类中调用super .__ init __()

时间:2018-04-03 20:11:48

标签: python tornado super

我正在尝试从龙卷风中的TCPServer类继承,并且在运行代码时我不断收到此错误。我正在使用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 

我有以下代码:

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

我甚至尝试在super。 init ()

中添加参数

已更改

super.__init__()

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

1 个答案:

答案 0 :(得分:1)

super需要有关调用类的信息。在Python 3中,一旦您调用 super,就会自动提供此信息以及调用对象。您的代码super.__init__指的是通用超级对象上的一个插槽。

您想要的是super之后的括号:

super().__init__()