Talking through pipes interactively (on win32)

J

Jane Austine

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?

Jane
 
T

Thorsten Pferdekämper

Jane Austine said:
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?

Hi,
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.
Perhaps you can try spawn().
Kind Regards,
Thorsten
 
B

Brian Kelley

Jane said:
I need to control a command line program via python.

wxPython has a wxProcess class that makes this fairly easy. It is also
portable between unix and windows (not macosX) the downside is that you
need to run it in a wxWindows GUI environment.

The other potential is to figure out how to use win32 Processes which
are fairly hairy. The technical issue at hand is that under win32 you
can't use python's select library on file objects or standard in or
standard out pipes which makes controlling them fairly difficult.

You can see a good demo of wxProcess in the wxPython demo. The upside
is that wxProcess doesn't require a gui to be visible, just a wxPython
application environment, the downside is that wxPython is fairly large.

Hope this helps.
 
E

Eric Brunel

Jane said:
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?

Can you please post a small script showing the exact problem you have?
Communicating with a program via pipes works on Windows: we do it all the time.
It can be pretty complicated, but it can be done.
 
J

Jane Austine

Can you please post a small script showing the exact problem you have?
Communicating with a program via pipes works on Windows: we do it all the
time. It can be pretty complicated, but it can be done.

Okay. For example, python interactive shell:

....(it hangs from here)...

However, it works if sout is closed before reading from sin:'6'
 
K

K_Lee

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?

Jane

Expect is availabe on Win32. I have used it and it works
very well.
 
A

Alex Martelli

Thorsten said:
Hi,
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
http://bmrc.berkeley.edu/people/chaffee/expectnt.html

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.


Alex
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,169
Messages
2,570,920
Members
47,464
Latest member
Bobbylenly

Latest Threads

Top