python

关注公众号 jb51net

关闭
首页 > 脚本专栏 > python > Python subprocess.Popen 实时输出

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

作者:墨痕诉清风

这篇文章主要介绍了Python subprocess.Popen实时输出stdout正确管道写法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

大部分的程序是这样的:

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()

方法二(待测试)

import subprocess
from multiprocessing.dummy import Pool as ThreadPool
command = poc + ' -t ' + ip + ' -p ' + port
result = subprocess.getoutput(command)
if 'WARNING: SERVER IS VULNERABLE' in result:
    result = AAAAA
else:
    result = BBBBBB

到此这篇关于Python subprocess.Popen 实时输出 stdout(正确管道写法)的文章就介绍到这了,更多相关Python subprocess.Popen 实时输出内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文