Talking through pipes interactively (on win32)
aleax at aleax.it
Mon Nov 10 17:12:35 CET 2003
Thorsten Pferdekämper wrote:
> "Jane Austine" <janeaustine50 at hotmail.com> wrote in message
> news:ba1e306f.0311091118.24d80000 at posting.google.com...
>> I need to control a command line program via python.
>> I first tried popen2 and 3 but I couldn't find a way to talk to the
>> subprocess interactively; that is, read some and then write some, and
>> read some more again... (suppose controlling an interactive shell
>> program) When I try this with popen family, it just hangs. I have to
>> close one pipe before reading from the other.
>> I know that there is a wonderful thing called Expect, but it's not
>> available on my machine(win32).
>> Can anyone help me out?
> in "Python in a nutshell", I have found a remark about that. It seems that
> this depends on some buffering mechanisms the programs use. The program
> you are running with popen just waits until the input stream is closed. As
> long as you can not control the behaviour of this program, you hardly can
> do anything, espacially on win32.
Right: there is, indeed, very little you can do in the general case. If a
"commandline program" buffers its stdio when it's talking to pipes rather
than a terminal, then the only way to drive its input AND read its output
"a little at a time" is to trick it into believing that it's talking to a
terminal -- not easy on Windows. Unfortunately, typical C runtime libraries
implementing stdio _DO_ behave exactly like this -- buffering except when
talking to a terminal...
> Perhaps you can try spawn().
That would not help at all -- it cannot defeat buffering.
One thing that COULD defeat buffering, IF your particular win32 machine
supports a telnet-server (but I think you may need some extra-cost MS
add-on for that), would be for your Python program to "telnet to localhost"
and run the commandline program that way.
Alternatively, you MIGHT be able to find a version of 'expect' that
works with your machine. Google may help; e.g., try
Expect is a Tcl/Tk add-on, not a Python one, but if you can use it, it
can still open avenues towards possible solutions.
More information about the Python-list