popen2 [英] popen2

查看:99
本文介绍了popen2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我得到了一个非常有用的答案来解决我在

windows中遇到的问题。我是python的新手,但发现它非常有用。

我正在创建一个运行测试文件和记录输出的脚本,脚本

已经有效了在win32上,当你想在许多unix

盒子中进行测试时,我做了一个telnet程序,这个程序非常好用(但很慢) )。我现在要做的是在

a unix系统上运行脚本,然后创建一个rsh函数来运行测试

不同的unix盒子他们使用python。


以下代码片段是我在win32上使用的代码片段。只是示例

设置一些env变量并执行exe,收集并处理

输出。 :


输出,输入= popen2(" cmd.exe")

#设置环境。

input.write (set LI_DIR = value \ n)

input.write(" set LI_DIR_ALT = value \ n")

input.write(" set LI_ARCH = value \ n")

input.write(" set LI_PRECISION = value \ n")

input.write(" set LI_PERFORMANCE = value\\ \\ n")

input.write(" set LI_LINKING = \ n")

input.write(" set LI_ARCHFLAG = \ n")

input.write(" set LI\\\
")

#将命令写入其中。

input.write(f_mwtest_exe_str +"" + f_mwtest_args +"" +

os.path.dirname(f_testlwc_str)+"" + f_testlwc_str +" \\\
")


input.write(" exit \ n")

而1:

text = output.readline()

如果是文本:

处理文本行

else:

休息:


unix等价物是这样的:


来自popen2 import popen2

输出,输入= popen2(" csh")

#设置环境。

input.write(" setenv DISPLAY doors:0.0\ n" )


input.writeinput.write(f_mwtest_exe_str +" " + f_mwtest_args +" " +

os.path.dirname(f_testlwc_str)+" " + f_testlwc_str +" \ n")


print" finished

处理测试文件的输出。

问题是目前我无法停止等待

直到exe执行完毕,python脚本执行

所有内容然后之后执行测试文件。我在想

必须在csh或sh命令上切换才能停止这个

发生但我不知道它是什么。我工作的unix机器都是不同的平台类型,因此平台类型为pro b / b
不能帮助你。


注意上面的命令会更加多样化,并且不仅仅是用于设置环境的



任何帮助对我来说很有价值

TIA


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("set LI_DIR=value\n")
input.write("set LI_DIR_ALT=value\n")
input.write("set LI_ARCH=value\n")
input.write("set LI_PRECISION=value\n")
input.write("set LI_PERFORMANCE=value\n")
input.write("set LI_LINKING=\n")
input.write("set LI_ARCHFLAG=\n")
input.write("set LI\n")

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

input.write("exit\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("setenv DISPLAY doors:0.0\n")

input.writeinput.write(f_mwtest_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

推荐答案

在文章< f3 ************************* @ posting.google.com> ,
gu*********@Machineworks.com (盖伊)写道:
In article <f3*************************@posting.google.com> ,
gu*********@Machineworks.com (Guy) wrote:
unix等效项是这样的:

来自popen2 import popen2
输出,input = popen2(" csh")
#设置环境。
input.write(" setenv DISPLAY doors:0.0\ n)

input.writeinput.write(f_mwtest_exe_str +"" + f_mwtest_args + "" +
os.path.dirname(f_testlwc_str)+"" + f_testlwc_str +" \ n")

打印完成
进程测试文件的输出。

问题是目前我无法停止等待
直到exe执行完毕,python脚本执行
一切然后测试文件随后执行。我在想,必须切换csh或sh命令来阻止这种情况发生,但我不知道它是什么。我正在工作的unix机器都是不同的平台类型,因此平台类型无法帮助你。
The unix equivalant is something like this :

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

input.writeinput.write(f_mwtest_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.




尝试类似

status = input.close()

result = output.read()

如果状态为无:

打印''成功:'',repr(结果)

else:

打印''错误退出'',状态,repr(结果)


close()是重要部分:


1.刷新缓冲区,使数据真正进入shell

2.关闭shell的输入,因此它不会停留在

等待更多。

3.返回shell进程的退出状态,或者没有

如果退出状态为0.(这不会很好用

for rsh。)


虽然我在,但我建议你使用sh。相反

的csh,因为最终它会给你带来更少的悲伤与

错误和错误。 setenv x v - > x = v export x如果你

这样做。我还推荐使用popen2(sh 2>& 1),以便管道输出包含错误/诊断输出




Donn Cave, do**@u.washington.edu


这篇关于popen2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆