Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1178267
  • 博文数量: 252
  • 博客积分: 5421
  • 博客等级: 大校
  • 技术积分: 2418
  • 用 户 组: 普通用户
  • 注册时间: 2007-06-17 12:59
文章分类

全部博文(252)

文章存档

2017年(3)

2016年(18)

2015年(31)

2014年(18)

2013年(7)

2012年(8)

2011年(12)

2010年(30)

2009年(32)

2008年(57)

2007年(36)

分类: 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 ""


阅读(1218) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~