网络编程
位置:首页>> 网络编程>> Python编程>> Python调用adb命令实现对多台设备同时进行reboot的方法

Python调用adb命令实现对多台设备同时进行reboot的方法

作者:老猪新路  发布时间:2022-08-06 02:40:45 

标签:Python,adb,reboot

首先,adb实现对设备的reboot命令是:adb reboot . 但是如果是两台/多台设备的时候,需要声明serial number: adb -s serial_no reboot.

那么,如何用python实现对多台设备进行adb操作呢(reboot)?

这里涉及到 python 下 subprocess model的使用:


import subprocess

adb device 获取所有设备的 serial number:


devices = subprocess.Popen(
'adb devices'.split(),
stdout=subprocess.PIPE,
stderr=subprocess.PIPE
).communicate()[0]

这样adb device命令的返回信息都在devices下,但是我们只需要 serial number的:


serial_nos = []
for item in devices.split():
filters = ['list', 'of', 'device', 'devices', 'attached']
if item.lower() not in filters:
 serial_nos.append(item)

这样serial_nos 下保存的就是所有设备的 serial number 了,下面我们只需要依次对其进行adb -s [serial_number] reboot即可:


for serial_no in serial_nos:
reboot_cmds.append('adb -s %s reboot' % serial_no)
for reboot_cmd in reboot_cmds:
subprocess.Popen(
 reboot_cmd.split(),
 stdout=subprocess.PIPE,
 stderr=subprocess.PIPE
).communicate()[0]

这样,每个设备都进行了reboot的操作了……

来源:https://blog.csdn.net/laozhuxinlu/article/details/71727886

0
投稿

猜你喜欢

手机版 网络编程 asp之家 www.aspxhome.com