Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/279.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何编写Twisted客户端插件_Python_Twisted - Fatal编程技术网

Python 如何编写Twisted客户端插件

Python 如何编写Twisted客户端插件,python,twisted,Python,Twisted,我使用了twisted来实现客户机。它很好用。现在我希望能够将命令行参数传递给它,因此我需要实现一个twisted插件。我已经进行了许多搜索,以找到显示如何将程序转换为插件的资源。然而,我找不到我想要的东西 以下是我的client.pycode的相关部分: import sys import time import os import errno import re from stat import * global runPath runPath = '/home/a02/Desktop/'

我使用了
twisted
来实现客户机。它很好用。现在我希望能够将命令行参数传递给它,因此我需要实现一个
twisted
插件。我已经进行了许多搜索,以找到显示如何将程序转换为插件的资源。然而,我找不到我想要的东西

以下是我的
client.py
code的相关部分:

import sys
import time
import os
import errno
import re
from stat import *
global runPath

runPath = '/home/a02/Desktop/'

from twisted.python import log
from GlobalVariables import *
from twisted.internet import reactor, threads, endpoints
from time import sleep
from twisted.internet.protocol import ClientFactory# , Protocol
from twisted.protocols import basic
import copy


class MyChat(basic.LineReceiver):
    def connectionMade(self):
        print "Connected to the server!"
        EchoClientFactory.buildClientObject(self.factory, self)
        self.runPythonCommands = RunPythonCommands ()
        return

    def connectionLost(self, reason):
        print "Lost Connection With Server!"
        #self.factory.clients.remove(self)
        #self.transport.loseConnection()
        print 'connection aborted!'
        #reactor.callFromThread(reactor.stop)
        reactor.stop ()
        return

    def lineReceived(self, line):
        #print "received", repr(line)
        line = line.strip ()
        if line == "EOF":
            print "Test Completed"
        self.factory.getLastReceivedMsg (line)
        exeResult = self.runPythonCommands.runPythonCommand(line.strip())

        self.sendLine(exeResult)
        EchoClientFactory.lastReceivedMessage = ""
        EchoClientFactory.clientObject[0].receivedMessages = []
        return

    def message (self, line):
        self.sendLine(line)
        #print line
        return


class EchoClientFactory(ClientFactory):
    protocol = MyChat
    clientObject = []
    lastReceivedMessage = ''

    def buildClientObject (self, newClient):
        client = Client ()
        self.clientObject.append(client)
        self.clientObject[0].objectProtocolInstance = newClient

        return

    def getLastReceivedMsg (self, message):
        self.lastReceivedMessage = message
        self.clientObject [0].lastReceivedMsg = message
        self.clientObject[0].receivedMessages.append (message)
        return 
    def connectionLost (self):
        reactor.stop()
        return

    def clientConnectionFailed(self, connector, reason):
        print 'Connection failed. Reason:', reason
        connector.connect () 
        return
以下是我为我的
客户端插件.py编写的内容:

from zope.interface import implements
from twisted.python import usage
from twisted.plugin import IPlugin
from twisted.application.service import IServiceMaker
from twisted.application.internet import TCPServer
from twisted.spread import pb
from slave.client import EchoClientFactory,MyChat, Client, RunPythonCommands, MyError, line


class Options(usage.Options):
    optParameters = [["port", "p", 8789, "The port number to listen on."], ["host", "h", "192.168.47.187", "The host to connect to"]]

class MyServiceMaker(object):
    implements(IServiceMaker, IPlugin)
    tapname = "echoclient"
    description = "Echo Client"
    options = Options

    def makeService(self, options):
        clientfactory = pb.PBServerFactory(EchoClientFactory ())
        return TCPServer(options["host"],int(options["port"]), clientfactory)

serviceMaker = MyServiceMaker()

我使用了文档中提到的相同文件夹层次结构。由于我在网上找不到足够多的插件示例,我真的被困在这一点上了。我将感谢任何帮助。有人能告诉我如何更改代码吗?提前谢谢。

您是否应该使用
tac
文件从命令行运行它


然后在另一个程序中使用您的
MyChat
类应该更容易…

您必须从
MyServiceMaker().makeService
方法返回一个主服务对象。尝试从twisted.internet导入服务添加
,然后在
makeService
的开头添加此项:
top\u service=service.Multiservice()
创建TCPServer服务:
tcp\u service=TCPServer(…)
将其添加到顶级服务:
tcp\u服务。setServiceParent(顶级服务)
然后返回顶级服务:
返回顶级服务


您可能还需要查看本系列优秀的教程(第16篇文章对您的问题非常有用)

只需按照以下链接获取文档和帮助:

输出:

C:\Python27>python.exe twist001.py
Response received

你能提供一个详细的解释,而不是你建议的方法的概要吗?我认为你的答案最好放在评论部分,在我看来。
C:\Python27>python.exe twist001.py
Response received