gpt4 book ai didi

python - 扭曲 - Python3 类型错误 : __init__() missing 1 required positional argument: 'factory'

转载 作者:行者123 更新时间:2023-12-03 01:06:09 27 4
gpt4 key购买 nike

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

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):
...

关于python - 扭曲 - Python3 类型错误 : __init__() missing 1 required positional argument: 'factory' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49163393/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com