def get_rx(interface = 'eth0'): rsbytes = [] cmd = 'cat /proc/net/dev' r = os.popen(cmd).readlines() if len(r) < 4: print "error: can't find eth interface" return rsbytes interface_dict = {} for i in xrange(2,len(r),1): #从 lo 开始 interface_name = r[i].split()[0].split(':')[0] interface_dict[interface_name] = i
if interface in interface_dict: position = interface_dict.get(interface) recvbytes = r[position].split()[1] sendbytes = r[position].split()[9] rsbytes.append(int(recvbytes)) rsbytes.append(int(sendbytes))
return rsbytes
def iftop_interface(interface = 'eth0'): begin = int(time.time()) beginrs = get_rx(interface) if not beginrs: print 'error: can not find interface %s' % interface return while True: time.sleep(2) endrs = get_rx(interface) end = int(time.time()) rxrate = float((endrs[0] - beginrs[0])) / (end - begin) * 8 sxrate = float((endrs[1] - beginrs[1])) / (end - begin) * 8 tl = time.localtime(end) date = time.strftime('%m-%d %H:%M:%S', tl) cout = "%s [recv(rate) = %s Mbps] [send(rate) = %s Mbps] \n" % (date,rxrate / 1000000,sxrate / 1000000)