Python subprocess.Popen 实时输出 stdout(正确管道写法)

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/u012206617/article/details/84560895

大部分的程序是这样的:

from subprocess import Popen, PIPE, STDOUT

p = Popen(cmd, stdout=PIPE, stderr=STDOUT, shell=True)
while True:
    print(p.stdout.readline())
    if not line: 
        break

但是由于子程序没有进行 flush 的话,会把结果缓存到系统中。导致程序运行完成,上面的程序才会进行打出(会一直卡在readline这个函数)。

解决方法:

p = subprocess.Popen(cmd, stdout=subprocess.PIPE, bufsize=1)
for line in iter(p.stdout.readline, b''):
    print line,
p.stdout.close()
p.wait()

实际弱口令我是这样写的

import subprocess     #Popen


proc = subprocess.Popen(medusaCMD, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell=True)
for line in iter(proc.stdout.readline, 'b'):
    print line
    if not subprocess.Popen.poll(proc) is None:
        if line == "":
            break
proc.stdout.close()

记小的写法

proc = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
try:
    while True:
        buff = proc.stdout.readline()
        print(buff)
        if buff == '' and proc.poll() != None:
            break
        else:
            .....
except Exception:
    data["status"] = -1
finally:
    return data

单次管道输出写法

# -*- coding: UTF-8 -*-

import re
import sys
import subprocess
from subprocess import Popen, PIPE, STDOUT

#docker_info = {"CONTAINER ID":"", "NAME":"", "CPU %":"", "MEM USAGE / LIMIT":"", \
#               "MEM %":"", "NET I/O":"", "BLOCK I/O":"", "PIDS":""}
docker_list = []


cmd = "docker stats -a --no-stream"
proc = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
try:
    buff = proc.communicate()
    stritem = buff[0]
    str_list = re.split(r'  +|\n', stritem)
    for i in range(8, len(str_list)-1):
        if i % 8 == 0:
            value = 0
            docker_info = {}
            docker_info["CONTAINER ID"] = str_list[i]
        else:
            value += 1
            if value == 1:
                docker_info["NAME"] = str_list[i]
            elif value == 2:
                docker_info["CPU %"] = str_list[i]
            elif value == 3:
                docker_info["MEM USAGE / LIMIT"] = str_list[i]
            elif value == 4:
                docker_info["MEM %"] = str_list[i]
            elif value == 5:
                docker_info["NET I/O"] = str_list[i]
            elif value == 6:
                docker_info["BLOCK I/O"] = str_list[i]
            elif value == 7:
                docker_info["PIDS"] = str_list[i]
                docker_list.append(docker_info)
                value = 0
    print docker_list
except Exception as e:
    print "error", e
    sys.exit(1)
    proc.stdout.close()

 

展开阅读全文

没有更多推荐了,返回首页