473,387 Members | 1,374 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Talking through pipes interactively (on win32)

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
Jul 18 '05 #1
3 3488
> 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:
import os
sout,sin=os.popen2('python -u')
sout.write('print 2*3\n')
sout.flush()
sin.read(1)
....(it hangs from here)...

However, it works if sout is closed before reading from sin: import os
sout,sin=os.popen2('python -u')
sout.write('print 2*3\n')
sout.close()
sin.read(1)

'6'
Jul 18 '05 #2
ja***********@hotmail.com (Jane Austine) wrote in message news:<ba**************************@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?

Jane


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

--
SLink generates hyperlinks for all functions, macros, variables from
thousands of src file. Browse hyperlinked source code for .NET CLI,
Mozilla, Apache, NetBSD, Ethereal, Chorus OS, TCL, Python, PHP at
http://www.slink-software.com?E=SLinkEvn_1004
Jul 18 '05 #3
Thorsten Pferdekämper wrote:
"Jane Austine" <ja***********@hotmail.com> wrote in message
news:ba**************************@posting.google.c om...
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.


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

Jul 18 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Daniel Greenblatt | last post by:
Has anyone used the SocketServer.py module successfully on windows? I don't know if I'm doing something terribly wrong, here but I use a SocketServer in my app, running it in a seperate thread,...
2
by: Nir of the Waves | last post by:
In windows platforms how can I make two processes hold two ends of the same pipe? os.pipe() returns reader and writer descriptors but how do I export one of the ends to another process? ...
2
by: Tim Black | last post by:
In my recent experience, popen os pipes always fail when cwd is a UNC path. Can anyone shed any light on this? Although I've seen lots of UNC path-related problems in this newsgroup, I've not been...
3
by: David Douard | last post by:
Hi everybody, let me explain by problem: I am working on an application which consists in a C++ dll (numeric computations) and a Python IHM (Python/Tk), which must run under Linux and win32. My...
1
by: piyush | last post by:
I am in the process of deciding the IPC mechanisms to use for communication between 1) An application API (which would be set up as a NT service). It could be C++ or C#. The API as such might be...
6
by: Laszlo Zsolt Nagy | last post by:
Sorry, I realized that the import zlib was not executed from my (working) service. So here is the question: why can't I use zlib from a win32 service? Is there any way to make it working? ...
9
by: Hans J?rg Brinksmeyer | last post by:
Hi, does anyone have an idea for this problem: I use anonymous pipes to steer a console program under Win2000 with a second 'steering aplication'. The stdin and output are redirected to...
4
by: Ken Allen | last post by:
Is there any built-in facility for handling named pipes in C#/.Net, or must one use unsafe code to access the WIN32 API directly? There exists some code that uses named pipes heavily and there...
0
by: piyush | last post by:
Sorry for repeated posts, I couldnt get things right and complete in the previous post. I am in the process of deciding the IPC mechanisms to use for communication between 1) An application...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.