在 tornado TCPServer 的子类中调用 super.__init__()
Calling super.__init__() in a subclass of tornado TCPServer
我正在尝试从 tornado 中的 TCPServer class 继承,当我 运行 我的代码时,我一直收到此错误。我正在使用 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)
super
需要有关调用 class 的信息。在 Python 3 中,一旦您 调用 super
,就会自动提供此信息以及调用对象。您的代码 super.__init__
引用了通用超级对象上的一个插槽。
你要的是super
:
后面的括号
super().__init__()
我正在尝试从 tornado 中的 TCPServer class 继承,当我 运行 我的代码时,我一直收到此错误。我正在使用 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)
super
需要有关调用 class 的信息。在 Python 3 中,一旦您 调用 super
,就会自动提供此信息以及调用对象。您的代码 super.__init__
引用了通用超级对象上的一个插槽。
你要的是super
:
super().__init__()