abstrait:前言流量信息可以直接在/proc/net/dev中進(jìn)行查看,筆者實(shí)現(xiàn)的程序使用命令:python net.py interface 其中interface為網(wǎng)卡名稱,使用什么網(wǎng)卡,電腦有哪些網(wǎng)卡,可以使用sudo ifconfig 進(jìn)行查看。Python實(shí)現(xiàn)的程序如下:# coding:utf-8 import sys, 
前言
流量信息可以直接在/proc/net/dev中進(jìn)行查看,筆者實(shí)現(xiàn)的程序使用命令:
python net.py interface
其中interface為網(wǎng)卡名稱,使用什么網(wǎng)卡,電腦有哪些網(wǎng)卡,可以使用
sudo ifconfig
進(jìn)行查看。
Python實(shí)現(xiàn)的程序如下:
# 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'處開(kāi)始,首先通過(guò)參數(shù)獲取interface,然后調(diào)用get_net_data()函數(shù)獲取流量信息,接下來(lái)都是一些數(shù)據(jù)處理的過(guò)程。
更多關(guān)于python如何查看系統(tǒng)網(wǎng)絡(luò)流量的信息請(qǐng)關(guān)注PHP中文網(wǎng)(ipnx.cn)其他文章!