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

yipeiwu_com6年前Python基础

首先,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的操作了……

以上这篇Python调用adb命令实现对多台设备同时进行reboot的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

Python使用QQ邮箱发送邮件报错smtplib.SMTPAuthenticationError

Python使用QQ邮箱发送邮件报错smtplib.SMTPAuthenticationError

最新在学习Python的基础入门系列课程,今天学习到使用python 的内置库smtplib发送邮件内容。 使用Python发送邮件步骤简单: 创建SMTP连接 使用邮箱和密码...

python实现指定字符串补全空格、前面填充0的方法

Python zfill()方法返回指定长度的字符串,原字符串右对齐,前面填充0。 zfill()方法语法:str.zfill(width) 参数width -- 指定字符串的长度。原字...

Python实现一个简单的MySQL类

本文实例讲述了Python实现一个简单的MySQL类。分享给大家供大家参考。 具体实现方法如下: 复制代码 代码如下:#!/usr/bin/env python # -*- coding...

浅谈Python数据类型之间的转换

Python数据类型之间的转换 函数 描述 int(x [,base])...

Python简单读写Xls格式文档的方法示例

Python简单读写Xls格式文档的方法示例

本文实例讲述了Python简单读写Xls格式文档的方法。分享给大家供大家参考,具体如下: 1. 模块安装 使用pip install命令安装, 即: pip install xlrd...