By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,235 Members | 1,022 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,235 IT Pros & Developers. It's quick & easy.

popen, Pipes with programs that expect user input

P: n/a
Hello everyone,
I am writing a terminal server client-server application, that offers
the client the ability to run commands on the server and read their
output.

So far everything works fine, but I encounter a problem with commands
which require some sort of user input; i.e. they don't return
immediately.

This is my setup:
Python 2.4.3 (#69, Apr 11 2006, 15:32:42) [MSC v.1310 32 bit (Intel)]
on win32
---------------------
import os

def myExecEx(command):
"""Executes a command and returns a tuple (stdin,
stdout_stderr)"""
outErrFile, inFile = os.popen4(command)

return (inFile, outErrFile)
fouterr, fin = myExecEx('date') # try 'date /t' as well
#fin.write('\n')

try:
data=fouterr.read()
print data
except:
print "an exception occurred"
---------------------

On Windows, the 'date' command will show the current date, and then
prompt me to enter a new one (thus waiting for something to come to
STDIN)

I am reading the output with:
data=fouterr.read()

but data is empty (also, I must mention that "an exception occurred"
is not shown)

If I execute
myExecEx('date /t')

(the /t parameter tells the date tool that it has to print the current
date and not wait for user input), then the program works as
expected).

Also, I noticed that if I uncomment the line in which I wrote to
STDIN:
fin.write('\n')

then I can read STDOUT without problems (but this is counter-intuitive
to me; I don't know what to write to STDIN before I see what STDOUT
has to say).
I have tried other commands from the popen family, but in either case
the behaviour was the same.
Could someone point out the cause of this? It seems to me that the
interpreter is stuck at this line
data=fouterr.read()

and it won't go forward unless popen returns. If so, how should the
objective be achieved?
Thank you

Jan 8 '07 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.