如何编写Twisted客户端插件

2024-05-23 16:30:55 发布

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

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

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

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

以下是我为我的client_plugin.py写的:

^{pr2}$

我使用了文档中提到的相同的文件夹层次结构。由于我在web上找不到足够的插件示例,我真的在这一点上卡住了。我会很感激你的帮助。谁能告诉我怎么改密码吗?提前谢谢你。在


Tags: fromimportselfclient插件messagereturnfactory
3条回答

必须从MyServiceMaker().makeService方法返回一个主服务对象。尝试添加from twisted.internet import service,然后在makeService中添加以下内容:top_service = service.Multiservice() 创建TCPServer服务:tcp_service = TCPServer(...) 将其添加到顶级服务:tcp_service.setServiceParent(top_service) 然后返回顶级服务:return top_service

您可能还需要看看Dave Peticolas中的这一系列优秀教程(第16篇文章对您的问题很有用)

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

twistedmatrix - docs

twisted.readthedocs.io -docs

from __future__ import print_function

from twisted.internet import reactor
from twisted.web.client import Agent
from twisted.web.http_headers import Headers

agent = Agent(reactor)

d = agent.request(
    'GET',
    'http://example.com/',
    Headers({'User-Agent': ['Twisted Web Client Example']}),
    None)

def cbResponse(ignored):
    print('Response received')
d.addCallback(cbResponse)

def cbShutdown(ignored):
    reactor.stop()
d.addBoth(cbShutdown)

reactor.run()

输出:

^{pr2}$

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

http://twistedmatrix.com/documents/12.2.0/core/howto/application.html#auto5

那么在另一个程序中使用你的MyChat类应该更容易。。。在

相关问题 更多 >