我想通过subprocess.Popen()
运行一个进程并通过python shell与它通信,就像subprocess.Popen
的常见行为一样。除此之外,我想推测性地将STDIN和STDOUT记录到日志文件中。记录过程'STDIN和STDOUT
我该怎么办?
我想通过subprocess.Popen()
运行一个进程并通过python shell与它通信,就像subprocess.Popen
的常见行为一样。除此之外,我想推测性地将STDIN和STDOUT记录到日志文件中。记录过程'STDIN和STDOUT
我该怎么办?
假设漫谈意味着散漫和散漫意味着所有在同一个文件中,那么下面的代码段就是你所要求的。
话语记录与源的鉴别和相互作用
覆盖其通信方法等类似的问题here
import subprocess
def logcommunicate(self, s):
self.logfilehandle.write("Input "+s)
std = self.oldcommunicate(s)
self.logfilehandle.write("Output "+std[0])
return std
subprocess.Popen.oldcommunicate = subprocess.Popen.communicate
subprocess.Popen.communicate = logcommunicate
logfh = open("/tmp/communicate.log", "a")
proc = subprocess.Popen(['cat'], stdin=subprocess.PIPE, stdout=subprocess.PIPE)
proc.logfilehandle = logfh
result = proc.communicate("hello there\n")
print result
话语记录与源
首先使用StringIO的判别而不是文件,然后子类StringIO覆盖它的写入方法打开,附加时间戳和来源。然后写自定义比较的排序基于时间戳和源,时间戳第一功能,然后源输入,然后输出
with open("file.log","wb") as in logfile:
out = MyOutPutStringIO.StringIO()
in = MyInputStringIO.StringIO()
subprocess.Popen(cmd, shell=True, universal_newlines = True, stdin=in, stdout=out)
#Then after you are done
linestotal = []
for line in in.readlines():
linestotal.append(line)
for line in out.readlines():
linestotal.append(line)
linestotal.sort(customsortbasedontimestampandinput)
for line in linestotal.readlines():
logwrite.write(line)
话语测井
with open("file.log","wb") as in logfile:
subprocess.Popen(cmd, shell=True, universal_newlines = True, stdin=logfile, stdout=logfile)
相反如下所示
草书记录
with open("stdout.txt","wb") as out:
with open("stderr.txt","wb") as err:
with open("stdin.txt","wb") as in:
subprocess.Popen(cmd, shell=True, universal_newlines = True, stdin=in,stdout=out,stderr=err)
你是什么意思的话语?你的意思是他们都登录到同一个文件? – Appleman1234 2012-02-14 20:44:00
是的,但不只是在STDIN之后附加STDOUT。我需要它的话题,所以我可以告诉哪个输入带来了任何输出。 – iTayb 2012-02-14 20:46:56