Python?subprocess.Popen?實(shí)時(shí)輸出?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
但是由于子程序沒有進(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)文章希望大家以后多多支持腳本之家!
- python基礎(chǔ)教程之popen函數(shù)操作其它程序的輸入和輸出示例
- Python調(diào)用系統(tǒng)命令的四種方法詳解(os.system、os.popen、commands、subprocess)
- Python調(diào)用系統(tǒng)命令os.system()和os.popen()的實(shí)現(xiàn)
- 解決python3中os.popen()出錯(cuò)的問題
- python中的subprocess.Popen()使用詳解
- 對(duì)Python subprocess.Popen子進(jìn)程管道阻塞詳解
- Python中的Popen函數(shù)demo演示
相關(guān)文章
python 負(fù)數(shù)取模運(yùn)算實(shí)例
這篇文章主要介紹了python 負(fù)數(shù)取模運(yùn)算實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-06-06Python3 itchat實(shí)現(xiàn)微信定時(shí)發(fā)送群消息的實(shí)例代碼
使用微信,定時(shí)往指定的微信群里發(fā)送指定信息。接下來通過本文給大家分享Python3 itchat實(shí)現(xiàn)微信定時(shí)發(fā)送群消息的實(shí)例代碼,需要的朋友可以參考下2019-07-07Python實(shí)現(xiàn)數(shù)據(jù)透視表詳解
今天小編就為大家分享一篇用Python實(shí)現(xiàn)數(shù)據(jù)的透視表的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2021-10-10Python異常信息的不同展現(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-11PyTorch中的方法torch.randperm()示例介紹
在 PyTorch 中,torch.randperm(n) 函數(shù)用于生成一個(gè)從 0 到 n-1 的隨機(jī)排列的整數(shù)序列,這篇文章主要介紹了PyTorch中的方法torch.randperm()介紹,需要的朋友可以參考下2024-05-05Python中read()、readline()和readlines()三者間的區(qū)別和用法
這篇文章主要給大家介紹了關(guān)于Python中讀取文件的read()、readline()和readlines()方法三者間的區(qū)別和用法,需要的朋友可以參考下2017-07-07Python通過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