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()
  • 相关阅读:
    loadRunner运行场景时,事务数为0或是只显示添加的事务的数
    loadrunner12自带的机票预订服务,解决httpd: Could not reliably determine the server's fully qualified domain name 问题
    Loadrunner12的下载和安装
    【AI测试】人工智能 (AI) 测试--第二篇
    【AI测试】人工智能 (AI) 测试--开篇
    CVAT 用户指南
    List of devices attached 没有手机设备号 解决办法
    Pip安装Django超时(time out)解决方法
    ipsec_笔记
    cisco 2600(转)
  • 原文地址:https://www.cnblogs.com/AlexBai326/p/5955325.html
Copyright © 2011-2022 走看看