[Twisted-Python] Conch: multiple commands
Paul Swartz
paulswartz at gmail.com
Thu Feb 9 08:14:20 MST 2006
Brendan Simon wrote:
> I'm trying to use and understand the latest Twisted and Conch (I'm a
> newbie). I can get teh sample apps going fine but they all seem to send
> one command and then exit. I'm trying to workout how to send multiple
> commands to the server from the same client application.
>
> The sshsimpleclient.py does send multiple commands (false, true, cat)
> with separate channels.
> Is that the preferred way to send multiple commands?
> Doesn't that require some setup overhead every time I want to issue a
> command to the server?
That's the way SSH requires sending multiple commands. It does require
some server overhead, but there's no way around that.
> Is it possible to open _one_ channel, and send commands in a
> synchronously? ie. wait for commands to return output and then issue
> other commands if necesary??
> Maybe this goes against the grain of the Twisted Asynchronous Framework ???
You can send one command at a time, but you still have to write the code
asynchronously.
> If I assume that I have to open a new channel for each command, and I
> have a 100 different commands, I could have 100 classes for each command
> (seems a bit extreme) or have one class that gets the command passed to
> it. The problem I see with the latter is that I don't know how to wait
> until the first command has got a reply and is finished. It seems that
> the channelOpen method returns straight away. How can I control the
> program execution to wait for the response, error or timeout? Example:
> def servicesStarted(self):
> self.openChannel(CommandChannel.openChannel('ls /tmp/dir1')
> # how do I wait for list of dir1 to finish?
> self.openChannel(CommandChannel.openChannel('ls /tmp/dir2')
> # how do I wait for list of dir2 to finish?
> self.openChannel(CommandChannel.openChannel('ls /tmp/dir3')
> # how do I wait for list of dir3 to finish?
>
What I'd do is also pass a Deferred to CommandChannel, that is
call/errbacked when the command finishes.
def servicesStarted(self):
d = defer.Deferred()
d.addCallback(self._cbFirstLs)
d.addErrback(self._ebFirstLs)
self.openChannel(CommandChannel(d, "ls /tmp/dir1"))
def _cbFirstLs(self, result):
print 'directory listing', result
d = defer.Deferred()
self.openChannel(CommandChannel(d, "ls /tmp/dir2"))
def _ebFirstLs(self, f):
log.err()
etc., for all the commands you want to run.
-p
--
Paul Swartz
paulswartz at gmail dot com
http://z3p.livejournal.com/
AIM: z3penguin
GPG: 5CF0B1C9
More information about the Twisted-Python
mailing list