日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

python如何查看系統網絡流量的信息

系統 1674 0

前言

流量信息可以直接在 /proc/net/dev 中進行查看,筆者實現的程序使用命令:

            
python net.py interface
          

其中 interface 為網卡名稱,使用什么網卡,電腦有哪些網卡,可以使用

            
sudo ifconfig
          

進行查看。

Python實現的程序如下:

            
# coding:utf-8
import sys, time, os


'''
Inter-|  Receive                        | Transmit
 face |bytes  packets errs drop fifo frame compressed multicast|bytes  packets errs drop fifo colls carrier compressed
  lo:  28169   364  0  0  0   0     0     0  28169   364  0  0  0   0    0     0
 wlan1: 7432984  6018  0  0  0   0     0     0  681381  6115  0  0  0   0    0     0
vmnet1:    0    0  0  0  0   0     0     0    0   56  0  0  0   0    0     0
vmnet8:    0    0  0  0  0   0     0     0    0   55  0  0  0   0    0     0
 eth0:    0    0  0  0  0   0     0     0    0    0  0  0  0   0    0     0

'''

_unit_=['B','KB','MB','GB','TB']

def get_net_data(interface):
  for line in open('/proc/net/dev', 'r'):
    if line.split(':')[0].find(interface)>=0:
      return map(int, line.split(':')[1].split())

def convert_bytes_to_string(b):
  cnt = 0
  while b >= 1024.0:
    b = float(b) / 1024.0
    cnt += 1
  return '%.2f%s'%(b,_unit_[cnt])

if __name__ == '__main__':
  interface = sys.argv[1]
  while True:
    net_data = get_net_data(interface)
    receive_data_bytes = net_data[0]
    transmit_data_bytes = net_data[8]
    os.system('clear')
    print 'Interface:%s  -> Receive Data: %s  Transmit Data: %s'%(interface, convert_bytes_to_string(receive_data_bytes), convert_bytes_to_string(transmit_data_bytes))
    time.sleep(1)
          

程序入口從 if name=='main' 處開始,首先通過參數獲取 interface ,然后調用 get_net_data() 函數獲取流量信息,接下來都是一些數據處理的過程。

總結

以上就是這篇文章的全部內容了,希望對大家的學習或者工作帶來一定的幫助,如果有疑問大家可以留言交流。


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!??!

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 吐鲁番市| 芜湖市| 城步| 青浦区| 措勤县| 大理市| 丰县| 闸北区| 汝城县| 老河口市| 崇仁县| 宣城市| 吉木乃县| 佛坪县| 莱阳市| 阿拉善盟| 广河县| 徐闻县| 石景山区| 东丰县| 广汉市| 安远县| 泽库县| 五大连池市| 漳浦县| 香格里拉县| 怀宁县| 蕲春县| 深水埗区| 霍山县| 顺昌县| 原平市| 长宁区| 黑龙江省| 津南区| 天津市| 长沙市| 嘉荫县| 唐海县| 兴文县| 顺义区|