python执行adb命令_android – 如何使用Python执行adb命令?

我想创建一个可用于执行Android adb命令的python脚本.

我看了https://github.com/rbrady/python-adb,但似乎无法让它完美运作.

有什么建议?

解决方法:

我不得不修改一些函数让它在Python 2.7上运行并使用子进程代替.这里修改了我的版本代码:

def __build_command__(self,cmd):

if self.__devices is not None and len(self.__devices) > 1 and self.__target is None:

self.__error = "Must set target device first"

return None

if type(cmd) is tuple:

a = list(cmd)

elif type(cmd) is list:

a = cmd

else:

a = [cmd]

a.insert(0, self.__adb_path)

if self.__target is not None:

a.insert(1, ['-s', self.__target])

return a

def run_cmd(self, cmd):

"""

Run a command against adb tool ($adb )

"""

self.__clean__()

if self.__adb_path is None:

self.__error = "ADB path not set"

return

try:

args = self.__build_command__(cmd)

if args is None:

return

# print 'args>', args

cmdp = subprocess.Popen(args, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)

self.__output, self.__error = cmdp.communicate()

retcode = cmdp.wait()

# print 'stdout>', self.__output

# print 'stderr>', self.__error

if retcode < 0:

print >>sys.stderr, "Child was terminated by signal", -retcode

else:

return

except OSError, e:

self.__error = str(e)

return

标签:python,android

来源: https://codeday.me/bug/20190704/1375790.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值