Python Forum
print the result of iperf log - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: print the result of iperf log (/thread-24605.html)



print the result of iperf log - jacklee26 - Feb-22-2020

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()



RE: print the result of iperf log - Larz60+ - Feb-22-2020

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)



RE: print the result of iperf log - jacklee26 - Feb-23-2020

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()



RE: print the result of iperf log - Larz60+ - Feb-23-2020

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.