欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python?subprocess.Popen?實(shí)時(shí)輸出?stdout的解決方法(正確管道寫法)

 更新時(shí)間:2023年07月18日 11:41:07   作者:墨痕訴清風(fēng)  
這篇文章主要介紹了Python?subprocess.Popen實(shí)時(shí)輸出stdout正確管道寫法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

大部分的程序是這樣的:

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

但是由于子程序沒有進(jìn)行 flush 的話,會(huì)把結(jié)果緩存到系統(tǒng)中。導(dǎo)致程序運(yùn)行完成,上面的程序才會(huì)進(jìn)行打出(會(huì)一直卡在readline這個(gè)函數(shù))。

解決方法:

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

實(shí)際弱口令我是這樣寫的

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

方法二(待測(cè)試)

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

到此這篇關(guān)于Python subprocess.Popen 實(shí)時(shí)輸出 stdout(正確管道寫法)的文章就介紹到這了,更多相關(guān)Python subprocess.Popen 實(shí)時(shí)輸出內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • python 負(fù)數(shù)取模運(yùn)算實(shí)例

    python 負(fù)數(shù)取模運(yùn)算實(shí)例

    這篇文章主要介紹了python 負(fù)數(shù)取模運(yùn)算實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2020-06-06
  • Python3 itchat實(shí)現(xiàn)微信定時(shí)發(fā)送群消息的實(shí)例代碼

    Python3 itchat實(shí)現(xiàn)微信定時(shí)發(fā)送群消息的實(shí)例代碼

    使用微信,定時(shí)往指定的微信群里發(fā)送指定信息。接下來通過本文給大家分享Python3 itchat實(shí)現(xiàn)微信定時(shí)發(fā)送群消息的實(shí)例代碼,需要的朋友可以參考下
    2019-07-07
  • Python實(shí)現(xiàn)數(shù)據(jù)透視表詳解

    Python實(shí)現(xiàn)數(shù)據(jù)透視表詳解

    今天小編就為大家分享一篇用Python實(shí)現(xiàn)數(shù)據(jù)的透視表的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2021-10-10
  • 基于python計(jì)算并顯示日間、星期客流高峰

    基于python計(jì)算并顯示日間、星期客流高峰

    這篇文章主要介紹了基于python顯示日間、星期客流高峰,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-05-05
  • Python異常信息的不同展現(xiàn)方法總結(jié)

    Python異常信息的不同展現(xiàn)方法總結(jié)

    在日常開發(fā)的過程中,當(dāng)代碼報(bào)錯(cuò)時(shí),我們通常要不斷打印、閱讀traceback提示信息,來調(diào)試代碼,這篇文章介紹了如何實(shí)現(xiàn)一個(gè)Exception?Hooks,使得traceback模塊的提示信息更加精確;同時(shí)還介紹了一些第三方庫,這些庫也提供了Exception?Hooks的功能
    2022-11-11
  • Pandas 多層索引操作的實(shí)現(xiàn)

    Pandas 多層索引操作的實(shí)現(xiàn)

    本文主要介紹了Pandas 多層索引操作的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2025-02-02
  • 利用Python寫了一個(gè)水果忍者小游戲

    利用Python寫了一個(gè)水果忍者小游戲

    這篇文章主要介紹了利用Python寫了一個(gè)水果忍者小游戲,
    2022-05-05
  • PyTorch中的方法torch.randperm()示例介紹

    PyTorch中的方法torch.randperm()示例介紹

    在 PyTorch 中,torch.randperm(n) 函數(shù)用于生成一個(gè)從 0 到 n-1 的隨機(jī)排列的整數(shù)序列,這篇文章主要介紹了PyTorch中的方法torch.randperm()介紹,需要的朋友可以參考下
    2024-05-05
  • Python中read()、readline()和readlines()三者間的區(qū)別和用法

    Python中read()、readline()和readlines()三者間的區(qū)別和用法

    這篇文章主要給大家介紹了關(guān)于Python中讀取文件的read()、readline()和readlines()方法三者間的區(qū)別和用法,需要的朋友可以參考下
    2017-07-07
  • Python通過rembg實(shí)現(xiàn)圖片背景去除功能

    Python通過rembg實(shí)現(xiàn)圖片背景去除功能

    在圖像處理領(lǐng)域,背景移除是一個(gè)常見且重要的任務(wù),Python中的rembg庫就是一個(gè)強(qiáng)大的工具,它基于深度學(xué)習(xí)技術(shù),能夠準(zhǔn)確、快速地移除圖像背景,本文將結(jié)合多個(gè)實(shí)際案例,詳細(xì)介紹rembg庫的安裝、基本用法、高級(jí)功能以及在實(shí)際項(xiàng)目中的應(yīng)用,需要的朋友可以參考下
    2024-09-09

最新評(píng)論