Controlling an external program with Python

Paul Duffin pduffin at hursley.ibm.com
Tue Jun 27 06:51:45 EDT 2000


Donn Cave wrote:
> 
> Quoth ge at nowhere.none (Grant Edwards):
> | In article <kuzoobue0g.fsf at lasipalatsi.fi>, Erno Kuusela wrote:
> |>>>>>> "Tony" == Tony Keating <keating at mech.uq.edu.au> writes:
> |>
> |>    Tony> start the program, then send various strings to the program,
> |>    Tony> read some output from the program then send some more
> |>    Tony> strings to the program.
> |>
> |> there are a couple of expect modules for python (so named because
> |> there is an old tcl-based language called expect for doing just this).
> |> parnassus lists 3 modules (<URL:http://www.vex.net/parnassus/
> |> apyllo.py?find=expect>), i haven't
> |> used any of them however...
> |
> | Or you can roll your own using the popen2 module.
> 

I think that you will find that Expect offers a lot more than a simple 
pipe to the executable.

> OK, but check to see if your application will work reliably on
> a pipe, before you put too much work a pipe solution.  I append
> a short sample program that works with the shell as a remote
> application, but deadlocks with awk.  The awk behavior is what
> I would expect from most programs, so normally you're sunk.
> 

The main problem is that most interactive programs rely on the C library's
setting of the buffering of the stdout IO channels. Basically when 
stdout is connected to a tty it has line buffering but when connected to 
a pipe, or a socket, or any other type of channel the buffering is normally 
set to about 4K.

If you have access to the source of the interactive program then you can
simply add a call to setlinebuf for stdout; stderr is always line 
buffered. If not then the only solution is to use something like Expect
(actually there is nothing like it) which uses pseudo ttys to ensure
correct behaviour and also has some really nice buffer handling.

Obviously you could do all this and the GUI in Tcl/Tk but as you are
asking this about Python then you may want to look into Python versions
of Expect. If they are not satisfactory then you could probably write
a little Tcl/Expect wrapper around the application which forces the
correct buffering and then control that using Python.



More information about the Python-list mailing list