我已经看了很多,但没有发现这样的错误。当我执行我的代码(下面)时,我得到异常ControllerFactory instance has no attribute 'startedConnecting'
。我尝试添加方法,身体只是pass
,但这只会导致它停止而不传输任何东西,让我相信问题在于我设置类的方式。
此代码基于扭曲网站的代码。它意味着能够传输服务器保存的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)
答案 0 :(得分:2)
对客户端使用twisted.internet.protocol.ClientFactory
(而不是twisted.internet.protocol.Factory
)。或者使用twisted.internet.endpoints
代替twisted.internet.reactor.connectTCP
的内容。
另外,请注意reactor.run()
阻止。跟在该行之后的所有代码都不会以任何有用的方式运行。