您的位置:首页 > 理论基础 > 计算机网络

查看linux网络流量小程序: netiostat

2011-05-01 08:52 309 查看
#!/usr/bin/python
#coding=utf-8
#file : netiostat
#author : flynetcn
from __future__ import division
import sys
import os
import time
import signal
netcmd = '/sbin/ifconfig eth0 | grep bytes'
def getnetio(line):
	s1 = line.find('RX bytes:')+9
	e1 = line.find(' ', s1)
	neti = line[s1:e1]
	s2 = line.find('TX bytes:')+9
	e2 = line.find(' ', s2)
	neto = line[s2:e2]
	return (int(neti), int(neto))

def int_handler(signum, frame):
	print ""
	sys.exit()
signal.signal(signal.SIGINT, int_handler)

line = os.popen(netcmd).readline().strip()
netio = getnetio(line)
neti_start = netio[0]
neto_start = netio[1]
time_start = time.time()
count = 60
while (count > 0):
	count -= 1
	time.sleep(1);
	info = []
	line = os.popen(netcmd).readline().strip()
	netio = getnetio(line)
	info.append("网络流入总量:%.4fm, 网络流出总量:%.4fm" % (netio[0]/1024/1024, netio[1]/1024/1024))
	time_curr  = time.time()
	neti_total = netio[0]-neti_start
	neto_total = netio[1]-neto_start
	sec_total  = time_curr-time_start
	neti_start = netio[0]
	neto_start = netio[1]
	time_start = time_curr
	info.append("当前网络流入速度:%.4fk/s" % (neti_total/sec_total/1024))
	info.append("当前网络流出速度:%.4fk/s" % (neto_total/sec_total/1024))
	show = ", ".join(info)
	sys.stdout.write(show+"/r")
	sys.stdout.flush()
print ""
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: