Twisted - Python3 TypeError: __init__() missing 1 required positional argument: 'factory'

Twisted - Python3 TypeError: __init__() missing 1 required positional argument: 'factory'

我正在编写一些 python 轮询 modbus 连接并将数据存储在 SQL 数据库中的软件。为了快速测试机器是否正常工作,我制作了一个简单的控制台样式 "HMI",您可以通过 telnet 进入。这是有问题的代码和错误。

from twisted.internet.protocol import Protocol, Factory
from twisted.internet.endpoints import TCP4ServerEndpoint
from twisted.internet import reactor
from twisted.internet import task
from twisted.internet import protocol

#from screenhelp import ScreenHelp ** Not relevant to error


class Screen(Protocol):

    def __init__(self, factory):
        self.factory = factory
        self.connection = False
        self.loop = task.LoopingCall(screenRefresh)
        self.loopDeferred = None
        self.stack = []
        self.cs = ScreenHelp()

    def connectionMade(self):
        self.loopDeferred = self.loop.start(self,5)

    def connectionLost(self, reason):
        self.loop.stop(self)

    def dataReceived(self, data):
        self.stack.append(data)

    def screenRefresh(self):

        #self.transport.write(self.cs.clr()) ** Not relevent - issues clear screen command.
        for x in self.stack:
            self.transport.write(x)

class ScreenFactory(Factory):
    def buildProtocol(self, addr):
        return Screen()


def main():
    endpoint1 = TCP4ServerEndpoint(reactor, 64000)
    endpoint1.listen(ScreenFactory)
    reactor.listenTCP(5000, fact)
    reactor.run()


if __name__ == "__main__":
     main()

错误是:

File "/usr/lib/python3/dist-packages/twisted/internet/protocol.py", line 135, in buildProtocol
    p = self.protocol()
TypeError: __init__() missing 1 required positional argument: 'factory'

在此先感谢您的帮助!

这可能与您的问题有关:

在本节中,您调用 Screen() 时不带任何参数:

class ScreenFactory(Factory):
    def buildProtocol(self, addr):
        return Screen()

但是,之前您定义 Screen 以采用 factory 参数:

class Screen(Protocol):
    def __init__(self, factory):
        ...