Python 实时读取 iperf3 命令输出并显示
使用 Python 写一个程序,运行如下命令 "iperf3 -c 1.1.1.1 -i", 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()
实时读取并每秒显示命令的每一行输出内容:您的代码已经可以实现实时读取并每秒显示命令的每一行输出。在您的代码中,使用了subprocess.Popen来创建一个子进程并运行命令,然后使用iter(process.stdout.readline, '')来循环读取子进程的输出。
在每次循环中,我们使用time.time()获取当前时间,并使用line.strip()去除输出行的换行符。最后,将当前时间和输出行打印出来。
如果您运行这段代码,将会实时读取iperf3 -c 192.168.124.92 -p11111 -i 1命令的输出,并每秒显示一次输出行。
原文地址: https://www.cveoy.top/t/topic/qzFb 著作权归作者所有。请勿转载和采集!