473,406 Members | 2,847 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,406 software developers and data experts.

popen, Pipes with programs that expect user input

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
0 1057

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

Similar topics

0
by: Mark Sandler | last post by:
Hi, When i create a process using popen and then if i decide that i am not interested in keeping the process pipes anymore, and I forget the correspond variables . Then, if python tries to destroy...
4
by: Kevin Walzer | last post by:
I'm trying to structure a Python script that streams output over a pipe. Here is my code: import os cmd = os.popen('echo foo | sudo -S /usr/sbin/tcpdump -en1') cmd.read() This returns...
8
by: dmoore | last post by:
Hi folks, I've seen the following issue come up in multiple posts to this mailing list: I have a python program that spawns a child process with popen or popen2 or popen3 or popen2.popen2...
25
by: Jeremy Banks | last post by:
Hi. I wondered if anyone knew the rationale behind the naming of the Popen class in the subprocess module. Popen sounds like the a suitable name for a function that created a subprocess, but the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.