[Twisted-Python] how to write an interactive client?

july july july.lzu at gmail.com
Sun May 29 12:49:59 MDT 2005


On 5/21/05, Jp Calderone <exarkun at divmod.com> wrote:
> On Fri, 20 May 2005 16:04:50 -0700, theshz <theshz at gmail.com> wrote:
> >Hi, I've written a simple twisted server, which accepts client connections,
> >and respond with some simple text depending on what the client sends. This
> >is working fine if I use telnet to connect to the server, and enter the
> >commands one at a time. I'd like to write a twisted client to simulate this,
> >i.e., the client has a list of commands to send, it will send one at a time,
> >(may be wait a little bit between the commands,) and print out the responses
> >from the server. I couldn't seem to get beyond the first command, following
> >code seems to send all the commands at the end, rather than one at a time,
> >how can I add sleep in between? Thanks:
> >
> >from twisted.internet.protocol import ReconnectingClientFactory
> >from twisted.protocols import basic
> >from twisted.internet import reactor
> >from sys import stdout
> >import time
> >
> >class MyClientProtocol(basic.LineReceiver):
> > def lineReceived(self,data):
> >  stdout.write("Server:" + data+"\n"),
> >
> > def connectionMade(self):
> >      stdout.write("connectionMade\n")
> >      self.transport.write("start:\r\n")
> >      self.transport.write("command1:\r\n")
> >      self.transport.write("command2:\r\n")
> >      self.transport.write("command3:\r\n")
> >      self.transport.write("end:\r\n")
> >
> 
>  Try this version of connectionMade, along with this definition of lineReceived:
> 
>    lines = ["command1", "command2", "command3", "end"]
>    def connectionMade(self):
>        print "connectionMade"
>        self.lines = self.lines[:]
>        self.sendLine("start:")
> 
>    def lineReceived(self, line):
>        print "Got a line:", repr(line)
>        if self.lines:
>            self.sendLine(self.lines.pop(0) + ":")
> 
>  Of course, there are other ways to do this.  You could respond to timing events instead of network events:
> 
>    lines = ["command1", "command2", "command3", "end"]
>    def connectionMade(self):
>        print "connectionMade"
>        self.lines = self.lines[:]
>        self.sendCommand()
> 
>    def sendCommand(self):
>        self.sendLine(self.lines.pop(0) + ":")
>        if self.lines:
>            reactor.callLater(3, self.sendCommand)
> 
>  Or you could respond to events from stdin, or from another connected protocol, or a GUI, or ....
> 
>  Hope this helps,
> 
>  Jp
> 
> _______________________________________________
> Twisted-Python mailing list
> Twisted-Python at twistedmatrix.com
> http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python
> 
okey , I think it's hard to parse command line in twisted reactor
event loopst . In my programs ,  I always 'import cmd' and in between
command , use reacotr.run() to send a message ,and waiting for a
replay when the replay comes back, the protocol() will terminate the
connection . and back into cmd loop.
Hope this helps,   ZP




More information about the Twisted-Python mailing list