adityashrm21 / Network-Usage-Tracker

A python script to keep track of, check and control the network usage
MIT License
12 stars 6 forks source link

IndexError: list index out of range #3

Open mehtaparitosh opened 3 years ago

mehtaparitosh commented 3 years ago

$python3 bandwidth.py 500 MiB

Traceback (most recent call last): File "bandwidth.py", line 57, in main() File "bandwidth.py", line 54, in main monitor(limit, unit) File "bandwidth.py", line 29, in monitor if unit==l[5] and limit<l[4]: IndexError: list index out of range

mehtaparitosh commented 3 years ago

Altered it for python3

#!/usr/bin/env python

import os
import sys
import time
#import re
import threading
import subprocess
import tkinter
import tkinter.messagebox as tkMessageBox

def monitor(limit, unit):
    check="vnstat"
    #os.system(check)
    proc=subprocess.Popen(check, shell=True, stdout=subprocess.PIPE)
    output=proc.communicate()
    output=str(output)
    #print output
    l = []
    for t in output.split():
        try:
            if t=="MiB" or t=="GiB":
                l.append(t)
            else:
                l.append(float(t))          
        except ValueError:
            pass
    #print(l)
    if unit==l[5] and limit<l[4]:
        print("\nnetwork usage limit exceeded!\n")
        top = Tkinter.Tk()
        def hello():
            tkMessageBox.showinfo("Warning!", "Network usage limit exceeded!!!!")
        B1 = Tkinter.Button(top, text = "Warning", command = hello)
        B1.pack()
        top.mainloop()
    arg=[limit,unit]
    threading.Timer(60.0, monitor, arg).start()
#def callMonitor(limit, unit):
#   t=threading.Timer(4.0, monitor(limit, unit))
#   t.start()
#   monitor(limit, unit)

def main():
    if len(sys.argv) >3 or len(sys.argv)<3:
        print('command usage: python bandwidth.py <data usage in MiB or GiB>')
        print('example: python bandwidth.py 500 MiB')
        print('or python bandwidth.py 2 GiB')
        exit(1)
    else:
        limit=float(sys.argv[1])
        unit=str(sys.argv[2])
        #callMonitor(limit, unit)
        monitor(limit, unit)

if __name__ == '__main__':
    main()