473,769 Members | 7,650 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Python Subprocess module

hey folks,

A simple question hopefully. despite all my searching I have not found a
satisfactory response.

The goal. Interact with a command line program. Simple enough, but the
key is INTERACT.

I tried the shell and comand approaches but that initiates, it does not
allow interaction with the programs.

So then I went with Popen and such... which then led to the subprocess
module.

I can create the object and read a few lines of output. but if I go too
far then the program hangs. the number of lines will differ depandening
on many function including the format of an input file so I can;t
hardcode how many lines to read.

I want to read all of STDOUT without failing because I went out of range.

next I want to read the final line of the output because it tells me
what is required for the next line of input.

I am supposed to be able to entre 'y' and return for the program to
continue if I agree with what I see in the stdout.

A problem exists though that I have tried
'y'
'y\n'
'y\r'

and nothing seems to get the program going again for I still cant; read
past the same point in the standard output. then I have to kill and
start over.

So the next approach included looking at Pexpect, which got realy
confusing realy fast and despite running fedora core and python 2.4.4 I
would like my application to be cross platform and there is no Pexpect
for Windows That I can see.

I have checked out many mailing lists and posts and tutorials but they
all admit to not be samples of complex interactivity. I guess I am
having that special case.

ASPN Python cookbok provided some code for a new Popen Class that allows
for interaction but I don't think I need to go that root....

I am essentialy trying to build and Python wrapper for a coomand line
program that i want to build a GUI around.

Any links or suggestions would be great. Unfortunaetly I don't
understand that abstract concepts in the manual reference. I am fresh in
the python world and find examples better than technical abstracts.

Cheers
Jul 13 '07 #1
7 3047

You could start up a pty and do your own Pexpect-like thing, using
read/readline/write.

However, this may not be portable to windows. Maybe if you get the python
that comes with cygwin, or build your own python under the cygwin
environment - that might be more likely to get you pty's under windows.

On Fri, 13 Jul 2007 17:15:49 -0400, Dave Sampson wrote:
hey folks,

A simple question hopefully. despite all my searching I have not found a
satisfactory response.

The goal. Interact with a command line program. Simple enough, but the
key is INTERACT.

I tried the shell and comand approaches but that initiates, it does not
allow interaction with the programs.

So then I went with Popen and such... which then led to the subprocess
module.

I can create the object and read a few lines of output. but if I go too
far then the program hangs. the number of lines will differ depandening
on many function including the format of an input file so I can;t
hardcode how many lines to read.

I want to read all of STDOUT without failing because I went out of range.

next I want to read the final line of the output because it tells me
what is required for the next line of input.

I am supposed to be able to entre 'y' and return for the program to
continue if I agree with what I see in the stdout.

A problem exists though that I have tried
'y'
'y\n'
'y\r'

and nothing seems to get the program going again for I still cant; read
past the same point in the standard output. then I have to kill and
start over.

So the next approach included looking at Pexpect, which got realy
confusing realy fast and despite running fedora core and python 2.4.4 I
would like my application to be cross platform and there is no Pexpect
for Windows That I can see.

I have checked out many mailing lists and posts and tutorials but they
all admit to not be samples of complex interactivity. I guess I am
having that special case.

ASPN Python cookbok provided some code for a new Popen Class that allows
for interaction but I don't think I need to go that root....

I am essentialy trying to build and Python wrapper for a coomand line
program that i want to build a GUI around.

Any links or suggestions would be great. Unfortunaetly I don't
understand that abstract concepts in the manual reference. I am fresh in
the python world and find examples better than technical abstracts.

Cheers
Jul 13 '07 #2
On Jul 13, 4:15 pm, Dave Sampson <sampe...@gmail .comwrote:
hey folks,

A simple question hopefully. despite all my searching I have not found a
satisfactory response.

The goal. Interact with a command line program. Simple enough, but the
key is INTERACT.

I tried the shell and comand approaches but that initiates, it does not
allow interaction with the programs.

So then I went with Popen and such... which then led to the subprocess
module.

I can create the object and read a few lines of output. but if I go too
far then the program hangs. the number of lines will differ depandening
on many function including the format of an input file so I can;t
hardcode how many lines to read.

I want to read all of STDOUT without failing because I went out of range.

next I want to read the final line of the output because it tells me
what is required for the next line of input.

I am supposed to be able to entre 'y' and return for the program to
continue if I agree with what I see in the stdout.

A problem exists though that I have tried
'y'
'y\n'
'y\r'

and nothing seems to get the program going again for I still cant; read
past the same point in the standard output. then I have to kill and
start over.

So the next approach included looking at Pexpect, which got realy
confusing realy fast and despite running fedora core and python 2.4.4 I
would like my application to be cross platform and there is no Pexpect
for Windows That I can see.

I have checked out many mailing lists and posts and tutorials but they
all admit to not be samples of complex interactivity. I guess I am
having that special case.

ASPN Python cookbok provided some code for a new Popen Class that allows
for interaction but I don't think I need to go that root....

I am essentialy trying to build and Python wrapper for a coomand line
program that i want to build a GUI around.

Any links or suggestions would be great. Unfortunaetly I don't
understand that abstract concepts in the manual reference. I am fresh in
the python world and find examples better than technical abstracts.

Cheers
There is something called http://www.rutherfurd.net/python/sendkeys/
send keys for the p.c. you can use a try: except: concept and on the
windows platform there is also a way to use autoit in python using
pywin. At one time (2001) there was a module that allowed you to use
gvim but it has dissapeared unless it is listed somewhere else (gvim
doesn't seem to work with the sendkey method and it is a shame it has
alot of cool text functions including command line support)

http://sourceforge.net/projects/dex-tracker/

Jul 14 '07 #3
On Jul 13, 2:15 pm, Dave Sampson <sampe...@gmail .comwrote:
hey folks,

A simple question hopefully. despite all my searching I have not found a
satisfactory response.

The goal. Interact with a command line program. Simple enough, but the
key is INTERACT.
You need Pexpect.
So then I went with Popen and such... which then led to the subprocess
module. I can create the object and read a few lines of output. but if I go too
far then the program hangs. the number of lines will differ depandening
on many function including the format of an input file so I can;t
hardcode how many lines to read.
There is nothing you can do about this when using a pipe.
This is because stdio will change the type of buffering when sending
stdout and stderr to a pipe. From the client side there is no way you
can change the buffer mode (you CAN change the mode, but it only
changes
it on YOUR side of the pipe -- not your child application's side).
and nothing seems to get the program going again for I still cant; read
past the same point in the standard output. then I have to kill and
start over.
Your child application is waiting for you, but you can't see what it
wrote
becuase it has not filled or flushed the output buffer. When a program
write to stdout and it is connected to a TTY it will automatically
flush
the output buffer when it writes a newline (or, rather, the clib will
do this).
But when writing to a pipe it will automatically use block buffering
and
will only flush the buffer when it is full.

It's not very complicated, but it can be very confusing at first.
It is also surprising that this behavior cannot be changed by the
parent application.
So the next approach included looking atPexpect, which got realy
confusing realy fast and despite running fedora core and python 2.4.4 I
would like my application to be cross platform and there is noPexpect
for Windows That I can see.
You are going to need something like Pexpect that uses pseudo-ttys
(pty).
Pexpect works under Cygwin on Windows, but not under the native
Windows Python.

Email me if you have questions about Pexpect and I'll try to help you.

Yours,
Noah

Jul 15 '07 #4
Dave Sampson wrote:
I am supposed to be able to entre 'y' and return for the program to
continue if I agree with what I see in the stdout.

A problem exists though that I have tried
'y'
'y\n'
'y\r'
Depending on the platform, you may need to send '\r\n'.

ASPN Python cookbok provided some code for a new Popen Class that allows
for interaction but I don't think I need to go that root....
You probably mean:
http://aspn.activestate.com/ASPN/Coo.../Recipe/440554

Why is that not sufficient? It will allow interaction with a called
subprocess, in a cross-platform manner, etc.
- Josiah
Jul 16 '07 #5
On Jul 13, 7:50 pm, Dan Stromberg - Datallegro
<dstromb...@dat allegro.comwrot e:
You could start up a pty and do your own Pexpect-like thing, using
read/readline/write.

However, this may not be portable to windows. Maybe if you get the python
that comes with cygwin, or build your own python under the cygwin

thanks for the suggestion. I will follow up on Noah's pexpect
sugestion bellow.

thanks for the input, cross platform is key with low overhead.

Jul 21 '07 #6
send keys for the p.c. you can use a try: except: concept and on the
windows platform there is also a way to use autoit in python using
pywin.
thanks for the uggestion about sendkeys. I might be able to build that
into the wrapper.

So you're sugesting:

NIX = Pexpect

WINDOZE = sendkeys

Correct?
Jul 21 '07 #7
Thanks for your response. It;s good to see you're promtoing Pexpect. I
was actualy wokring on a project at work that could have used Pexpect
to automate the creation of maps by tieing togethr a bunch of
processes through SSH and Pexpect looked like the money shot.

however it was enough of a battle to bring Python into the game since
it is government that bringing CYGWIN was a battle not worth
fighting. Which is unfortunate because I think Pexpect was exactly
what we needed in that case
>
You need Pexpect.
There is nothing you can do about this when using a pipe.
This is because stdio will change the type of buffering when sending
stdout and stderr to a pipe. From the client side there is no way you
can change the buffer mode (you CAN change the mode, but it only
changes
it on YOUR side of the pipe -- not your child application's side).

I obviously lack skills and understanding in many aspects of
programming. After all I'm a geographer / recreologist not a comp-sci
professional.

I understand the CONCEPT of pipes but don't know how to use or
implement.. the bigest things I ever do is something like

dmesg | grep wlan0

Where | is my pipe... but that is the extent of my knowledge and
skill.

Pexpect may very well be the option I need, and having a project that
supports the module is great. I am curous what is stopping Pexpect
from becoming cross platform.

Above, a user suggested implementing send keys in windows. Why not
include that as an try/except in Pexpect instead of my wrapper? That
way it can benifit many more people?

Cheers
Jul 21 '07 #8

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

Similar topics

2
4545
by: Xah Lee | last post by:
Python Doc Problem Example: os.system Xah Lee, 2005-09 today i'm trying to use Python to call shell commands. e.g. in Perl something like output=qx(ls) in Python i quickly located the the function due to its
0
361
by: Kurt B. Kaiser | last post by:
Patch / Bug Summary ___________________ Patches : 378 open ( +3) / 3298 closed (+34) / 3676 total (+37) Bugs : 886 open (-24) / 5926 closed (+75) / 6812 total (+51) RFE : 224 open ( +7) / 227 closed ( +7) / 451 total (+14) New / Reopened Patches ______________________
7
1859
by: Jonathan Fine | last post by:
Hello My problem is that I want a Python 2.4 module on a server that is running Python 2.3. I definitely want to use the 2.4 module, and I don't want to require the server to move to Python 2.4. More exactly, I am using subprocess, which is new in Python 2.4. What I am writing is something like
10
1507
by: bruce | last post by:
hi... can someone point me to where/how i would go about calling a ruby app from a python app, and having the python app being able to get a returned value from the ruby script. something like test.py a = os.exec(testruby.rb)
0
196
by: Michel Lespinasse | last post by:
On Thu, Aug 28, 2008 at 10:37:48AM +0100, Tim Golden wrote: The root cause is different (subprocess has separate implementations for windows and posix), but the issues are similar. In the posix case, I only observed the issue when you using subprocess.PIPE The attached patch against subprocess.py (as shipped on debian etch, based on python version 2.4.4) seems to work for me: --- /usr/lib/python2.4/subprocess.py 2008-04-16...
0
9423
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10214
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9996
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8872
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7410
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5304
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3963
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3563
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2815
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.