我正在尝试从龙卷风中的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)
答案 0 :(得分:1)
super
需要有关调用类的信息。在Python 3中,一旦您调用 super
,就会自动提供此信息以及调用对象。您的代码super.__init__
指的是通用超级对象上的一个插槽。
您想要的是super
之后的括号:
super().__init__()