python编写adb截图工具的实现源码

adb截图工具可用于Android手机及Android终端,Android端或者Android终端的远程截图至本地电脑中,今天通过本文给大家介绍python编写adb截图工具的实现源码,感兴趣的朋友一起看看吧

目录

一、 功能

二、使用说明

三、实现

1.初始源码

2.优化:增加ip连接断开重连处理

一、 功能

Android端或者Android终端的远程截图至本地电脑中

二、使用说明

1.adb截图工具可用于Android手机及Android终端

2.使用数据线连接前提:电脑与Android终端/手机已连接

Android终端/手机已打开开发者模式

3.生成的图片格式为png

三、实现

1.初始源码

import time import os,sys #截图 def screencap_cmd(filePath,devices=None): if filePath==None: filePath='D:/png/' if not os.path.exists(filePath): os.makedirs(filePath) filename=time.strftime("%Y%m%d%H%M%S",time.localtime())+".png" if devices==None: cmd_screencap='adb shell screencap -p sdcard/'+filename cmd_pull='adb pull sdcard/'+filename+' '+filePath+filename cmd_delete='adb shell rm sdcard/'+filename else: cmd_screencap='adb -s '+devices+' shell screencap -p sdcard/'+filename cmd_pull='adb -s '+devices+' pull sdcard/'+filename+' '+filePath+filename cmd_delete='adb -s '+devices+' shell rm sdcard/'+filename os.system(cmd_screencap) os.system(cmd_pull) os.system(cmd_delete) #保存地址判断及设备信息分类调用 def screencap_info(devices=None,filePath=None): if filePath==None: filePath='D:/png/' if not os.path.exists(filePath): os.makedirs(filePath) if devices==None: screencap_cmd(filePath) adb_device_none(filePath) else: screencap_cmd(filePath,devices) adb_info(devices,filePath) #更换设备 def change_devices(): r=os.popen("adb devices") lines=r.readlines() lines=lines[1:-1] n=len(lines) for i in lines: print(i.split("t")[0]) devices=input("请输入指定设备:n") for i in lines: if devices in i: return devices print("未找到改设备请重新输入!") change_devices() #截图命令判断,电脑连接多个设备或使用IP截图时 def adb_info(lines,filePath=None): s=input("是否截图(T/F/N)?n") if s=='t' or s=='T': if filePath==None: screencap_info(lines) else: screencap_info(lines,filePath) elif s=='N' or s=='n' or s=='exit': sys.exit() elif s=='F' or s=='f': devices=change_devices() if filePath==None: adb_info(devices) else: adb_info(devices,filePath) #截图命令判断,电脑连接仅连接一个设备时 def adb_device_none(filePath=None): s=input("是否截图(T/F/N)?n") if s=='t' or s=='T': if filePath==None: screencap_info() else: screencap_info(None,filePath) elif s=='N' or s=='n' or s=='exit': sys.exit() elif s=='F' or s=='f': devices=change_devices() if filePath==None: adb_info(devices) else: adb_info(devices,filePath) #使用设备判断 def adb_devices(n,lines,filePath=None): if n==0: print("请检查是否已接连或启动调试模式或安装adb环境") s=input("再次启动(T/F)?n") if s=='t' or s=='T': r=os.popen("adb devices") lines=r.readlines() lines=lines[1:-1] n=len(lines) adb_devices(n,r.readlines()) else: sys.exit() elif ":5555" in lines: print("T:截图 F:更换设备 exit|N:退出 ") if filePath==None: adb_info(lines) else: adb_info(lines,filePath) elif n==1: print("T:截图 F:更换设备 exit|N:退出") if filePath==None: adb_device_none() else: adb_device_none(filePath) elif n>1: for i in lines: print(i.split("t")[0]) devices=input("请输入指定设备:n") for i in lines: if devices in i: print("T:截图 F:更换设备 exit|N:退出") if filePath==None: adb_info(devices) else: adb_info(devices,filePath) print("未找到改设备请重新输入!") if filePath==None: adb_devices(n,lines) else: adb_devices(n,lines,filePath) #输入IP def ip_info(): ipPath=input("请输入IP:(不输入enter跳过默认使用数据线连接)n") if len(ipPath)>0: try: cnd='adb connect '+ipPath os.system(cnd) return ipPath except: print("ADB调试模式为USB,需要切换到无线调试模式n") print("切换方法:n第一步:Android设备开启USB调试,并且通过USB线连接到电脑n第二步:在终端执行以下命令”adb tcpip 5555“n第三步:在终端执行以下命令”adb connect ip“。此时拔出USB线,应该就可以adb通过wifi调试设备n") return ip_info() if __name__ == '__main__': ipPath=ip_info() filePath=input("请输入保存地址:(不输入enter跳过,默认保存至D:png)n") #查询当前连接的设备 r=os.popen("adb devices") lines=r.readlines() lines=lines[1:-1] n=len(lines) ip_add=0 if ipPath!=None: for i in lines: if ipPath in i: ip_add=i.split("t")[0] if len(filePath)>0: if ":\" in filePath: if ip_add!=0: adb_devices(n,ip_add,filePath) else: adb_devices(n,lines,filePath) else: print("地址输入非法,使用默认地址n") if ip_add!=0: adb_devices(n,ip_add) else: adb_devices(n,lines) else: if ip_add!=0: adb_devices(n,ip_add) else: adb_devices(n,lines)

2.优化:增加ip连接断开重连处理

在输入ip函数ip_info()增加ip连接判断,将os.system替换成os.popen函数执行cmd命令:

r=os.popen(cnd) #读取执行结果 lines=r.readlines()[0] if 'unable' in lines: print("连接失败n") return ip_info() else: return ipPath

os.system函数无法获取执行结果,只能获取执行成功或失败的结果,成功返回0,失败返回1,且会执行的结果打印在终端上,相当于调起dos执行命令

os.popen函数可获取执行的结果内容,但获取的结果要进行readl

上一篇:Appium+Python自动化环境搭建实例教程下一篇:一篇文章教会你PYcharm的用法 热门搜索:

截图工具 

java代码实现截图 

编码实现 

python简单代码编写 

自带截图工具 

相关文章

python编写adb截图工具的实现源码

2021-09-10阅读(4112)评论(0)推荐()

adb截图工具可用于Android手机及Android终端,Android端或者Android终端的远程截图至本地电脑中,今天通过本文给大家介绍python编写...

如何使用python写截屏小工具

2021-11-06阅读(6036)评论(0)推荐()

这篇文章主要介绍了如何使用python写截屏小工具,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

Python编写屏幕截图程序方法

2021-11-04阅读(7272)评论(0)推荐()

这篇文章主要介绍了Python编写屏幕截图程序方法,本文讲解使用开源程序pywin32实现屏幕截图和读取剪切板功能,需要的朋友可以参考下

Ubuntu截图工具gnome-screenshot使用教程

2021-09-10阅读(4185)评论(0)推荐()

Ubuntu自带的截图工具非常好用,可以自已设置各种截图的快捷键,下面我们一起来了解一下这个截图工具gnome-screenshot

Ubuntu 18.04中截图工具shutter的编辑按钮不可用的解决办法

2021-11-03阅读(8822)评论(0)推荐()

Shutter是一个由第三方提供的在Ubuntu上运行的截图工具。这篇文章主要介绍了Ubuntu 18.04中截图工具shutter的编辑按钮不可用的解决办法及...

python如何编写类似nmap的扫描工具

2021-10-13阅读(7084)评论(0)推荐()

这篇文章主要介绍了python如何编写类似nmap的扫描工具,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

重装win7系统后打开截图工具显示“截图工具当前未在计算机上运行”如何解决

2021-09-18阅读(6612)评论(0)推荐()

重装win7系统后打开截图工具显示“截图工具当前未在计算机上运行”怎么办,小编总结了一些使用方法,感兴趣的小伙伴们可以参考一下

取消

有人回复时邮件通知我

提交评论

相关文章

Python中的函数(二) 在上一篇文章中提到了Python中函数的定...
Python中的字符串 可能大多数人在学习C语言的时候,最先接触...
Python 面向对象编程(一) 虽然Python是解释性语言,但是它...
Python面向对象编程(二) 在前面一篇文章中谈到了类的基本定...
Python中的函数(一) 接触过C语言的朋友对函数这个词肯定非...
在windows下如何快速搭建web.py开发框架 用Python进行web开发...