zoukankan      html  css  js  c++  java
  • 【python调用windows CLI】调用adb统计Android app的流量消耗

    主要记录python如何调用windows CLI

    手机连接PC,adb devices可以看到手机sn

    通过adb 获取指定app的processID UID

    读取Android  /proc/net/xt_qtaguid/stats 获取指定列,格式化输出,用于后续统计app消耗的流量

    #coding=utf-8 
    import subprocess
    import time
    fo = open(r"D:foo.txt", "w")
    #获取进程ID
    getProcessIdcmd = 'adb shell ps | grep appname'
    p = subprocess.Popen(getProcessIdcmd, shell=False, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
    content = p.stdout.readlines()
    if len(content) == 1:
        processId = content[0].split()[1]
    else:
        print "not get processID"
    #获取进程对应的UID
    getUidcmd = 'adb shell cat /proc/' + processId + '/status | grep Uid'
    
    p = subprocess.Popen(getUidcmd, shell=False, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
    content = p.stdout.readlines()
    uidList = content[0].strip().split('	')
    print uidList
    uid = uidList[1]
    
    #获取UID对应的Traffic
    getTrafficcmd = 'adb shell cat /proc/net/xt_qtaguid/stats | grep ' + uid
    
    for i in range(10000):
        currentTime =  time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
        traffic_initial = [0]*16
        traffic_prefix = [] 
        p = subprocess.Popen(getTrafficcmd, shell=False, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)    
        for line in p.stdout.readlines():    
            ll = line.strip()
            ll2=ll.replace(' ',',')
            ll2_list=ll2.split(',')
            traffic_list = ll2_list[5:]
            traffic_prefix = ll2_list[0:4]
            traffic_list_int = [int(e) for e in traffic_list]
            
            traffic_initial = [x+y for x, y in zip(traffic_initial, traffic_list_int)]
            #print traffic_list
            print currentTime + "," + ll2
        retval = p.wait()
        print traffic_initial
        traffic_list_str = [str(e) for e in traffic_initial]
        print traffic_prefix + traffic_list_str
        traffic = ','.join(traffic_prefix + traffic_list_str)
        print currentTime +','+ traffic
        fo.write(currentTime +','+ traffic + '
    ')
        time.sleep(60)
        print '--------------'
    fo.close()
  • 相关阅读:
    bzoj 4012: [HNOI2015]开店
    POJ 1054 The Troublesome Frog
    POJ 3171 Cleaning Shifts
    POJ 3411 Paid Roads
    POJ 3045 Cow Acrobats
    POJ 1742 Coins
    POJ 3181 Dollar Dayz
    POJ 3040 Allowance
    POJ 3666 Making the Grade
    洛谷 P3657 [USACO17FEB]Why Did the Cow Cross the Road II P
  • 原文地址:https://www.cnblogs.com/AlexBai326/p/5955325.html
Copyright © 2011-2022 走看看