简体   繁体   English

使用python的子进程从iperf3获取实时output

[英]Getting real time output from iperf3 using python's subprocess

This is a follow-on to: Getting realtime output using subprocess这是后续: Getting realtime output using subprocess

I'm trying to use subprocess to capture output from iperf3 in real time (using python 3.6 on windows).我正在尝试使用子进程实时从 iperf3 捕获 output(在 Windows 上使用 python 3.6)。 The goal is to leave the iperf3 session running continuously and grab the data to update a real time plot.目标是让iperf3 session持续运行并抓取数据实时更新一个plot。

I created an implementation based on the referenced question (see code at end of post), but the code still waits on the first "readline" call for the iperf3 session to complete.我根据引用的问题创建了一个实现(请参阅帖子末尾的代码),但代码仍在等待 iperf3 session 的第一个“readline”调用完成。

Output and desired behavior Output 和期望的行为

My code returns the output:我的代码返回 output:

Iperf test
Popen returns after: 0.012966156005859375 seconds
Readline 0 returned after: 3.2275266647338867 seconds, line was: Connecting to host 10.99.99.21, port 5201
Readline 1 returned after: 3.2275266647338867 seconds, line was: [  4] local 10.99.99.7 port 55563 connected to 10.99.99.21 port 5201
Readline 2 returned after: 3.2275266647338867 seconds, line was: [ ID] Interval           Transfer     Bandwidth
Readline 3 returned after: 3.2275266647338867 seconds, line was: [  4]   0.00-0.50   sec  27.4 MBytes   458 Mbits/sec
Readline 4 returned after: 3.2275266647338867 seconds, line was: [  4]   0.50-1.00   sec  29.0 MBytes   486 Mbits/sec
Exited

The outputs show that the first readline call doesn't return until after 3 seconds, when the iperf session completes.输出显示第一个 readline 调用直到 3 秒后才返回,此时 iperf session 完成。 The desired behavior is that the readline calls 0, 1, and 2 return almost immediately, and readline call #3 returns after approx.期望的行为是 readline 调用 0、1 和 2 几乎立即返回,而 readline 调用 #3 在大约 3 秒后返回。 0.5 seconds, as soon as iperf3 has completed the first 0.5 second reporting interval. 0.5 秒,一旦 iperf3 完成第一个 0.5 秒报告间隔。

Code代码

import subprocess
import time

if __name__ == "__main__":
    print('Iperf test')
    tref = time.time()

    reads_to_capture = 5
    times = [0] * reads_to_capture
    lines = [''] * reads_to_capture

    interval = 0.5
    ip = '10.99.99.21' # Iperf server IP address
    process = subprocess.Popen(f'iperf3 -c {ip} -f m -i {interval} -t 3', encoding = 'utf-8',
            stdout=subprocess.PIPE)

    print(f'Popen returns after: {time.time() - tref} seconds')

    cnt = 0

    while True:
        output = process.stdout.readline()
        if cnt < reads_to_capture: # To avoid flooding the terminal, only print the first 5
            times[cnt] = time.time() - tref
            lines[cnt] = output
            cnt = cnt + 1
        if output == '': 
            rc = process.poll()
            if rc is not None:
                break

    rc = process.poll()

    for ii in range(reads_to_capture):
        print(f'Readline {ii} returned after: {times[ii]} seconds, line was: {lines[ii].strip()}')

    print('Exited')

Sorry for my late answer.对不起我迟到的答案。 There is an API for Iperf3, luckily this comes with the standard iperf3 build/installation. Iperf3 有一个 API,幸运的是,它带有标准的 iperf3 构建/安装。

This API allows python to take the common output of iperf3.这个API允许python取iperf3的常用output。

I let you the official website of the python wrapper for iperf3.我让你官网的python wrapper for iperf3。 It comes with simple examples for your use.它带有简单的示例供您使用。 Hope I could have gave you an answer.希望我能给你一个答案。

https://iperf3-python.readthedocs.io/en/latest/index.html https://iperf3-python.readthedocs.io/en/latest/index.html

In order to get the real time output from iperf3 to the subprocess.Popen you need the --forceflush flag in the iperf3 command.为了从 iperf3 获取实时 output 到subprocess.Popen ,您需要在 iperf3 命令中使用--forceflush标志。 The --forceflush flag is introduced in iperf 3.1.5, unfortunately the official compiled iperf3.exe file have only until iperf 3.1.3. --forceflush标志是在 iperf 3.1.5 中引入的,不幸的是官方编译的 iperf3.exe 文件直到 iperf 3.1.3 才有。

Two solution for you,两种解决方案,

  1. get the iperf >= 3.1.5 from non official route like: https://files.budman.pw/从非官方途径获取 iperf >= 3.1.5,例如: https://files.budman.pw/
  2. use linux's iperf3使用 linux 的 iperf3

Attached with my code:附上我的代码:

import subprocess

my_iperf_process = subprocess.Popen(["iperf3","-c","192.168.0.1","--forceflush"],stdout=subprocess.PIPE)

for line in my_iperf_process.stdout:
    print(line)

The help message of --forceflush : --forceflush的帮助信息:

--forceflush    force flushing output at every interval

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python iperf3 得到“无法将 cookie 发送到服务器:” - Python iperf3 getting "unable to send cookie to server: " python的subprocess.Popen()的实时颜色刷新输出 - Real-time colour refreshing output from python's subprocess.Popen() 是否可以实时将python子流程的输出流式传输到网页? - Is it possible to stream output from a python subprocess to a webpage in real time? Python Subprocess 从 stdout 块读取(实时读取输出) - Python Subprocess reading from stdout blocks (reading output in real time) 使用iperf3和tcpdump计算发送和接收数据包之间的时间延迟? - Calculate the time delay between send and received packets using iperf3 and tcpdump? 为什么从Python 3子进程读取实时输出需要按Enter键以进行数据输出? - Why does reading real time output from Python 3 subprocess require pressing enter for data output? 实时显示子流程输出 - Display subprocess output in real time 在python中运行一个子进程,都以“实时”显示输出并将其保存到变量中 - Run a subprocess in python and both show the output in “real time” and save it to a variable 实时管道与 python subprocess.check_output 或解决方法? - Real-time piping with python subprocess.check_output or workaround? 如何使用实时 output 运行 python 子进程? - How to run python subprocess with real-time output?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM