473,788 Members | 2,713 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

popen2

Guy
Hi

I was given an exstremly useful answer to a problem that I had in
windows. I'm relativly new with python, but find it exstremly useful.
I'm creating a script to run test files and record output, the script
already works on win32 and I've made a telnet procedure which works
quite well when you want to do the testing on one of the many unix
boxes (but its very slow). What I want to do now is run the script on
a unix system, and later on, create a rsh function to run the tests on
different unix boxes which have not got python on them.

The following snippet of code is what I use on win32. just example
sets some env vars and executes an exe, collects and processes the
output. :

output, input = popen2("cmd.exe ")
# Sets the enviroment.
input.write("se t LI_DIR=value\n" )
input.write("se t LI_DIR_ALT=valu e\n")
input.write("se t LI_ARCH=value\n ")
input.write("se t LI_PRECISION=va lue\n")
input.write("se t LI_PERFORMANCE= value\n")
input.write("se t LI_LINKING=\n")
input.write("se t LI_ARCHFLAG=\n" )
input.write("se t LI\n")

# Writes commands to it.
input.write(f_m wtest_exe_str + " " + f_mwtest_args + " " +
os.path.dirname (f_testlwc_str) + " " + f_testlwc_str + "\n")

input.write("ex it\n")

while 1:
text = output.readline ()
if text:
process text line
else:
break:

The unix equivalant is something like this :

from popen2 import popen2
output, input = popen2("csh")
# Sets the enviroment.
input.write("se tenv DISPLAY doors:0.0\n")

input.writeinpu t.write(f_mwtes t_exe_str + " " + f_mwtest_args + " " +
os.path.dirname (f_testlwc_str) + " " + f_testlwc_str + "\n")

print "finished"
process the output from the test file.
The problem is at the moment I can't get the process to stop and wait
until the exe has finished executing, the python script executes
everything and then the test file is executed afterwards. I'm thinking
that there must be a switch on the csh or sh command to stop this
happening but I don't know what it is. The unix machines that I'm
working on are all different plat types, so the platform type proberly
won't help you.

NOTE the commands above will be a lot more varied, and are not just
used to set the enviroment up.

Any help would be of great value to me
TIA

Guy
Jul 18 '05 #1
1 7140
In article <f3************ *************@p osting.google.c om>,
gu*********@Mac hineworks.com (Guy) wrote:
The unix equivalant is something like this :

from popen2 import popen2
output, input = popen2("csh")
# Sets the enviroment.
input.write("se tenv DISPLAY doors:0.0\n")

input.writeinpu t.write(f_mwtes t_exe_str + " " + f_mwtest_args + " " +
os.path.dirname (f_testlwc_str) + " " + f_testlwc_str + "\n")

print "finished"
process the output from the test file.
The problem is at the moment I can't get the process to stop and wait
until the exe has finished executing, the python script executes
everything and then the test file is executed afterwards. I'm thinking
that there must be a switch on the csh or sh command to stop this
happening but I don't know what it is. The unix machines that I'm
working on are all different plat types, so the platform type proberly
won't help you.


Try something like
status = input.close()
result = output.read()
if status is None:
print 'success:', repr(result)
else:
print 'error exit', status, repr(result)

The close() is the important part:

1. flush your buffer so data actually goes to the shell
2. closes the shell's input so it doesn't stay around
waiting for more.
3. returns exit status for the shell process, or None
if the exit status is 0. (This won't work so well
for rsh.)

While I'm at it, I would recommend that you use "sh" instead
of "csh", because in the end it will give you less grief with
bugs and misfeatures. "setenv x v" -> "x=v export x" if you
do this. I would also recommend popen2("sh 2>&1") so that
the piped output includes error/diagnostic output.

Donn Cave, do**@u.washingt on.edu
Jul 18 '05 #2

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

Similar topics

1
1712
by: A. Lloyd Flanagan | last post by:
OK, I've got a weird one I haven't been able to figure out yet. Admittedly I haven't had time to dig into the library source, but this behavior certainly doesn't seem right. Here's a test case: """Test program to demonstrate problem with popen2 module.""" import popen2 def main (argv): mycmd = 'python2.3 -c "for i in range(100000):\n print i"' p = popen2.Popen3(mycmd)
1
3111
by: | last post by:
This could possibly be a bug, but I don't understand it fully so I'm posting here first. Searching the list told me other people are having this problem too. I have created a class which inherits popen2.Popen3. The problem is that popen2.Popen3 does not define a __del__ method to handle proper closing of the pipes or destruction of the process. So I created one in my class:
4
1666
by: P | last post by:
I've written a couple of apps that required running a command and grabbing the output, and I've found the existing interfaces problematic for this. I think the proliferation of functions and classes in the popen2 module illustrates the problem (popen2.{popen2,popen3,popen4,Popen3,Popen4}) Now if I want to read both stdout and stderr seperately then it's awkward to say the least to implement that without deadlocking using
1
2662
by: Vivien Mallet | last post by:
Hello, I use popen2.Popen4 and I experienced problems with it. Let me show you with an example. The following script is called "lines" (it prints lines of 'X'): --------------------- #!/usr/bin/env python import sys
1
2582
by: Magnus Lycka | last post by:
I'm trying to read standard out in a process started with popen2 in a non-blocking way. (Other good ways of doing this than the one I tried are appreciated.) I've tried to dumb down my code to see what happens, and socket.poll seems to behave very strangely. I've tried to use the .poll method for the poll object with and without a timeout, but in either case, the output randomly switches between on of the versions below. It runs fast,...
9
1838
by: Martin P. Hellwig | last post by:
Hi all, I was doing some popen2 tests so that I'm more comfortable using it. I wrote a little python script to help me test that (testia.py): --------------------------------- someline = raw_input("something:") if someline == 'test': print("yup")
3
3104
by: mikem76 | last post by:
How do I automatically redirect stdout and stderr when using os.popen2 to start a long running process. If the process prints a lot of stuff to stdout it will eventually stop because it runs out of buffer space. Once I start reading the stdout file returned by os.popen2 then the process resumes. I know that I could just specify > /dev/null when starting the process but I'd like to know if there is a way to start a process using os.popen2...
1
1597
by: mikem76 | last post by:
Is there a way to get the process id when starting a process using os.popen2 or os.popen3 on linux? Mike
1
1802
by: diego | last post by:
I'm trying to understand how popen2 works. Found in this group, that popen2.popen2 can cause trouble so i chose win32pipe.popen2. have a look a the listing of 2 files: ekmain.py: ************** import win32pipe (stdin1, stdout1) = win32pipe.popen2("test1.py") stdin1.write("1\n")
0
9656
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9498
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
10366
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...
0
10175
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10112
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,...
1
7518
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
6750
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5536
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2894
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.