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

Wrapping A Shell

I'm not sure if this is really the right place to ask this question, but
since the implementation is in Python, I figured I'd give it a shot.

I want to "wrap" a shell process using popen inside of python program
rather than creating a new shell process for each line I process in the
app. For example, the code might look like:
std = stdin, stdout, stderr = os.popen3("bash")

print >stdin, "ls"

print stdout.readline()

However, it appears my understanding of popen (or perhaps buffered IO)
is off somewhere, because this certainly doesn't work anything like I
expect it to (it hangs on stdout.readline).

Obviously the example above is very contrived, but eventually I'll be
using this in an OpenGL "terminal" widget. Am I approaching this the
wrong way?

Nov 28 '06 #1
2 3961

Jeremy Moles wrote:
I'm not sure if this is really the right place to ask this question, but
since the implementation is in Python, I figured I'd give it a shot.

I want to "wrap" a shell process using popen inside of python program
rather than creating a new shell process for each line I process in the
app. For example, the code might look like:
std = stdin, stdout, stderr = os.popen3("bash")

print >stdin, "ls"

print stdout.readline()

However, it appears my understanding of popen (or perhaps buffered IO)
is off somewhere, because this certainly doesn't work anything like I
expect it to (it hangs on stdout.readline).

Obviously the example above is very contrived, but eventually I'll be
using this in an OpenGL "terminal" widget. Am I approaching this the
wrong way?
Try flushing the bufffer for stdin.
Cheers,
-T

Nov 28 '06 #2
Jeremy Moles <je****@emperorlinux.comwrote:
I'm not sure if this is really the right place to ask this question, but
since the implementation is in Python, I figured I'd give it a shot.

I want to "wrap" a shell process using popen inside of python program
rather than creating a new shell process for each line I process in the
app. For example, the code might look like:
std = stdin, stdout, stderr = os.popen3("bash")

print >stdin, "ls"

print stdout.readline()

However, it appears my understanding of popen (or perhaps buffered IO)
is off somewhere, because this certainly doesn't work anything like I
expect it to (it hangs on stdout.readline).

Obviously the example above is very contrived, but eventually I'll be
using this in an OpenGL "terminal" widget. Am I approaching this the
wrong way?
Short answer: use pexpect

http://pexpect.sourceforge.net/

Long answer:

Firstly modern pythonistas use subprocess instead of os.popen*.

Secondly, buffering is going to cause you trouble and possible
deadlocks.

There are two ways to rid yourself of deadlock.

1) read and write from seperate threads / processes. This is the
traditional unix way

2) use non blocking IO

Here is a possible way to solve the problems with subprocess (unix
only) and non blocking IO.

However I suspect you really want to use pexpect for this job...

from os import O_NONBLOCK
from errno import EAGAIN
from subprocess import Popen, PIPE
from fcntl import fcntl, F_SETFL, F_GETFL
from time import time

p = Popen(["/bin/bash", "-i"], shell=False, bufsize=0, stdin=PIPE, stdout=PIPE, stderr=PIPE, close_fds=True)

# Make output streams non blocking (unix only)
for fd in (p.stdout.fileno(), p.stderr.fileno()):
fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | O_NONBLOCK)

def read_output(p, timeout=1.0):
"""Read the output from the subprocess, with timeout"""
end_time = time() + timeout
output = ""
while time() < end_time:
try:
output += p.stdout.read(1024)
except IOError, e:
if e.errno != EAGAIN:
raise
return output

p.stdin.write("ls\n")
print read_output(p)
p.stdin.write("uname -a\n")
print read_output(p)

--
Nick Craig-Wood <ni**@craig-wood.com-- http://www.craig-wood.com/nick
Nov 29 '06 #3

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

Similar topics

13
by: Roy Smith | last post by:
I've got a C library with about 50 calls in it that I want to wrap in Python. I know I could use some tool like SWIG, but that will give me a too-literal translation; I want to make some...
11
by: yawnmoth | last post by:
word wrapping normally treats some spaces as line feeds, if there hasn't been a line feed for quite a while. so while a string with eighty consecutive a's might not word wrap, a space placed...
8
by: Siemel Naran | last post by:
Hi. I'm writing a command shell that reads commands from standard input. At this point I have the command in a std::string. Now I want to execute this command in the shell. From the Borland...
5
by: nimdez | last post by:
Hi, I am working on an existing code base in which a lot of data displayed to the user is formatted in tables. Most tables are printed row-by-row using printf() with "%s" print conversion...
7
by: S Borg | last post by:
Hello, I am parsing text from one document to another. I have a scheme similar to: for x in myfoobar: print >> mytextfile, "%s " % mydictionary, #all on same line print >> mytextfile,...
8
by: Nathan | last post by:
I am trying to prevent a horizontal list from wrapping. Each list item is floated with "float: left". Currently I use an ugly hack. I set the width of the list to a large number which is...
3
by: Rocky Zhou | last post by:
I wonder is there any way to make the wrapper program can wrap options && arguments for the the subprocess/command the wrapper will execute? by getopt or optparse module? This is something...
21
by: Tom Gur | last post by:
Hi, It's seems that csh and tcsh acts a bit different when handling special characters in quotes. i.e: if i'll supply my program with the following arguments: -winpath "c:\\temp\\" tcsh will...
25
by: dennijr | last post by:
ok, shell always used to be easy for me, now its starting to get annoying cause i dont know wats wrong heres the simplist code possible: Private Sub IExplorer_Click() a = Shell("C:\Program...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.