您的位置:首页 > 编程语言 > Python开发

python脚本获取当前Android流量

2017-09-13 15:38 369 查看
import time
#use to control time
import subprocess
import  os
from color import *

#############################
#这里要进入自己adb目录。每个人根据自己的adb 目录设置
#      注意用的是反斜杠
############################
adb_path='C:/Users/lvbiao/Desktop/adb'

os.chdir(adb_path)

Uid_cmd = 'adb shell "dumpsys package com.unisound.ant.platform.hardware  | grep userId"'

def getUid( ):#获取UID
p1 = subprocess.Popen(Uid_cmd, shell=True, stdout=subprocess.PIPE);
uidLongString = p1.stdout.read()
uid_info = uidLongString.decode('UTF-8','strict')
uidLongList = uid_info.split()
uidMap = uidLongList[0]
#print ("uidmap====" + uidMap )
uid= uidMap.split("=")
#print("uid1===" + uid[1])
uid =uid[1]
#print ("str is = " + uid )
return uid

Rev_cmd = 'adb shell "cd proc && cd uid_stat && cd '+ getUid( ) +' && cat tcp_rcv"'
def getRev():#获取某个APP的TCP下载流量
p_rev = subprocess.Popen(Rev_cmd, shell=True, stdout=subprocess.PIPE);
flo_rec =int(p_rev.stdout.read())
#print ("flo_rec" + str(flo_rec))
clr.print_red_text("com.unisound.ant.platform.hardware recv Bytes ")
clr.print_red_text("RECV BYTES is =" + str(flo_rec))
clr.print_red_text("========end===========")
return flo_rec
SEN_cmd = 'adb shell "cd proc && cd uid_stat && cd '+ getUid( ) +' && cat tcp_snd"'
def getSEN():#获取某个APP的TCP下载流量
p_rev = subprocess.Popen(SEN_cmd, shell=True, stdout=subprocess.PIPE);
flo_sen =int(p_rev.stdout.read())
#print ("flo_sen" + str(flo_sen))
clr.print_blue_text("========================")
clr.print_blue_text("com.unisound.ant.platform.hardware SEND Bytes ")
clr.print_blue_text("SEND BYTES is =" + str(flo_sen))
clr.print_blue_text("========end===========")
return flo_sen
clr = Color()
while 1:
getUid( )
time.sleep(1)
getRev()
time.sleep(1)
getSEN()
time.sleep(1)


实时监控系统收发的系统流量。监控的好工具
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: