Preface
Traffic information can be directly in the /proc/net/dev
view, the author implements the program to use the command:
Python net.py interface
Which interface
is the name of the network card, what network card to use, the computer has what network card, you can use
sudo ifconfig
To view.
Python implements the following programs:
# Coding:utf-8import SYS, time, OS ' inter-| Receive | Transmit face |bytes packets errs drop FIFO frame compressed multicast|bytes packets errs Drop FIFO colls carrier Compre ssed 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 0vmnet1: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 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_by Tes_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 (' cl Ear ') 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)
The entrance of the program if name=='main'
begins at the beginning, first through the parameter obtains interface
, then calls the get_net_data()
function obtains the traffic information, next is some data processing process.
Summarize
The above is the entire content of this article, I hope that everyone's study or work to bring certain help, if there are questions you can message exchange.