zoukankan      html  css  js  c++  java
  • linux系统使用python监测网络接口获取网络的输入输出

    #!/usr/bin/env Python
    import time
    import sys

    if len(sys.argv) > 1:
     INTERFACE = sys.argv[1]
    else:
     INTERFACE = 'eth0'
    STATS = []
    print 'Interface:',INTERFACE

    def rx():
     ifstat = open('/proc/net/dev').readlines()
     for interface in  ifstat:
      if INTERFACE in interface:
       stat = float(interface.split()[1])
       STATS[0:] = [stat]

    def tx():
     ifstat = open('/proc/net/dev').readlines()
     for interface in  ifstat:
      if INTERFACE in interface:
       stat = float(interface.split()[9])
       STATS[1:] = [stat]

    print 'In   Out'
    rx()
    tx()

    while True:
     time.sleep(1)
     rxstat_o = list(STATS)
     rx()
     tx()
     RX = float(STATS[0])
     RX_O = rxstat_o[0]
     TX = float(STATS[1])
     TX_O = rxstat_o[1]
     RX_RATE = round((RX - RX_O)/1024/1024,3)
     TX_RATE = round((TX - TX_O)/1024/1024,3)
     print RX_RATE ,'MB  ',TX_RATE ,'MB'

  • 相关阅读:
    winsows10 小技巧
    数组与智能指针
    卸载 VS2015
    Effective C++
    修改 git commit 的信息
    线程管理
    并发编程简介
    个别算法详解
    git 删除某个中间提交版本
    git 查看某一行代码的修改历史
  • 原文地址:https://www.cnblogs.com/zhanglong66/p/6646481.html
Copyright © 2011-2022 走看看