#!/usr/bin/python<br />#coding=utf-8<br />#file : netiostat<br />#author : flynetcn<br />from __future__ import division<br />import sys<br />import os<br />import time<br />import signal<br />netcmd = '/sbin/ifconfig eth0 | grep bytes'<br />def getnetio(line):<br />s1 = line.find('RX bytes:')+9<br />e1 = line.find(' ', s1)<br />neti = line[s1:e1]<br />s2 = line.find('TX bytes:')+9<br />e2 = line.find(' ', s2)<br />neto = line[s2:e2]<br />return (int(neti), int(neto))</p><p>def int_handler(signum, frame):<br />print ""<br />sys.exit()<br />signal.signal(signal.SIGINT, int_handler)</p><p>line = os.popen(netcmd).readline().strip()<br />netio = getnetio(line)<br />neti_start = netio[0]<br />neto_start = netio[1]<br />time_start = time.time()<br />count = 60<br />while (count > 0):<br />count -= 1<br />time.sleep(1);<br />info = []<br />line = os.popen(netcmd).readline().strip()<br />netio = getnetio(line)<br />info.append("網路流入總量:%.4fm, 網路流出總量:%.4fm" % (netio[0]/1024/1024, netio[1]/1024/1024))<br />time_curr = time.time()<br />neti_total = netio[0]-neti_start<br />neto_total = netio[1]-neto_start<br />sec_total = time_curr-time_start<br />neti_start = netio[0]<br />neto_start = netio[1]<br />time_start = time_curr<br />info.append("當前網路流入速度:%.4fk/s" % (neti_total/sec_total/1024))<br />info.append("當前網路流出速度:%.4fk/s" % (neto_total/sec_total/1024))<br />show = ", ".join(info)<br />sys.stdout.write(show+"/r")<br />sys.stdout.flush()<br />print ""