工厂实例没有属性“startedConnecting”

2024-09-29 23:17:28 发布

您现在位置:Python中文网/ 问答频道 /正文

我找了很多遍,但没有发现这样的错误。当我执行我的代码(如下)时,我得到一个异常ControllerFactory instance has no attribute 'startedConnecting'。我尝试添加一个方法,它的主体仅仅是pass,但这只会导致它在没有传输任何信息的情况下暂停,这使我相信问题出在我设置类的方式上。在

此代码基于twisted网站的代码。它意味着能够传输python文件,服务器保存这些文件,然后传输运行python文件的参数。在

#!/usr/bin/env python                                                                                                                                                                                        
from twisted.internet import reactor, protocol
import argparse

file_header = "pfile:"
run_header = "runwith:"

class Controller(protocol.Protocol):
    def sendMessage(self,message):
        self.transport.write(message)


class ControllerFactory(protocol.Factory):
    def buildProtocol(self, addr):
        cont = Controller()
        cont.factory = self
        return cont


if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument("--address")
    parser.add_argument("--file")
    parser.add_argument("--args")
    args = parser.parse_args()

    if(args.file and args.args):
        raise Exception("Can't send file and args at same time.")
    reactor.connectTCP(args.address, 1337, ControllerFactory())
    reactor.run()
    if(args.file):
        print(args.file)
        a = open(args.file)
        factory.connectedProtocol.sendMessage(file_header + a.read())
        a.close()
    if(args.args):
        print(args.args)
        factory.connectedProtocol.sendMessage(run_header + args.args)

Tags: 文件run代码selfparseriffactoryargs
1条回答
网友
1楼 · 发布于 2024-09-29 23:17:28

对客户端使用twisted.internet.protocol.ClientFactory(而不是twisted.internet.protocol.Factory)。或者使用twisted.internet.endpoints而不是twisted.internet.reactor.connectTCP。在

另外,请注意reactor.run()块。在那一行后面的所有代码都不会以任何有用的方式运行。在

相关问题 更多 >

    热门问题