Objective
Flow information can be directly in the /proc/net/dev
view, the author implemented the program to use the command:
Which interface
is the name of the network card, what network card to use, the computer has which network cards, you can use
For viewing.
The program that Python implements is as follows:
# 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 Compr essed 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 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 Ope N ('/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_b Ytes = 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)
The program entrance if name=='main'
starts at the beginning, obtains by interface
the parameter first, then calls the get_net_data()
function to obtain the flow information, next all is some data processing process.
Summarize
The above is the entire content of this article, I hope that the study or work to bring some help, if you have questions you can message exchange.