如何在不阻止输出的情况下捕获输出 python
How to capture output without preventing it from output in python
我想在 python 中使用 运行 命令行并捕获输出。我可以使用 subprocess.check_output
。但是它会抑制输出,我怎么能不抑制控制台输出呢?
这个怎么样?
from subprocess import Popen, PIPE
proc = Popen(["/usr/bin/nc", "-l", "9999"], stdout=PIPE)
buffer = []
line = proc.stdout.readline()
while line:
buffer.append(line)
print "LINE", line.strip()
line = proc.stdout.readline()
print "buffer", ''.join(buffer)
使用另一个终端发送一些文本
nc localhost 9999
# type something. the text should appear from the python code
破解 nc,你也会得到 buffer
中的输出
我想在 python 中使用 运行 命令行并捕获输出。我可以使用 subprocess.check_output
。但是它会抑制输出,我怎么能不抑制控制台输出呢?
这个怎么样?
from subprocess import Popen, PIPE
proc = Popen(["/usr/bin/nc", "-l", "9999"], stdout=PIPE)
buffer = []
line = proc.stdout.readline()
while line:
buffer.append(line)
print "LINE", line.strip()
line = proc.stdout.readline()
print "buffer", ''.join(buffer)
使用另一个终端发送一些文本
nc localhost 9999
# type something. the text should appear from the python code
破解 nc,你也会得到 buffer
中的输出