
On Fri, 20 May 2005 16:04:50 -0700, theshz <theshz@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