简体   繁体   中英

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

I'm writing some python software that polls a modbus connection and stores the data in an SQL database. To see if the machine is working as a quick test, I'm making a simple console style "HMI" that you can telnet into. Here is the offending code, and error.

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()

The error is:

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'

Thanks in advance for any help!

This may be related to your problem:

In this section, you call Screen() without any arguments:

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

However, earlier you defined Screen to take a factory argument:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM