扭曲的客户端协议-附加接口前端 [英] Twisted client protocol - attaching an interface frontend

查看:128
本文介绍了扭曲的客户端协议-附加接口前端的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在按照位于Twisted上编写客户端/服务器对的教程进行操作:

I am following the tutorial on writing a client/server pair in Twisted located here:

http://twistedmatrix.com/documents/current/core/howto/clients.html

我的所有工作都可以与客户端和服务器进行通信,系统使用自定义前缀来表示它与会话"的距离.它首先发送json字符串以建立会话,然后逐行发送文件.实际上,这只是一项锻炼,而不是其他任何事情.

I have everything working for the communication of my client and server, the system uses custom prefixes to denote how far it is into the 'conversation'. It sends a json string initially to set up the session, and then sends a file line by line. It's really just an exercise more than anything else.

Client.py:

Client.py:

class ClientProtocol(protocol.Protocol):

    def connectionMade(self):
        json_string = json.dumps({
            'uid':ObjectId(),
            'lat':43.78,
            'lon':-72.5831
        },cls=Encoder)
        self.transport.write('OPN|'+json_string)
        self.fileObject = open('test.csv')

    def sendFile(self):
        lineData = self.fileObject.readline()
        if lineData != '':
            self.transport.write('UPD|')
            self.transport.write(lineData)
        else:
            self.transport.write('EOF|done')

    def dataReceived(self,data):
        if data in ['ELLO','RECVD']:
            self.sendFile()


class ClientFactory(protocol.Factory):
    def buildProtocol(self,addr):
        return ClientProtocol()

if __name__ == '__main__':
    point = TCP4ClientEndpoint(reactor,'localhost',5000)
    d = point.connect(ClientFactory())
    reactor.run()

服务器:

    class TransferProtocol(protocol.Protocol):

        ran = 0

        def connectionLost(self,reason):
            print reason

        def connectionMade(self):
            self.fileObject = open('output','w')

        def dataReceived(self,data):
            methods = {'OPN':'open','UPD':'upload','EOF':'endfile'}
            if data[0:3] in methods.keys():
                func = getattr(self,methods[data[0:3]])
                func(data[4:])

        def open(self,data):
            print 'OPEN %s' % data
            self.transport.write('ELLO')

        def endfile(self,data):
            print 'END %s' % data
            self.transport.loseConnection()

        def upload(self,data):
            self.ran+=1
            self.fileObject.write(data)
            self.transport.write('RECVD')

class myProtocolFactory(protocol.Factory):
    protocol = TransferProtocol

    def doStart(self):
        pass

    def startedConnecting(self, connectorInstance):
        print connectorInstance

    def buildProtocol(self, address):
        print address
        return self.protocol()

    def clientConnectionLost(self, connection, reason):
        print reason
        print connection

    def clientConnectionFailed(self, connection, reason):
        print connection
        print reason

    def doStop(self):
        pass

if __name__ == '__main__':
    reactor.listenTCP(5000, myProtocolFactory())
    reactor.run()

当前,我在一个终端中运行服务器,而在另一终端中运行客户端.正如预期的那样,两个反应堆都启动并通信一次,客户端先传输其身份json,然后传输文件内容,然后终止连接.我无法理解的是如何将该协议连接"到cmd接口之类的东西,以使其在结束后在命令中产生这些进程.由于客户端中的反应堆会无限期运行,因此它会执行一次网络对话,然后在反应堆中不断循环.

Currently, I run the server in one terminal and the client in another. As expected, both reactors start and communicate once, the client transmits it's identity json followed by the file contents and then terminates the connection. What I can't understand is how to 'hook up' this protocol to something like the cmd interface, to make it spawn these processes on command after one ends. Because the reactor in the client runs indefinitely, it performs the network conversation once and then just loops through continuously in the reactor.

我用Twisted构建的所有东西,无论是客户端还是服务器,都只是响应网络调用,所以我想知道什么是使它成为一次性"客户端协议并在来自输入命令的情况下激活的正确方法吗? cmd.有人甚至会用电抗器来做这样的事情吗?还是完全扭曲,而不是按照协议手动打开套接字?

Everything I've ever build with Twisted, client or server, just responded to network calls, so I am wondering what would be the correct approach to making this a "one-shot" client protocol that would activate on input commands from cmd. Would one even use the reactor for something like this? Or Twisted at all for that matter as opposed to just manually opening sockets that follow the protocol?

编辑

我在Google和SO上发现了Twisted stdio库,但是我无法将终端输入协议挂接到网络协议上.

I've found the Twisted stdio library fishing around on google and SO, but I'm having trouble hooking the terminal input protocol up to the network protocol.

我有LineReceiver的终端协议继承,并且提示正确显示.我已将网络工厂分配给终端协议的factory对象,然后尝试从提示中调用网络协议的方法.问题是,分配给该属性的连接器对象没有使用它应该创建的协议的任何方法.

I have the terminal protocol inherit for LineReceiver, and the prompts show correctly. I've assigned the network factory to the terminal protocol's factory object, and then attempt to call methods of the network protocol from the prompt. The problem is, the connector object assigned to this property doesn't use any of the methods of the protocol it's supposed to create.

为了简洁起见,我将其放在GitHub上:

I've put it on GitHub for brevity's sake:

https://github.com/DeaconDesperado/swfty-share/blob/master/client.py

推荐答案

否不需要外部布线即可将服务器和客户端服务组合在一起.

No There is no need for external wiring for bringing both your server and client services up together.

Twisted提供了必要的手段,以确保它可以启动所有服务并在关闭扭曲的应用程序时将其关闭.它提供了执行此操作的工具.

Twisted provides the necessary means to ensure that it can start all your services and bring them down when you shut down a twisted application. It provides the facility to do this.

阅读有关扭曲应用程序的出色教程:

Read this excellent tutorial on twisted application:

  1. http://krondo.com/?p=2345

阅读以下内容以获取更多详细信息:

Read the following for more details:

  1. http://twistedmatrix.com/documents/current/core/howto/basics.html
  2. http://twistedmatrix.com/documents/current/core/howto/application.html
  3. http://twistedmatrix.com/documents/current/core/howto/plugin.html
  4. http://twistedmatrix.com/documents/current/core/howto/tap.html
  1. http://twistedmatrix.com/documents/current/core/howto/basics.html
  2. http://twistedmatrix.com/documents/current/core/howto/application.html
  3. http://twistedmatrix.com/documents/current/core/howto/plugin.html
  4. http://twistedmatrix.com/documents/current/core/howto/tap.html

关于SO:

  1. 没有扭曲的扭曲应用

这篇关于扭曲的客户端协议-附加接口前端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆