Twisted client protocol - adding an interface to the interface

I follow the tutorial on writing a client / server pair in Twisted, located here:

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

I have everything that works to connect my client and server, the system uses custom prefixes to indicate how far it is in the “conversation”. First, it sends a json string to configure the session, and then sends the file line by line. This is really just an exercise more than anything else.

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() 

Server:

  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() 

I am currently running the server in one terminal and the client in another. As expected, both reactors are started and reported once, the client passes its json identifier, followed by the contents of the file, and then terminates the connection. I cannot figure out how to “connect” this protocol to something like the cmd interface in order to get these processes to run the command after completion. Since the reactor in the client runs indefinitely, it performs a network conversation once, and then simply runs continuously in the reactor.

Everything that I have ever created with Twisted, the client or the server, simply answered network calls, so I wonder what would be the right approach to making this a “one-time” client protocol, which is activated when entering commands from cmd . Is it even possible to use a reactor for something like that? Or is Twisted not at all for this, unlike just manually opening sockets that follow the protocol?

EDIT

I found the Twisted stdio library that fished on google and SO, but I had problems connecting the terminal input protocol to the network protocol.

I inherit the terminal protocol for LineReceiver , and the prompts are displayed correctly. I assigned the network factory to the factory terminal protocol, and then try to call the network protocol methods from the prompt. The problem is that the connector object assigned to this property does not use any of the protocol methods that it was supposed to create.

I put it on GitHub for short:

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

0
python twisted client
source share
1 answer

No No external wiring is required to simultaneously connect both servers and client services.

Twisted provides the necessary tools to ensure that it can launch all of your services and reset them when you complete the twisted application. This makes it possible to do this.

Read this great twisted application tutorial:

Read more below:

In SO:

  • Twisted application without twistd
+1
source share

All Articles