flynetcnflynetcn.blog.chinaunix.net
flynetcn
全部博文(252)
2017年(3)
2016年(18)
2015年(31)
2014年(18)
2013年(7)
2012年(8)
2011年(12)
2010年(30)
2009年(32)
2008年(57)
2007年(36)
MagicBoy
wangfei0
大鬼不动
dajun1
Leejiter
ahuimsn
zscc
cynthia
Bsolar
khls27
asodin
kenpear
分类: Python/Ruby
2011-05-01 09:00:09
#!/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 ""
上一篇:Some of the Best Open Source Project's in VC++
下一篇:colinux网卡设置
登录 注册