Python Forum

Full Version: print the result of iperf log
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
do anyone know how to print the result from the iperf log, test111.txt.
Do anyone have any idea on how to print the iperf log.
i wish to print like this:

Cycle: 0
[ ID] Interval Transfer Bandwidth
[ 5] 0.00-10.00 sec 11.0 GBytes 9.46 Gbits/sec sender
[ 5] 0.00-10.00 sec 11.0 GBytes 9.46 Gbits/sec receiver
Cycle: 1
[ ID] Interval Transfer Bandwidth
[ 5] 0.00-10.00 sec 11.3 GBytes 9.68 Gbits/sec sender
[ 5] 0.00-10.00 sec 11.3 GBytes 9.68 Gbits/sec receiver


import subprocess
import os
input11= 5
input_no=input("Please enter cycle: ")


if input_no =='':
    input_no=input11

for n in range(int(input_no)):

    command="iperf3 -c localhost --logfile test111.txt\n"
    p=subprocess.Popen(command, shell=True)
    print("Cycle:",n)
 
    p.wait()
open as a file after for loop
and read line by line

to open file:
    with open('test111.txt') as fp:
        for line in fp:
            line = line.strip()
            print(line)
hi Larzo60,
i have a question right now, how can i call function in a for loop.
i run a iperf command, and then wish to go to a function to print the text file. is there any idea about it.

My main code is to let user to type in the cycle, and then run iperf. When running iperf it will write al logfile, and i wish after running it can go to readfile function to put the related log information.

right now it occurs 'readfile' is not defined
# -*- coding: utf-8 -*-
import subprocess
import os
input11= 5
input_no=input("Please enter cycle: ")
# input_no= input("Enter a number: ")
if input_no =='':
    input_no=input11

for n in range(int(input_no)):
    #command="iperf3 -c 192.168.3.18 --logfile C:\\iperf-3.1.3-win64\n"
    command="iperf3 -c localhost --logfile test111.txt\n"
    p=subprocess.Popen(command, shell=True)
    print("Cycle:",n)
     
    p.wait()
    readfile()
	

def readfile():
    searchfile = open("test111.txt", "r")
    for line in searchfile:
        
        if "sender" in line: 
            #print("Cycle:\n", "=" *80)
            print (line)
        if "receiver" in line: 
            print (line)
        #count=+1
    searchfile.close()
readfile is defined after your loop, so 'not defined' is the proper error.
remember python is an interpreter, so it evaluates code as read.

Moving the loop code after the function definition will work.