从/向进程读/写 [英] Read/Write from/to a process

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

问题描述



我想开始一个新的过程,并且能够从/向它读取/写入
它。我尝试过这样的事情......


导入子流程为sp

p = sp.Popen(" cmd.exe",stdout = sp.PIPE)

p.stdin.write(" hostname \ n")


然而,它似乎无法奏效。我认为cmd.exe正在捕捉它。


我也试过

f = open(" out.txt"," w")

sys.stdout = f

os.system(" cmd.exe")


...但是out.txt没有''不包含cmd.exe的任何输出


那么,如何在Windows上创建进程(在本例中为cmd.exe)和

能够读/写它吗?


谢谢

Hi,
I would like to start a new process and be able to read/write from/to
it. I have tried things like...

import subprocess as sp
p = sp.Popen("cmd.exe", stdout=sp.PIPE)
p.stdin.write("hostname\n")

however, it doesn''t seem to work. I think the cmd.exe is catching it.

I also tried
f = open("out.txt", "w")
sys.stdout = f
os.system("cmd.exe")

...but out.txt didn''t contain any output from cmd.exe

So, how can I create a process (in this case, cmd.exe) on Windows and
be able to read/write from/to it?

Thanks

推荐答案

On 2005年10月24日07:20:42 -0700,jas <共******* @ gmail.com>在comp.lang.python中声明

跟随:
On 24 Oct 2005 07:20:42 -0700, "jas" <co*******@gmail.com> declaimed the
following in comp.lang.python:

我想开始一个新的过程并且能够阅读/从/写到
它。我尝试了类似的东西......

导入子进程为sp
p = sp.Popen(" cmd.exe",stdout = sp.PIPE)
p.stdin .write(" hostname \ n")
然而,它似乎无法奏效。我认为cmd.exe正在捕捉它。
Hi,
I would like to start a new process and be able to read/write from/to
it. I have tried things like...

import subprocess as sp
p = sp.Popen("cmd.exe", stdout=sp.PIPE)
p.stdin.write("hostname\n")

however, it doesn''t seem to work. I think the cmd.exe is catching it.




一:你没有读过任何返回的信息。输出......


二:输出似乎只在EOF上可用,这意味着

衍生命令处理器必须首先退出...虽然你可以一次读一个

字符,然后必须建立行和预期的提示

字符串...

这似乎工作:

- = - = - = - = - = - = - = - = -

导入子流程

导入os


PROMPT = os.getcwd()+">"


def getLine(proc):

ld = []

而True:

c = proc.stdout.read(1)

如果c ==" \r":继续#skip Windows< cr>

if c!=" \ n":ld.append(c)#save all but< lf>

if c是无或c ==" \ n"或者c ==">":break

ln ="" .join(ld)

return ln


p = subprocess.Popen(" cmd.exe",stdout = subprocess.PIPE,stdin =

subprocess.PIPE)


print" ****开始捕获的输出

而True:

ln = getLine(p)

print ln

if ln == PROMPT:break

print" **** END of captured output"


p.stdin.write(" ipconfig \ n")

打印****开始捕获的输出

而True:

ln = getLine(p)

打印ln

如果ln == PROMPT:break

print" ****捕获输出的结尾"


p.stdin.write(" dir \ n")

print" ****开始捕获的输出"

while是的:

ln = getLine(p)

打印ln

如果ln == PROMPT:break

打印****捕获输出结束


p.stdin.write(" exit \ n")

- = - = - = - = - = - = - = - = - = -

E:\ UserData \ Denen Lee Bieber \ My Documents> python script1.py

****捕获输出的开始

Microsoft Windows XP [版本5.1.2600]

(C)版权所有1985-2001 Microsoft Corp.


E:\ UserData \ Dennis Lee Bieber \\我的文件>

****捕获输出结束

****捕获输出的开始

ipconfig


Windows IP配置

以太网适配器本地连接:


特定于连接的DNS后缀。 :

IP地址。 。 。 。 。 。 。 。 。 。 。 。 :192.168.1.100

子网掩码。 。 。 。 。 。 。 。 。 。 。 :255.255.255.0

IP地址。 。 。 。 。 。 。 。 。 。 。 。 :fe80 :: 211:11ff:fee1:f303%4

默认网关。 。 。 。 。 。 。 。 。 :192.168.1.1


隧道适配器Teredo隧道伪接口:


特定于连接的DNS后缀。 :

IP地址。 。 。 。 。 。 。 。 。 。 。 。 :

3ffe:831f:4004:1956:0:fbde:bd0a:e50b

IP地址。 。 。 。 。 。 。 。 。 。 。 。 :fe80 :: 5445:5245:444f%5

默认网关。 。 。 。 。 。 。 。 。 :::


隧道适配器自动隧道伪接口:


特定于连接的DNS后缀。 :

IP地址。 。 。 。 。 。 。 。 。 。 。 。 :fe80 :: 5efe:192.168.1.100%2

默认网关。 。 。 。 。 。 。 。 。 :


E:\ UserData \ Denen Lee Bieber \我的文件>

****捕获输出结束

****捕获输出的开始

dir

驱动器E中的数量是数据

卷序列号是2626-D991


目录E:\ UserData \ Denenie Lee Bieber \我的文件


10/24/2005 09:23 AM< DIR> ;



One: you didn''t read any of the "returned" output...

Two: the output only seems to be available upon EOF, which means the
spawned command processor has to exit first... Though you CAN read one
character at a time, and then have to build lines and expected prompt
strings...
This seems to work:
-=-=-=-=-=-=-=-=-
import subprocess
import os

PROMPT = os.getcwd() + ">"

def getLine(proc):
ld = []
while True:
c = proc.stdout.read(1)
if c == "\r": continue #skip Windows <cr>
if c != "\n": ld.append(c) #save all but <lf>
if c is None or c == "\n" or c == ">": break
ln = "".join(ld)
return ln

p = subprocess.Popen("cmd.exe", stdout=subprocess.PIPE, stdin =
subprocess.PIPE)

print "**** START of captured output"
while True:
ln = getLine(p)
print ln
if ln == PROMPT: break
print "**** END of captured output"

p.stdin.write("ipconfig\n")
print "**** START of captured output"
while True:
ln = getLine(p)
print ln
if ln == PROMPT: break
print "**** END of captured output"

p.stdin.write("dir\n")
print "**** START of captured output"
while True:
ln = getLine(p)
print ln
if ln == PROMPT: break
print "**** END of captured output"

p.stdin.write("exit\n")
-=-=-=-=-=-=-=-=-=-
E:\UserData\Dennis Lee Bieber\My Documents>python script1.py
**** START of captured output
Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.

E:\UserData\Dennis Lee Bieber\My Documents>
**** END of captured output
**** START of captured output
ipconfig

Windows IP Configuration
Ethernet adapter Local Area Connection:

Connection-specific DNS Suffix . :
IP Address. . . . . . . . . . . . : 192.168.1.100
Subnet Mask . . . . . . . . . . . : 255.255.255.0
IP Address. . . . . . . . . . . . : fe80::211:11ff:fee1:f303%4
Default Gateway . . . . . . . . . : 192.168.1.1

Tunnel adapter Teredo Tunneling Pseudo-Interface:

Connection-specific DNS Suffix . :
IP Address. . . . . . . . . . . . :
3ffe:831f:4004:1956:0:fbde:bd0a:e50b
IP Address. . . . . . . . . . . . : fe80::5445:5245:444f%5
Default Gateway . . . . . . . . . : ::

Tunnel adapter Automatic Tunneling Pseudo-Interface:

Connection-specific DNS Suffix . :
IP Address. . . . . . . . . . . . : fe80::5efe:192.168.1.100%2
Default Gateway . . . . . . . . . :

E:\UserData\Dennis Lee Bieber\My Documents>
**** END of captured output
**** START of captured output
dir
Volume in drive E is Data
Volume Serial Number is 2626-D991

Directory of E:\UserData\Dennis Lee Bieber\My Documents

10/24/2005 09:23 AM <DIR>


谢谢,这肯定是一个开始。正如你所提到的,cd是指cd。可能是

一个问题。


也许检查线是否以>结尾是不够的?


Dennis Lee Bieber写道:
Thanks, that is certainly a start. As you mentioned, the "cd" could is
an issue.

Perhaps checking to see if the line ends with ">" is sufficient?

Dennis Lee Bieber wrote:
2005年10月24日07:20:42 -0700,jas <共******* @ gmail.com>在comp.lang.python中声明了以下内容:
On 24 Oct 2005 07:20:42 -0700, "jas" <co*******@gmail.com> declaimed the
following in comp.lang.python:

我想开始一个新进程并能够从/向读/写
它。我尝试了类似的东西......

导入子进程为sp
p = sp.Popen(" cmd.exe",stdout = sp.PIPE)
p.stdin .write(" hostname \ n")
然而,它似乎无法奏效。我认为cmd.exe正在捕捉它。
Hi,
I would like to start a new process and be able to read/write from/to
it. I have tried things like...

import subprocess as sp
p = sp.Popen("cmd.exe", stdout=sp.PIPE)
p.stdin.write("hostname\n")

however, it doesn''t seem to work. I think the cmd.exe is catching it.



一:你没有读过任何返回的内容。输出......

二:输出似乎只在EOF上可用,这意味着
衍生的命令处理器必须先退出...虽然你可以读一个
角色一次,然后必须建立线条和预期的提示
字符串...

这似乎工作:
- = - = - = - = - = - = - = - = -
导入子流程
导入os

PROMPT = os.getcwd()+">"

def getLine (proc):
ld = []
而True:
c = proc.stdout.read(1)
如果c ==" \r":continue#如果c为无或c == \
"或者c ==">":break
ln ="" .join(ld)
返回ln

p = subprocess.Popen(" cmd .exe",stdout = subprocess.PIPE,stdin =
subprocess.PIPE)

打印****开始捕获的输出
而True:
ln = getLine(p)
打印ln
如果ln == PROMPT:break
print" **** END of capture output"

p。 stdin.write(" ipconfig \ n")
打印" ****捕获输出的开始时间"
而True:
ln = getLine(p)
打印ln
如果ln == PROMPT:break
print" **** END of capture output"

p.stdin.write(" dir \ n")
打印****开始捕获输出
而True:
ln = getLine(p)
打印ln
如果ln == PROMPT:break
打印****捕获输出结束

p.stdin.write(" exit \ n")
- = - = - = - = E =:= - = - = - = - = -
E: \ userData \ Denen Lee Bieber \ My Documents> python script1.py
****捕获输出的开始
Microsoft Windows XP [版本5.1.2600]
(C)版权所有1985-2001微软公司

E:\ UserData \ Denen Lee Bieber \我的文件>
****结束捕获的输出
**** START捕获的输出
ipconfig

Windows IP配置

以太网适配器本地连接:

特定于连接的DNS后缀。 :
IP地址。 。 。 。 。 。 。 。 。 。 。 。 :192.168.1.100
子网掩码。 。 。 。 。 。 。 。 。 。 。 :255.255.255.0
IP地址。 。 。 。 。 。 。 。 。 。 。 。 :fe80 :: 211:11ff:fee1:f303%4
默认网关。 。 。 。 。 。 。 。 。 :192.168.1.1

隧道适配器Teredo隧道伪接口:

特定于连接的DNS后缀。 :
IP地址。 。 。 。 。 。 。 。 。 。 。 。 :
3ffe:831f:4004:1956:0:fbde:bd0a:e50b
IP地址。 。 。 。 。 。 。 。 。 。 。 。 :fe80 :: 5445:5245:444f%5
默认网关。 。 。 。 。 。 。 。 。 :::

隧道适配器自动隧道伪接口:

特定于连接的DNS后缀。 :
IP地址。 。 。 。 。 。 。 。 。 。 。 。 :fe80 :: 5efe:192.168.1.100%2
默认网关。 。 。 。 。 。 。 。 。 :

E:\ UserData \ Denen Lee Bieber \我的文件>
****捕获输出的结束
****捕获输出的开始
dir
驱动器E中的卷是数据
卷序列号是2626-D991

目录E:\ UserData \ Denen Lee Bieber \ My Documents

10/24/2005 09:23 AM< DIR>

2005年10月24日上午09:23< DIR>
.. 07/25/2005 10:39 PM< DIR>
.metadata
10/06/2005 09:54 AM< DIR>
Ada Progs
08/13 / 2005 02:01 PM< DIR>
代理商数据
2005年10月21日上午09:29 421,820 apress_offer.pdf
07/03/2005 11:36 AM 132 cp.py
07/17/2005 12:25 PM< DIR>
Cyber​​link
07/06/2005 09:32 AM 102,400 db1.mdb
07/26/2005 12: 20 AM 26,614 eclipse_code.xml
10/24/2005 01:08 AM< DIR>
Eudora
06/24/2005 08:50 PM 667 fake_oosums.ads
06/24/2005 08:50 PM 695 fake_oosums.ali
09/06/2005 09:01 PM< DIR>
Genealogy
07/13/2005 10:56 PM< DIR>
HomeSite
05/08/2005 01:05 PM< DIR>
投资
2005/10/21 10:04< DIR>
Java Progs
08/04/2005 10:13 PM 162 main.py
10/11/2005 10:43 PM< DIR>
我的下载
05/01/2005 10:31 AM< DIR>
我的电子书
04/22/2005 12:09 AM < DIR>
我的音乐
07/10/2005 11:43 AM< DIR>
我的照片
06/29/2005 11:55 PM< DIR>
我的PSP档案
05/23/2005 09:30 AM< DIR>
我的视频
05/01/2005 12:49 PM< DIR>
Office文件
06/27/2005 03:19 PM 7,961,778
org.e clipse.jdt.doc.user.I20050627-1435.pdf
06/27/2005 03:19 PM 6,791,109
org.eclipse.platform.doc.user.I20050627-1435.pdf
10/11/2005 10:52 PM 56 oth_tsr_rm_750.ram
07/20/2005 09:32 AM 108,457 parkerred15yc.jpg
09/03/2005 10:36 PM< DIR>
Python Progs
2005年10月20日下午10:38< DIR>
Quicken
07/10/2005 12:09 PM 3,356,248 results.xml
06/11 / 2005年12:03 PM 935 Scout_Ship Database.lnk
2005年3月3日下午12:38< DIR>
Scout_Ship我的文件
2005年10月24日上午9:23 971 Script1。 2005年5月25日下午12:40 1,107 Script1_old.py
08/28/2005 11:47 AM< DIR>
SimpleMu日志
06/24/2005 08:56 PM 1,201 student_pack.ads
06/24/2005 08:49 PM 1,144 student_pack.ads.0
06/24/2005 08:56 PM 1,342 student_pack.ali
08 / 02/2 005 11:39 PM 4,096 t.DOC
06/20/2005 10:11 AM 104 t.rx
08/05/2005 08:41 PM 66,452 Untitled-1.tif
08 / 05/2005 08:41 PM< DIR>
VCheck
10/03/2005 02:58 AM< DIR>
Visual Studio
2005/10/03 02 :51 AM< DIR>
Visual Studio 2005
21个文件18,847,490字节
25 Dir(s)267,162,845,184字节免费

E:\ UserData \ Denen Lee Bieber \我的文件>
****收到的输出结束

E:\ UserData \ Denen Lee Bieber \我的文件>

- = - = - = - = - = - = - = - = -

请注意我的getLine()必须返回真正的EOL,或者在命令提示符的末尾(stuff>)返回。我还必须在开始时初始化
提示符。如果有人发出了cd,命令到子进程,提示将全部不按顺序,代码将挂起。

你还会注意到提示/结束/捕获序列,以及
下一个命令是/ next / capture序列的开始。

-



One: you didn''t read any of the "returned" output...

Two: the output only seems to be available upon EOF, which means the
spawned command processor has to exit first... Though you CAN read one
character at a time, and then have to build lines and expected prompt
strings...
This seems to work:
-=-=-=-=-=-=-=-=-
import subprocess
import os

PROMPT = os.getcwd() + ">"

def getLine(proc):
ld = []
while True:
c = proc.stdout.read(1)
if c == "\r": continue #skip Windows <cr>
if c != "\n": ld.append(c) #save all but <lf>
if c is None or c == "\n" or c == ">": break
ln = "".join(ld)
return ln

p = subprocess.Popen("cmd.exe", stdout=subprocess.PIPE, stdin =
subprocess.PIPE)

print "**** START of captured output"
while True:
ln = getLine(p)
print ln
if ln == PROMPT: break
print "**** END of captured output"

p.stdin.write("ipconfig\n")
print "**** START of captured output"
while True:
ln = getLine(p)
print ln
if ln == PROMPT: break
print "**** END of captured output"

p.stdin.write("dir\n")
print "**** START of captured output"
while True:
ln = getLine(p)
print ln
if ln == PROMPT: break
print "**** END of captured output"

p.stdin.write("exit\n")
-=-=-=-=-=-=-=-=-=-
E:\UserData\Dennis Lee Bieber\My Documents>python script1.py
**** START of captured output
Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.

E:\UserData\Dennis Lee Bieber\My Documents>
**** END of captured output
**** START of captured output
ipconfig

Windows IP Configuration
Ethernet adapter Local Area Connection:

Connection-specific DNS Suffix . :
IP Address. . . . . . . . . . . . : 192.168.1.100
Subnet Mask . . . . . . . . . . . : 255.255.255.0
IP Address. . . . . . . . . . . . : fe80::211:11ff:fee1:f303%4
Default Gateway . . . . . . . . . : 192.168.1.1

Tunnel adapter Teredo Tunneling Pseudo-Interface:

Connection-specific DNS Suffix . :
IP Address. . . . . . . . . . . . :
3ffe:831f:4004:1956:0:fbde:bd0a:e50b
IP Address. . . . . . . . . . . . : fe80::5445:5245:444f%5
Default Gateway . . . . . . . . . : ::

Tunnel adapter Automatic Tunneling Pseudo-Interface:

Connection-specific DNS Suffix . :
IP Address. . . . . . . . . . . . : fe80::5efe:192.168.1.100%2
Default Gateway . . . . . . . . . :

E:\UserData\Dennis Lee Bieber\My Documents>
**** END of captured output
**** START of captured output
dir
Volume in drive E is Data
Volume Serial Number is 2626-D991

Directory of E:\UserData\Dennis Lee Bieber\My Documents

10/24/2005 09:23 AM <DIR>
.
10/24/2005 09:23 AM <DIR>
..
07/25/2005 10:39 PM <DIR>
.metadata
10/06/2005 09:54 AM <DIR>
Ada Progs
08/13/2005 02:01 PM <DIR>
Agent Data
10/21/2005 09:29 AM 421,820 apress_offer.pdf
07/03/2005 11:36 AM 132 cp.py
07/17/2005 12:25 PM <DIR>
Cyberlink
07/06/2005 09:32 AM 102,400 db1.mdb
07/26/2005 12:20 AM 26,614 eclipse_code.xml
10/24/2005 01:08 AM <DIR>
Eudora
06/24/2005 08:50 PM 667 fake_oosums.ads
06/24/2005 08:50 PM 695 fake_oosums.ali
09/06/2005 09:01 PM <DIR>
Genealogy
07/13/2005 10:56 PM <DIR>
HomeSite
05/08/2005 01:05 PM <DIR>
Investing
10/21/2005 10:04 PM <DIR>
Java Progs
08/04/2005 10:13 PM 162 main.py
10/11/2005 10:43 PM <DIR>
My Downloads
05/01/2005 10:31 AM <DIR>
My eBooks
04/22/2005 12:09 AM <DIR>
My Music
07/10/2005 11:43 AM <DIR>
My Pictures
06/29/2005 11:55 PM <DIR>
My PSP Files
05/23/2005 09:30 AM <DIR>
My Videos
05/01/2005 12:49 PM <DIR>
Office Documents
06/27/2005 03:19 PM 7,961,778
org.eclipse.jdt.doc.user.I20050627-1435.pdf
06/27/2005 03:19 PM 6,791,109
org.eclipse.platform.doc.user.I20050627-1435.pdf
10/11/2005 10:52 PM 56 oth_tsr_rm_750.ram
07/20/2005 09:32 AM 108,457 parkerred15yc.jpg
09/03/2005 10:36 PM <DIR>
Python Progs
10/20/2005 10:38 PM <DIR>
Quicken
07/10/2005 12:09 PM 3,356,248 results.xml
06/11/2005 12:03 PM 935 Scout_Ship Database.lnk
07/03/2005 12:38 PM <DIR>
Scout_Ship My Documents
10/24/2005 09:23 AM 971 Script1.py
09/25/2005 12:40 PM 1,107 Script1_old.py
08/28/2005 11:47 AM <DIR>
SimpleMu Logs
06/24/2005 08:56 PM 1,201 student_pack.ads
06/24/2005 08:49 PM 1,144 student_pack.ads.0
06/24/2005 08:56 PM 1,342 student_pack.ali
08/02/2005 11:39 PM 4,096 t.DOC
06/20/2005 10:11 AM 104 t.rx
08/05/2005 08:41 PM 66,452 Untitled-1.tif
08/05/2005 08:41 PM <DIR>
VCheck
10/03/2005 02:58 AM <DIR>
Visual Studio
10/03/2005 02:51 AM <DIR>
Visual Studio 2005
21 File(s) 18,847,490 bytes
25 Dir(s) 267,162,845,184 bytes free

E:\UserData\Dennis Lee Bieber\My Documents>
**** END of captured output

E:\UserData\Dennis Lee Bieber\My Documents>

-=-=-=-=-=-=-=-=-

Note that my "getLine()" has to return on either a real EOL, OR on
the end of a command prompt ("stuff>"). I also had to initialize the
prompt at the start. If someone issued a "cd" command to the subprocess,
the prompt would be all out of sequence, and the code would hang.

You''ll also note that the prompt /ends/ a capture sequence, and the
next command is the start of the /next/ capture sequence.

--

> ================================================== ============<
> wl*****@ix.netcom.com | Wulfraed Dennis Lee Bieber KD6MOG<
> wu******@dm.net | Bestiaria支持人员<
> ================================================== ============<
>主页:< http://www.dm.net/~wulfraed/> <
>溢出页面:< http://wlfraed.home.netcom.com/> <
> ================================================== ============ <
> wl*****@ix.netcom.com | Wulfraed Dennis Lee Bieber KD6MOG <
> wu******@dm.net | Bestiaria Support Staff <
> ================================================== ============ <
> Home Page: <http://www.dm.net/~wulfraed/> <
> Overflow Page: <http://wlfraed.home.netcom.com/> <






实际上,我无法检查>只因为如果你是一个导演,一行可以

以>结尾但不是输出的结束


jas写道:
actually, i can''t check for ">" only because if you a dir, a line can
end with a > but is not the end of the output

jas wrote:
谢谢,这肯定是一个开始。正如你所提到的,cd是指cd。可能是一个问题。

或许可以查看该行是否以>结尾。已经足够了吗?

Dennis Lee Bieber写道:
Thanks, that is certainly a start. As you mentioned, the "cd" could is
an issue.

Perhaps checking to see if the line ends with ">" is sufficient?

Dennis Lee Bieber wrote:
2005年10月24日07:20:42 -0700,jas <共******* @ gmail.com>在comp.lang.python中声明了以下内容:
On 24 Oct 2005 07:20:42 -0700, "jas" <co*******@gmail.com> declaimed the
following in comp.lang.python:

我想开始一个新进程并能够从/向读/写
它。我尝试了类似的东西......

导入子进程为sp
p = sp.Popen(" cmd.exe",stdout = sp.PIPE)
p.stdin .write(" hostname \ n")
然而,它似乎无法奏效。我认为cmd.exe正在捕捉它。
Hi,
I would like to start a new process and be able to read/write from/to
it. I have tried things like...

import subprocess as sp
p = sp.Popen("cmd.exe", stdout=sp.PIPE)
p.stdin.write("hostname\n")

however, it doesn''t seem to work. I think the cmd.exe is catching it.



一:你没有读过任何返回的内容。输出......

二:输出似乎只在EOF上可用,这意味着
衍生的命令处理器必须先退出...虽然你可以读一个
角色一次,然后必须建立线条和预期的提示
字符串...

这似乎工作:
- = - = - = - = - = - = - = - = -
导入子流程
导入os

PROMPT = os.getcwd()+">"

def getLine (proc):
ld = []
而True:
c = proc.stdout.read(1)
如果c ==" \r":continue#如果c为无或c == \
"或者c ==">":break
ln ="" .join(ld)
返回ln

p = subprocess.Popen(" cmd .exe",stdout = subprocess.PIPE,stdin =
subprocess.PIPE)

打印****开始捕获的输出
而True:
ln = getLine(p)
打印ln
如果ln == PROMPT:break
print" **** END of capture output"

p。 stdin.write(" ipconfig \ n")
打印" ****捕获输出的开始时间"
而True:
ln = getLine(p)
打印ln
如果ln == PROMPT:break
print" **** END of capture output"

p.stdin.write(" dir \ n")
打印****开始捕获输出
而True:
ln = getLine(p)
打印ln
如果ln == PROMPT:break
打印****捕获输出结束

p.stdin.write(" exit \ n")
- = - = - = - = E =:= - = - = - = - = -
E: \ userData \ Denen Lee Bieber \ My Documents> python script1.py
****捕获输出的开始
Microsoft Windows XP [版本5.1.2600]
(C)版权所有1985-2001微软公司

E:\ UserData \ Denen Lee Bieber \我的文件>
****结束捕获的输出
**** START捕获的输出
ipconfig

Windows IP配置

以太网适配器本地连接:

特定于连接的DNS后缀。 :
IP地址。 。 。 。 。 。 。 。 。 。 。 。 :192.168.1.100
子网掩码。 。 。 。 。 。 。 。 。 。 。 :255.255.255.0
IP地址。 。 。 。 。 。 。 。 。 。 。 。 :fe80 :: 211:11ff:fee1:f303%4
默认网关。 。 。 。 。 。 。 。 。 :192.168.1.1

隧道适配器Teredo隧道伪接口:

特定于连接的DNS后缀。 :
IP地址。 。 。 。 。 。 。 。 。 。 。 。 :
3ffe:831f:4004:1956:0:fbde:bd0a:e50b
IP地址。 。 。 。 。 。 。 。 。 。 。 。 :fe80 :: 5445:5245:444f%5
默认网关。 。 。 。 。 。 。 。 。 :::

隧道适配器自动隧道伪接口:

特定于连接的DNS后缀。 :
IP地址。 。 。 。 。 。 。 。 。 。 。 。 :fe80 :: 5efe:192.168.1.100%2
默认网关。 。 。 。 。 。 。 。 。 :

E:\ UserData \ Denen Lee Bieber \我的文件>
****捕获输出的结束
****捕获输出的开始
dir
驱动器E中的卷是数据
卷序列号是2626-D991

目录E:\ UserData \ Denen Lee Bieber \ My Documents

10/24/2005 09:23 AM< DIR>

2005年10月24日上午09:23< DIR>
.. 07/25/2005 10:39 PM< DIR>
.metadata
10/06/2005 09:54 AM< DIR>
Ada Progs
08/13 / 2005 02:01 PM< DIR>
代理商数据
2005年10月21日上午09:29 421,820 apress_offer.pdf
07/03/2005 11:36 AM 132 cp.py
07/17/2005 12:25 PM< DIR>
Cyber​​link
07/06/2005 09:32 AM 102,400 db1.mdb
07/26/2005 12: 20 AM 26,614 eclipse_code.xml
10/24/2005 01:08 AM< DIR>
Eudora
06/24/2005 08:50 PM 667 fake_oosums.ads
06/24/2005 08:50 PM 695 fake_oosums.ali
09/06/2005 09:01 PM< DIR>
Genealogy
07/13/2005 10:56 PM< DIR>
HomeSite
05/08/2005 01:05 PM< DIR>
投资
2005/10/21 10:04< DIR>
Java Progs
08/04/2005 10:13 PM 162 main.py
10/11/2005 10:43 PM< DIR>
我的下载
05/01/2005 10:31 AM< DIR>
我的电子书
04/22/2005 12:09 AM < DIR>
我的音乐
07/10/2005 11:43 AM< DIR>
我的照片
06/29/2005 11:55 PM< DIR>
我的PSP档案
05/23/2005 09:30 AM< DIR>
我的视频
05/01/2005 12:49 PM< DIR>
Office文件
06/27/2005 03:19 PM 7,961,778
org.e clipse.jdt.doc.user.I20050627-1435.pdf
06/27/2005 03:19 PM 6,791,109
org.eclipse.platform.doc.user.I20050627-1435.pdf
10/11/2005 10:52 PM 56 oth_tsr_rm_750.ram
07/20/2005 09:32 AM 108,457 parkerred15yc.jpg
09/03/2005 10:36 PM< DIR>
Python Progs
2005年10月20日下午10:38< DIR>
Quicken
07/10/2005 12:09 PM 3,356,248 results.xml
06/11 / 2005年12:03 PM 935 Scout_Ship Database.lnk
2005年3月3日下午12:38< DIR>
Scout_Ship我的文件
2005年10月24日上午9:23 971 Script1。 2005年5月25日下午12:40 1,107 Script1_old.py
08/28/2005 11:47 AM< DIR>
SimpleMu日志
06/24/2005 08:56 PM 1,201 student_pack.ads
06/24/2005 08:49 PM 1,144 student_pack.ads.0
06/24/2005 08:56 PM 1,342 student_pack.ali
08 / 02/2 005 11:39 PM 4,096 t.DOC
06/20/2005 10:11 AM 104 t.rx
08/05/2005 08:41 PM 66,452 Untitled-1.tif
08 / 05/2005 08:41 PM< DIR>
VCheck
10/03/2005 02:58 AM< DIR>
Visual Studio
2005/10/03 02 :51 AM< DIR>
Visual Studio 2005
21个文件18,847,490字节
25 Dir(s)267,162,845,184字节免费

E:\ UserData \ Denen Lee Bieber \我的文件>
****收到的输出结束

E:\ UserData \ Denen Lee Bieber \我的文件>

- = - = - = - = - = - = - = - = -

请注意我的getLine()必须返回真正的EOL,或者在命令提示符的末尾(stuff>)返回。我还必须在开始时初始化
提示符。如果有人发出了cd,命令到子进程,提示将全部不按顺序,代码将挂起。

你还会注意到提示/结束/捕获序列,以及
下一个命令是/ next / capture序列的开始。

-



One: you didn''t read any of the "returned" output...

Two: the output only seems to be available upon EOF, which means the
spawned command processor has to exit first... Though you CAN read one
character at a time, and then have to build lines and expected prompt
strings...
This seems to work:
-=-=-=-=-=-=-=-=-
import subprocess
import os

PROMPT = os.getcwd() + ">"

def getLine(proc):
ld = []
while True:
c = proc.stdout.read(1)
if c == "\r": continue #skip Windows <cr>
if c != "\n": ld.append(c) #save all but <lf>
if c is None or c == "\n" or c == ">": break
ln = "".join(ld)
return ln

p = subprocess.Popen("cmd.exe", stdout=subprocess.PIPE, stdin =
subprocess.PIPE)

print "**** START of captured output"
while True:
ln = getLine(p)
print ln
if ln == PROMPT: break
print "**** END of captured output"

p.stdin.write("ipconfig\n")
print "**** START of captured output"
while True:
ln = getLine(p)
print ln
if ln == PROMPT: break
print "**** END of captured output"

p.stdin.write("dir\n")
print "**** START of captured output"
while True:
ln = getLine(p)
print ln
if ln == PROMPT: break
print "**** END of captured output"

p.stdin.write("exit\n")
-=-=-=-=-=-=-=-=-=-
E:\UserData\Dennis Lee Bieber\My Documents>python script1.py
**** START of captured output
Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.

E:\UserData\Dennis Lee Bieber\My Documents>
**** END of captured output
**** START of captured output
ipconfig

Windows IP Configuration
Ethernet adapter Local Area Connection:

Connection-specific DNS Suffix . :
IP Address. . . . . . . . . . . . : 192.168.1.100
Subnet Mask . . . . . . . . . . . : 255.255.255.0
IP Address. . . . . . . . . . . . : fe80::211:11ff:fee1:f303%4
Default Gateway . . . . . . . . . : 192.168.1.1

Tunnel adapter Teredo Tunneling Pseudo-Interface:

Connection-specific DNS Suffix . :
IP Address. . . . . . . . . . . . :
3ffe:831f:4004:1956:0:fbde:bd0a:e50b
IP Address. . . . . . . . . . . . : fe80::5445:5245:444f%5
Default Gateway . . . . . . . . . : ::

Tunnel adapter Automatic Tunneling Pseudo-Interface:

Connection-specific DNS Suffix . :
IP Address. . . . . . . . . . . . : fe80::5efe:192.168.1.100%2
Default Gateway . . . . . . . . . :

E:\UserData\Dennis Lee Bieber\My Documents>
**** END of captured output
**** START of captured output
dir
Volume in drive E is Data
Volume Serial Number is 2626-D991

Directory of E:\UserData\Dennis Lee Bieber\My Documents

10/24/2005 09:23 AM <DIR>
.
10/24/2005 09:23 AM <DIR>
..
07/25/2005 10:39 PM <DIR>
.metadata
10/06/2005 09:54 AM <DIR>
Ada Progs
08/13/2005 02:01 PM <DIR>
Agent Data
10/21/2005 09:29 AM 421,820 apress_offer.pdf
07/03/2005 11:36 AM 132 cp.py
07/17/2005 12:25 PM <DIR>
Cyberlink
07/06/2005 09:32 AM 102,400 db1.mdb
07/26/2005 12:20 AM 26,614 eclipse_code.xml
10/24/2005 01:08 AM <DIR>
Eudora
06/24/2005 08:50 PM 667 fake_oosums.ads
06/24/2005 08:50 PM 695 fake_oosums.ali
09/06/2005 09:01 PM <DIR>
Genealogy
07/13/2005 10:56 PM <DIR>
HomeSite
05/08/2005 01:05 PM <DIR>
Investing
10/21/2005 10:04 PM <DIR>
Java Progs
08/04/2005 10:13 PM 162 main.py
10/11/2005 10:43 PM <DIR>
My Downloads
05/01/2005 10:31 AM <DIR>
My eBooks
04/22/2005 12:09 AM <DIR>
My Music
07/10/2005 11:43 AM <DIR>
My Pictures
06/29/2005 11:55 PM <DIR>
My PSP Files
05/23/2005 09:30 AM <DIR>
My Videos
05/01/2005 12:49 PM <DIR>
Office Documents
06/27/2005 03:19 PM 7,961,778
org.eclipse.jdt.doc.user.I20050627-1435.pdf
06/27/2005 03:19 PM 6,791,109
org.eclipse.platform.doc.user.I20050627-1435.pdf
10/11/2005 10:52 PM 56 oth_tsr_rm_750.ram
07/20/2005 09:32 AM 108,457 parkerred15yc.jpg
09/03/2005 10:36 PM <DIR>
Python Progs
10/20/2005 10:38 PM <DIR>
Quicken
07/10/2005 12:09 PM 3,356,248 results.xml
06/11/2005 12:03 PM 935 Scout_Ship Database.lnk
07/03/2005 12:38 PM <DIR>
Scout_Ship My Documents
10/24/2005 09:23 AM 971 Script1.py
09/25/2005 12:40 PM 1,107 Script1_old.py
08/28/2005 11:47 AM <DIR>
SimpleMu Logs
06/24/2005 08:56 PM 1,201 student_pack.ads
06/24/2005 08:49 PM 1,144 student_pack.ads.0
06/24/2005 08:56 PM 1,342 student_pack.ali
08/02/2005 11:39 PM 4,096 t.DOC
06/20/2005 10:11 AM 104 t.rx
08/05/2005 08:41 PM 66,452 Untitled-1.tif
08/05/2005 08:41 PM <DIR>
VCheck
10/03/2005 02:58 AM <DIR>
Visual Studio
10/03/2005 02:51 AM <DIR>
Visual Studio 2005
21 File(s) 18,847,490 bytes
25 Dir(s) 267,162,845,184 bytes free

E:\UserData\Dennis Lee Bieber\My Documents>
**** END of captured output

E:\UserData\Dennis Lee Bieber\My Documents>

-=-=-=-=-=-=-=-=-

Note that my "getLine()" has to return on either a real EOL, OR on
the end of a command prompt ("stuff>"). I also had to initialize the
prompt at the start. If someone issued a "cd" command to the subprocess,
the prompt would be all out of sequence, and the code would hang.

You''ll also note that the prompt /ends/ a capture sequence, and the
next command is the start of the /next/ capture sequence.

--

> ================================================== ============<
> wl*****@ix.netcom.com | Wulfraed Dennis Lee Bieber KD6MOG<
> wu******@dm.net | Bestiaria支持人员<
> ================================================== ============<
>主页:< http://www.dm.net/~wulfraed/> <
>溢出页面:< http://wlfraed.home.netcom.com/> <
> ================================================== ============ <
> wl*****@ix.netcom.com | Wulfraed Dennis Lee Bieber KD6MOG <
> wu******@dm.net | Bestiaria Support Staff <
> ================================================== ============ <
> Home Page: <http://www.dm.net/~wulfraed/> <
> Overflow Page: <http://wlfraed.home.netcom.com/> <






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

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