python如何查看系統(tǒng)網(wǎng)絡(luò)流量的信息
前言
流量信息可以直接在/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'處開始,首先通過參數(shù)獲取interface,然后調(diào)用get_net_data()函數(shù)獲取流量信息,接下來都是一些數(shù)據(jù)處理的過程。
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望對大家的學(xué)習(xí)或者工作帶來一定的幫助,如果有疑問大家可以留言交流。
- python實(shí)現(xiàn)域名系統(tǒng)(DNS)正向查詢的方法
- python實(shí)現(xiàn)DNS正向查詢、反向查詢的例子
- Python中使用scapy模擬數(shù)據(jù)包實(shí)現(xiàn)arp攻擊、dns放大攻擊例子
- python腳本實(shí)現(xiàn)分析dns日志并對受訪域名排行
- Python寫的一個簡單DNS服務(wù)器實(shí)例
- python實(shí)現(xiàn)dnspod自動更新dns解析的方法
- python使用在線API查詢IP對應(yīng)的地理位置信息實(shí)例
- python操作mongodb根據(jù)_id查詢數(shù)據(jù)的實(shí)現(xiàn)方法
- Python使用淘寶API查詢IP歸屬地功能分享
- Python實(shí)現(xiàn)的簡單dns查詢功能示例
相關(guān)文章
Python中eval()函數(shù)的詳細(xì)使用教程
eval函數(shù)在Python中具有非常重要的地位,熟練的使用eval函數(shù)能夠?yàn)槲覀兊腜ython編程提供很多的便利之處,下面這篇文章主要給大家介紹了關(guān)于Python中eval()函數(shù)的詳細(xì)使用,需要的朋友可以參考下2022-07-07
pandas string轉(zhuǎn)dataframe的方法
下面小編就為大家分享一篇pandas string轉(zhuǎn)dataframe的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-04-04
python實(shí)現(xiàn)12306登錄并保存cookie的方法示例
這篇文章主要介紹了 python實(shí)現(xiàn)12306登錄并保存cookie的方法示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12
使用python讀取csv文件快速插入數(shù)據(jù)庫的實(shí)例
今天小編就為大家分享一篇使用python讀取csv文件快速插入數(shù)據(jù)庫的實(shí)例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-06-06
python+Tesseract OCR實(shí)現(xiàn)截屏識別文字
pytesseract Python常用pytesseract進(jìn)行圖片上的文字識別,本文主要介紹了python+Tesseract?OCR實(shí)現(xiàn)截屏識別文字,具有一定的參考價值,感興趣的可以了解一下2023-11-11
Python中Dataframe數(shù)據(jù)排序方法(含實(shí)例講解)
在進(jìn)行數(shù)據(jù)分析操作時,經(jīng)常需要對數(shù)據(jù)按照某行某列排序,或者按照多行多列排序,以及按照索引值排序等等,下面這篇文章主要給大家介紹了關(guān)于Python中Dataframe數(shù)據(jù)排序方法的相關(guān)資料,需要的朋友可以參考下2023-02-02
jupyter notebook 使用過程中python莫名崩潰的原因及解決方式
這篇文章主要介紹了jupyter notebook 使用過程中python莫名崩潰的原因及解決方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-04-04

