简体   繁体   中英

Sort data from a CSV file and implement a graph using python

I am trying to generate a graph (time Vs bandwidth) from a CSV file. The CSV file contains the data of stdout after executing a python script. Now, I have to keep only interval and bandwidth in the CSV file and plot a graph from there. The upper file is the python script I'm using and the lower file is the CSV file from where I need to sort data. Suggestions are truly welcome to edit the python script or possibly to write a new python script to generate the plot. Thanks in advance..

import sys
import time
import select
import csv
import paramiko

class Logger(object):
    def __init__(self):
        self.terminal = sys.stdout
        self.log = open("logfile.csv", "a")

    def write(self, message):
        self.terminal.write(message)
        self.log.write(message)

    def flush(self):
        #this flush method is needed for python 3 compatibility.
        #this handles the flush command by doing nothing.
        #you might want to specify some extra behavior here.
        pass
sys.stdout = Logger()

host = '169.254.115.1'
i = 1

#
# Try to connect to the host.
# Retry a few times if it fails.
#
while True:
    print ('Trying to connect to %s (%i/2)' % (host, i))

    try:
        ssh = paramiko.SSHClient()
        ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        ssh.connect(host, port=22, username='user', password='user')
        print ("Connected to %s" % host)
        break
    except paramiko.AuthenticationException:
        print ("Authentication failed when connecting to %s") % host
        sys.exit(1)
    except:
        print ("Could not SSH to %s, waiting for it to start" % host)
        i += 1
        time.sleep(2)

    # If we could not connect within time limit
    if i == 2:
        print ("Could not connect to %s. Giving up") % host
        sys.exit(1)

# Send the command (non-blocking)
stdin, stdout, stderr = ssh.exec_command("cd /opt/cohda/test; sudo ./runtest_iperf_tx.sh")

# Wait for the command to terminate

while not stdout.channel.exit_status_ready():

    # Only print data if there is data to read in the channel
    if stdout.channel.recv_ready():
        rl, wl, xl = select.select([stdout.channel], [], [], 0.0)
        if len(rl) > 0:
            # Print data from stdout
            print (stdout.channel.recv(1024)),



#
# Disconnect from the host
#


print ("Command done, closing SSH connection")
ssh.close()
Trying to connect to 169.254.115.1 (1/2)                
Connected to 169.254.115.1              
b'Usage: ./runtest_iperf_tx.sh [ChanSCH] [Mode: none|remote|local|target] [MCS] [PRIORITY]\nUsing: ./runtest_iperf_tx.sh 180 target MK2MCS_R12QPSK MK2_PRIO_4\n'                
b'Interface:         wave-raw\nChannel: SCH\nRadio: B\nChannelNumber:  180\nDefaultMCS:     10\nDefaultTxPower: 40\nDefaultTRC:     0\nDefaultTPC:     0\nBandwidth:      10\nDualTxControl:  0\nChannelUtilisationPeriod: 49\nTxAntenna:      3\nRxAntenna:      3\nMACAddr: 04:e5:48:00:00:b1\nFilter:            0x88b6  0x86dd  0x0800  0x0806\nMAC Address: 04:e5:48:00:00:b1\n'   
b'  ChannelNumber:  180\n  DefaultMCS:     10\n  DefaultTxPower: 40\n  DefaultTRC:     0\n  DefaultTPC:     0\n  Bandwidth:      10\n  DualTxControl:  0\n  ChannelUtilisationPeriod: 49\n  TxAntenna:      3\n  RxAntenna:      3\n  MACAddr: 04:e5:48:00:00:b1\n  Filter:         0x88b6  0x86dd  0x0800  0x0806\n'   
b'PING 192.168.234.1 (192.168.234.1) from 192.168.234.2 wave-data: 56(84) bytes of data.\n64 bytes from 192.168.234.1: icmp_seq=1 ttl=64 time=1998 ms\n'                
b'64 bytes from 192.168.234.1: icmp_seq=2 ttl=64 time=995 ms\n'             
b'64 bytes from 192.168.234.1: icmp_seq=3 ttl=64 time=1.85 ms\n'                
b'\n'               
b'--- 192.168.234.1 ping statistics ---\n3 packets transmitted   3 received  0% packet loss  time 2003ms\nrtt min/avg/max/mdev = 1.859/998.493/1998.492/815.126 ms   pipe 2\n'
b'------------------------------------------------------------\nServer listening on TCP port 5001\nTCP window size: 0.08 MByte (default)\n------------------------------------------------------------\n------------------------------------------------------------\nClient connecting to 192.168.234.1     TCP port 5001\nTCP window size: 0.04 MByte (default)\n------------------------------------------------------------\n[  5] local 192.168.234.2 port 59528 connected with 192.168.234.1 port 5001\n'         
b'[ ID] Interval       Transfer     Bandwidth\n[  5]  0.0- 1.0 sec  0.62 MBytes  5.24 Mbits/sec\n'              
b'[  5]  1.0- 2.0 sec  0.50 MBytes  4.19 Mbits/sec\n'               
b'[  5]  2.0- 3.0 sec  0.50 MBytes  4.19 Mbits/sec\n'               
b'[  5]  3.0- 4.0 sec  0.50 MBytes  4.19 Mbits/sec\n'               
b'[  5]  4.0- 5.0 sec  0.50 MBytes  4.19 Mbits/sec\n'               
b'[  5]  5.0- 6.0 sec  0.50 MBytes  4.19 Mbits/sec\n'               
b'[  5]  6.0- 7.0 sec  0.50 MBytes  4.19 Mbits/sec\n'               
b'[  5]  7.0- 8.0 sec  0.50 MBytes  4.19 Mbits/sec\n'               
b'[  5]  8.0- 9.0 sec  0.50 MBytes  4.19 Mbits/sec\n'               
b'[  5]  9.0-10.0 sec  0.50 MBytes  4.19 Mbits/sec\n'               
b'[  5] 10.0-11.0 sec  0.50 MBytes  4.19 Mbits/sec\n'               
b'[  5] 11.0-12.0 sec  0.50 MBytes  4.19 Mbits/sec\n'               
b'[  5] 12.0-13.0 sec  0.50 MBytes  4.19 Mbits/sec\n'               
b'[  5] 13.0-14.0 sec  0.50 MBytes  4.19 Mbits/sec\n'               
b'[  5] 14.0-15.0 sec  0.50 MBytes  4.19 Mbits/sec\n'               
b'[  5] 15.0-16.0 sec  0.50 MBytes  4.19 Mbits/sec\n'               
b'[  5] 16.0-17.0 sec  0.50 MBytes  4.19 Mbits/sec\n'               
b'[  5] 17.0-18.0 sec  0.50 MBytes  4.19 Mbits/sec\n'               
b'[  5] 18.0-19.0 sec  0.50 MBytes  4.19 Mbits/sec\n'               
b'[  5] 19.0-20.0 sec  0.50 MBytes  4.19 Mbits/sec\n[  5]  0.0-20.3 sec  10.2 MBytes  4.24 Mbits/sec\n'             
b'[  4] local 192.168.234.2 port 5001 connected with 192.168.234.1 port 44782\n'                
b'[  4]  0.0- 1.0 sec  0.48 MBytes  4.05 Mbits/sec\n'               
b'[  4]  1.0- 2.0 sec  0.50 MBytes  4.18 Mbits/sec\n'               
b'[  4]  2.0- 3.0 sec  0.50 MBytes  4.22 Mbits/sec\n'               
b'[  4]  3.0- 4.0 sec  0.51 MBytes  4.29 Mbits/sec\n'               
b'[  4]  4.0- 5.0 sec  0.51 MBytes  4.25 Mbits/sec\n'               
b'[  4]  5.0- 6.0 sec  0.50 MBytes  4.16 Mbits/sec\n'               
b'[  4]  6.0- 7.0 sec  0.51 MBytes  4.31 Mbits/sec\n'               
b'[  4]  7.0- 8.0 sec  0.50 MBytes  4.22 Mbits/sec\n'               
b'[  4]  8.0- 9.0 sec  0.52 MBytes  4.33 Mbits/sec\n'               
b'[  4]  9.0-10.0 sec  0.51 MBytes  4.24 Mbits/sec\n'               
b'[  4] 10.0-11.0 sec  0.47 MBytes  3.95 Mbits/sec\n'               
b'[  4] 11.0-12.0 sec  0.54 MBytes  4.55 Mbits/sec\n'               
b'[  4] 12.0-13.0 sec  0.50 MBytes  4.18 Mbits/sec\n'               
b'[  4] 13.0-14.0 sec  0.49 MBytes  4.14 Mbits/sec\n'               
b'[  4] 14.0-15.0 sec  0.51 MBytes  4.31 Mbits/sec\n'               
b'[  4] 15.0-16.0 sec  0.49 MBytes  4.09 Mbits/sec\n'               
b'[  4] 16.0-17.0 sec  0.49 MBytes  4.11 Mbits/sec\n'               
b'[  4] 17.0-18.0 sec  0.52 MBytes  4.34 Mbits/sec\n'               
b'[  4] 18.0-19.0 sec  0.51 MBytes  4.30 Mbits/sec\n'               
b'[  4] 19.0-20.0 sec  0.51 MBytes  4.25 Mbits/sec\n'               
b'[  4]  0.0-20.6 sec  10.4 MBytes  4.23 Mbits/sec\n'               
b'CCH: \n  RxReport: /dev/null\n  RxLog:    /dev/null\nSCH: \n  RxReport: /dev/null\n  RxLog:    /dev/null\n'               
b'CCH: \n  RxReport: /dev/null\n  RxLog:    /dev/null\nSCH: \n  RxReport: /dev/null\n  RxLog:    /dev/null\n'               
Command done     closing SSH connection         

you need to store the data in a table or similar data structure. There are lots of libraries for data analysis with python. Try pandas . for plotting you could also use matplotlib

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM