Python 实时读取 iperf3 命令输出并显示
使用 Python 编写程序,实时读取并显示运行 iperf3 -c 1.1.1.1 -i 命令的每一行输出。
方法一:使用 iter(process.stdout.readline, '')
import subprocess
import time
def run_iperf_command():
command = "iperf3 -c 192.168.124.92 -p11111 -i 1"
process = subprocess.Popen(command.split(), stdout=subprocess.PIPE, universal_newlines=True)
for line in iter(process.stdout.readline, ''):
print(time.time(), line.strip())
run_iperf_command()
方法二:使用 process.communicate() 每秒钟打印当前的行内容
import subprocess
import time
def run_iperf_command():
command = "iperf3 -c 1.1.1.1 -i 1"
process = subprocess.Popen(command.split(), stdout=subprocess.PIPE, universal_newlines=True)
while True:
output = process.stdout.readline()
if output == '' and process.poll() is not None:
break
if output:
print(time.time(), output.strip())
time.sleep(1)
run_iperf_command()
原文地址: https://www.cveoy.top/t/topic/qzE1 著作权归作者所有。请勿转载和采集!