【自动化测试】【安卓android】python 发送adb命令方法

#command 命令列表,可以传入任意命令,类型为list
#cmdMode可以选择发送命令方式为直接发送adb 命令还是先进入shell
    def sendAdbcmd(command, deviceID="", cmdMode="cmd", sleepTime=1,timeout=20,returnType="string"):
        res = ""
        contextlist = []
        try:
            if cmdMode == 'cmd':
                # 依次执行命令
                cmdFinal = ""
                for cmdComand in command:
                    cmdFinal = cmdFinal + cmdComand + '&'
                f = os.popen(cmdFinal)
                #print("发送命令为:%s" % cmdFinal)
                if sleepTime != 0:
                    sleep(sleepTime)
                contextlist = f.readlines()
                if contextlist != []:
                    pass
                    #print("打印命令返回结果%s" % str(contextlist))
                f.close()
            elif cmdMode == 'shell':
                if "exit" not in command:
                    command.append("exit")
                    command.append("exit")
                obj = subprocess.Popen('adb -s %s shell' % deviceID, stdin=subprocess.PIPE,
                                       stdout=subprocess.PIPE,
                                       stderr=subprocess.PIPE, shell=True)
                context, syserr = obj.communicate(("\n".join(command) + "\n").encode(),timeout=timeout)
                contextlist = str(context).split('\\r')
                if contextlist != []:
                    pass
                    #print("打印命令返回结果%s" % contextlist)

            if returnType == "list":
                return contextlist
            else:
                for con in contextlist:
                    for c in command:
                        if c in con:
                            try:
                                contextlist.remove(con)
                            except:
                                pass

                for con in contextlist:
                    if con != "":
                        res = res + con.lstrip("'b").replace('\r', "").replace('\n', "").strip(" ")

        except Exception as e:
            logging.info("adb 命令发送失败,失败原因%s" % e)
            return res
        else:
            return res

  • 0
    点赞
  • 0
    收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

奔波儿灞8708

你的鼓励将是我创作的最大动力

¥2 ¥4 ¥6 ¥10 ¥20
输入1-500的整数
余额支付 (余额:-- )
扫码支付
扫码支付:¥2
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值