Python subprocess 使用(一)

本文主要讲下 subprocess 的简单使用.

1: 通过subprocess 获取设备信息

import subprocess
def get_android_device_info():
# 使用adb命令获取设备信息
result = subprocess.run(['adb', 'devices', '-l'], capture_output=True, text=True)
output = result.stdout.strip() # 解析设备信息
devices = []
lines = output.split('\n')
for line in lines[1:]:
if 'device' in line:
device_info = line.split()
device = {
'serial': device_info[0],
'model': device_info[4],
'device': device_info[5],
'product': device_info[3],
'transport_id': device_info[6]
}
devices.append(device) return devices # 调用函数获取设备信息
devices = get_android_device_info()
for device in devices:
print('Serial:', device['serial'])
print('Model:', device['model'])
print('Device:', device['device'])
print('Product:', device['product'])
print('Transport ID:', device['transport_id'])
print('--')

具体的adb 执行结果如下:

List of devices attached
6b4a96b2 device usb:1-1 product:LeMax2_WW model:LEX820 device:le_x2 transport_id:43

2: 通过subprocess 获取屏幕分辨率

def get_device_physical():
command = 'adb shell wm size'
result = subprocess.check_output(command.split()).decode().strip()
resolution = result.split()[-1]
return resolution print('设备分辨率:' + str(get_device_physical()))

3: 获取设备系统日志

def capture_android_log():
# 使用adb命令获取Android系统日志
command = "adb logcat"
process = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) # 读取日志输出
while True:
line = process.stdout.readline().decode("utf-8")
if line:
print(line.strip())
else:
break # 调用函数抓取Android系统日志
capture_android_log()

Python subprocess 使用(一)的更多相关文章

  1. 【python库模块】Python subprocess模块功能与常见用法实例详解

    前言 这篇文章主要介绍了Python subprocess模块功能与常见用法,结合实例形式详细分析了subprocess模块功能.常用函数相关使用技巧. 参考 1. Python subprocess ...

  2. python subprocess popen 静默模式(不弹出console控制台)

    python subprocess popen 静默模式(不弹出console控制台) import subprocess,sys IS_WIN32 = 'win32' in str(sys.plat ...

  3. python subprocess相关操作

    python subprocess常用操作 1.subprocess模块的常用函数 函数 描述 subprocess.run() Python 3.5中新增的函数.执行指定的命令,等待命令执行完成后返 ...

  4. Python subprocess.Popen communicate() 和wait()使用上的区别

    之所以会纠结到这个问题上是因为发现在调用Popen的wait方法之后程序一直没有返回.google发现wait是有可能产生死锁的.为了把这个问题彻底弄清楚,搜索一些资料过来看看: 原文链接:http: ...

  5. python subprocess阻塞

    import select import os import subprocess import time import fcntl args = ['python','./fetch_file2.p ...

  6. python subprocess 自动运行实验室程序

    import threading, os, subprocess, time exec_path = "/home/xhz/gems/ruby/amd...../bin/tester.exe ...

  7. Python subprocess模块学习总结

    从Python 2.4开始,Python引入subprocess模块来管理子进程,以取代一些旧模块的方法:如 os.system.os.spawn*.os.popen*.popen2.*.comman ...

  8. python subprocess重定向标准输出

    subprocess.call("ping -c 1 %s" % ip,shell = True,stdout = open('/dev/null','w'),stderr = s ...

  9. Working with Python subprocess - Shells, Processes, Streams, Pipes, Redirects

    Posted: 2009-04-28 15:20 Tags: Python Note Much of the "What Happens When you Execute a Command ...

  10. 【转】 Python subprocess模块学习总结

    从Python 2.4开始,Python引入subprocess模块来管理子进程,以取代一些旧模块的方法:如 os.system.os.spawn*.os.popen*.popen2.*.comman ...

随机推荐

  1. phpstudy搭建虚拟域名

    phpstudy搭建虚拟域名 先使用phpstudy搭建好对应的环境 打开phpstudy控制面板,点击 其他选项菜单,选择 站点域名管理 输入网站虚拟域名,比如我这里DVWA靶场,我就写 www.d ...

  2. Java并发Map的面试指南:线程安全数据结构的奥秘

    简介 在计算机软件开发的世界里,多线程编程是一个重要且令人兴奋的领域.然而,与其引人入胜的潜力相伴而来的是复杂性和挑战,其中之一就是处理共享数据.当多个线程同时访问和修改共享数据时,很容易出现各种问题 ...

  3. SpringCloud搭建保姆级教程

    一.搭建服务注册与发现中⼼ 使⽤Spring Cloud Netflix 中的 Eureka 搭建服务注册与发现中⼼ 1.创建SpringBoot应用添加依赖 1.spring web 2.eurek ...

  4. Solution -「YunoOI 2017」由乃的 OJ

    Description Link. 起床困难综合症 上树. Solution 线段树维护,树剖上树. 具体题解有空再写,我要去睡觉了. #include<bits/stdc++.h> ty ...

  5. 如何vue3中使用全局变量,与Vue2的区别

    对比: 在vue2.x中我们挂载全局变量或方法是通过是使用Vue.prototype.$xxxx=xxx的形式来挂载,然后通过this.$xxx来获取挂载到全局的变量或者方法 但是 在vue3.x中显 ...

  6. C#学习笔记--变量类型的转换

    变量类型的转化: 转换原则 同类型的大的可以装小的,小类型的装大的就需要强制转换. 隐式转换: 同种类型的转换: //有符号 long-->int-->short-->sbyte l ...

  7. PPT太大发不出去?教你三个PPT压缩方法,200M的PPT变15M

    相信有很多小伙伴在工作的时候,都会制作不少的PPT,而我们也知道很多PPT在制作完成以后,体积就会变得非常大,在发送给别人的时候总是会受到限制,是有点难搞了. 别担心,今天小编将告诉大家三个简单的方法 ...

  8. the solution of Mining Your Own Business

    the description of problem (我看的是 PDF 里面的原题所以这里描述会和题目不一样,但是大意一致) 给定一个未必连通的无向图,问最少在几个点设置出口,可以保证任意一个点坍塌 ...

  9. [WUSTCTF 2020]朴实无华

    打开网页,就显示一个Hack me ,查看源码也是啥也没有,就用御剑扫一下 发现存在robots.txt文件 根据提示,打开/fAke_f1agggg.php 还真就一个错误的flag 仔细看了看,居 ...

  10. 【createWrapper】根据条件类创建查询wrapper

    前几天写一个有几十个字段的查询wrapper,写得我心烦意乱.然后就琢磨了一下能不能只传一个条件类对像就能创建对应的wrapper.去看了下mybatis-plus的文档没看到合适的api,有一个创建 ...