popen, Pipes with programs that expect user input

A

Alex

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
 

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

Forum statistics

Threads
473,995
Messages
2,570,226
Members
46,815
Latest member
treekmostly22

Latest Threads

Top