当前位置: 首页 > news >正文

python+adb

#!/usr/bin/python env
# -*- coding: utf-8 -*-
import os
import sys
import subprocess
from time import sleepimport logging
logging.basicConfig(level=logging.DEBUG)
class ScreenCapture():def get_screen_size(self):"""获取手机分辨率"""action = subprocess.Popen(['adb', 'shell', 'wm', 'size'], stdout=subprocess.PIPE)result = action.stdout.read()_, size = result.decode().split(':')self.width,self.height = size.strip().split('x')print(f'获取到手机分辨率为:{self.width},{self.height}')  # 1080,2436# return int(width), int(height)# print(f"result是{result}")# print(result.decode())# ss=result.decode().split(':')# aaa=ss[1].strip()self.x1=int(self.width)/2self.x2=int(self.width)/2self.y1=int(self.height)/4self.y2=int(self.height)/1.5print(f"x1是{self.x1},x2是{self.x2},y1是{self.y1},y2是{self.y2}")@staticmethoddef show_menu():print("========================================================== ")print("||      <1>远程连接设备       <2>向左滑动       <3>向右滑动    ||\n")print("||      <4>向上滑动           <5>向下滑动       <6>跑Monkey    ||\n")print("||      <7>单场景chrome测试   <8>               <9>          ||\n")print(" ========================================================= \n")# subprocess.Popen(#     ['adb', 'shell', 'input', 'swipe', str(self.x1), str(self.y1), str(self.x1), str(self.y2), '500'],#     stdout=subprocess.PIPE)# return int(width), int(height)# self.ScreenCapture(y1, y2, height)# subprocess.Popen(['adb', 'shell', 'input', 'swipe', str(self.x1), str(self.y1), str(self.x1), str(self.y2), '500'],stdout=subprocess.PIPE)def up_swipe(self):up_num = int(input("向上滑动多少次,用数字替代:"))interval_time = int(input("输入间隔的时间:"))for i in range(up_num):subprocess.Popen(['adb', 'shell', 'input', 'swipe', str(self.x1), str(self.y1), str(self.x1), str(self.y2), '500'],stdout=subprocess.PIPE)print("第{}次滑动".format(i + 1))sleep(interval_time)print("滑动结束,退出。。")sys.exit()def down_swipe(self):up_num = int(input("向下滑动多少次,用数字替代:"))interval_time=int(input("输入间隔的时间:"))for i in range(up_num):subprocess.Popen(['adb', '-s',{self.ip_input},'shell', 'input', 'swipe', str(self.x1), str(self.y2), str(self.x1), str(self.y1), '500'],stdout=subprocess.PIPE)sleep(interval_time)print("滑动结束,退出。。")sys.exit()def left_swipe(self):x1=str(int(self.width)*0.8)x2=str(int(self.width)*0.2)y1=str(int(self.height)*0.5)print(x1)print("滑动开始前")up_num = int(input("向左滑动多少次,用数字替代:"))for i in range(up_num):subprocess.Popen(['adb', 'shell', 'input', 'swipe', x2, y1, x1, y1, '500'],stdout=subprocess.PIPE)print("第{}次滑动".format(i+1))sleep(3)print("滑动结束,退出。。")sys.exit()def right_swipe(self):x1 = str(int(self.width) * 0.8)x2 = str(int(self.width) * 0.2)y1 = str(int(self.height) * 0.5)print(x1)print("滑动开始前")up_num = int(input("向左滑动多少次,用数字替代:"))for i in range(up_num):subprocess.Popen(['adb', 'shell', 'input', 'swipe', x1, y1, x2, y1, '300'],stdout=subprocess.PIPE)print("第{}次滑动".format(i + 1))sleep(3)print("滑动结束,退出。。")sys.exit()def get_device_id(self):self.adb_devices=os.popen("adb devices").read()self.adb_output = self.adb_devices.split("List of devices attached\n", 1)[1]lines = self.adb_output.strip().split('\n')# 遍历每一行,提取设备号self.device_ids = []for line in lines:# 如果行不是空的,并且包含"device",则提取设备号if line and "device" in line:device_id, _ = line.split(maxsplit=1)  # maxsplit=1确保只分割一次self.device_ids.append(device_id.strip())self.device_ids=self.device_ids[0]logging.info(f'device_ids号是: {self.device_ids}')return self.device_idsdef adb_connect(self, device_ip):adb_command = ['adb', 'connect', f'{device_ip}:5555']try:subprocess.Popen("adb tcpip 5555", stdout=subprocess.PIPE)# subprocess.check_call("adb -s {} tcp ip 5555", stdout=subprocess.PIPE)  #output = subprocess.check_output(adb_command, shell=True, stderr=subprocess.STDOUT)print(f"Connected to device at {device_ip}:5555")print(output.decode())except:print(f"Failed to connect to device at {device_ip}:5555", "请检查错误")sys.exit()def get_and_connect_ip(self):# 将原本在if num == 1下的代码移到这个方法中adb deip_command = f'adb -s {self.get_device_id()} shell ip route'result = subprocess.check_output(ip_command, shell=True, text=True)# 提取 IP 地址(这里假设IP地址是route命令输出的最后一行最后一个元素)ip_address = result.strip('\n').strip(' ').split(' ')[-1]print(f"Detected IP address: {ip_address},远程连接需拔掉USB设备")self.ip_input = input("输入你的IP地址(或者按回车使用检测到的IP):")if not self.ip_input:self.ip_input = ip_address  # 如果用户没有输入,则使用检测到的IPself.adb_connect(self.ip_input)self.run() # 连接后重新显示菜单def chrome_switch(self):os.system("adb devices")while True:os.system(f'adb -s {self.ip_input} shell am start -a android.intent.action.VIEW -d "http://baidu.com"')sleep(5)for i in range(20):subprocess.Popen(['adb', '-s',self.ip_input,'shell', 'input', 'swipe', str(self.x1), str(self.y2),str(self.x1), str(self.y1), '500'],stdout=subprocess.PIPE)sleep(3)os.system(f'adb -s {self.ip_input} shell am start -a android.intent.action.VIEW -d "http://163.com"')sleep(5)for i in range(20):subprocess.Popen(['adb', '-s', self.ip_input, 'shell', 'input', 'swipe', str(self.x1), str(self.y2),str(self.x1),str(self.y1), '500'],stdout=subprocess.PIPE)sleep(3)#def adb_connect(self, ip_input):# 假设此方法用于连接ADB#print(f"Connecting to ADB with IP: {ip_input}")# ... 省略实际连接ADB的代码def run(self):ScreenCapture.show_menu()  # 假设ScreenCapture是另一个模块或类,并且有show_menu方法num = int(input("输入你要选择的功能,用数字替代:"))# 定义动作字典action_mapping = {1: self.get_and_connect_ip,2: self.left_swipe,3: self.right_swipe,4: self.up_swipe,5: self.down_swipe,7: self.chrome_switch,}# 检查输入是否在映射中if num in action_mapping:action_mapping[num]()elif num == 6:# 对于特定的num值,可以直接处理passelse:print("\n\t输入有误!!!\t\n")self.run()  # 递归调用,重新显示菜单和接收输入if __name__ == '__main__':ScreenCapture().run()

相关文章:

  • 北京网站建设多少钱?
  • 辽宁网页制作哪家好_网站建设
  • 高端品牌网站建设_汉中网站制作
  • PHP即刻送达同城派送小程序系统
  • 数据分析-螺旋环状气泡图
  • 针对SVM算法初步研究
  • 【TabBar嵌套Navigation案例-复习昨天的内容-预习今天的内容 Objective-C语言】
  • 【数据结构】线性表的定义和基本操作
  • Java零基础-charAt(int index)详解
  • 从零实现诗词GPT大模型:实现多头自注意力
  • 如何使用命令行快速下载Google Drive/OneDrive大文件
  • 【时间盒子】-【6.任务页面】在同一个页面新建、编辑任务
  • 阳极氧化废酸处理
  • 陶建辉演讲干货分享,AI 时代下的数据预测和数据处理挑战
  • PL/SQL 继承Oracle Database 的可靠性、安全性和可移植性
  • Note24091201_组态王画面按钮静音设置
  • springboot教学检查管理系统---附源码77417
  • 12_持久化数据结构
  • [iOS]Core Data浅析一 -- 启用Core Data
  • [微信小程序] 使用ES6特性Class后出现编译异常
  • [译]Python中的类属性与实例属性的区别
  • 【108天】Java——《Head First Java》笔记(第1-4章)
  • 【译】React性能工程(下) -- 深入研究React性能调试
  • 【跃迁之路】【444天】程序员高效学习方法论探索系列(实验阶段201-2018.04.25)...
  • 230. Kth Smallest Element in a BST
  • Cookie 在前端中的实践
  • Java 内存分配及垃圾回收机制初探
  • Java多态
  • Java反射-动态类加载和重新加载
  • Making An Indicator With Pure CSS
  • Meteor的表单提交:Form
  • MySQL QA
  • Node 版本管理
  • Python中eval与exec的使用及区别
  • React as a UI Runtime(五、列表)
  • Spark RDD学习: aggregate函数
  • 1.Ext JS 建立web开发工程
  • ​Spring Boot 分片上传文件
  • # 执行时间 统计mysql_一文说尽 MySQL 优化原理
  • (1)bark-ml
  • (1/2)敏捷实践指南 Agile Practice Guide ([美] Project Management institute 著)
  • (14)Hive调优——合并小文件
  • (2)空速传感器
  • (java版)排序算法----【冒泡,选择,插入,希尔,快速排序,归并排序,基数排序】超详细~~
  • (超简单)使用vuepress搭建自己的博客并部署到github pages上
  • (附源码)计算机毕业设计ssm基于Internet快递柜管理系统
  • (三)mysql_MYSQL(三)
  • (四)搭建容器云管理平台笔记—安装ETCD(不使用证书)
  • (限时免费)震惊!流落人间的haproxy宝典被找到了!一切玄妙尽在此处!
  • (一)ClickHouse 中的 `MaterializedMySQL` 数据库引擎的使用方法、设置、特性和限制。
  • (一)使用IDEA创建Maven项目和Maven使用入门(配图详解)
  • (转) SpringBoot:使用spring-boot-devtools进行热部署以及不生效的问题解决
  • (转)shell调试方法
  • (转)原始图像数据和PDF中的图像数据
  • .bashrc在哪里,alias妙用
  • .NET LINQ 通常分 Syntax Query 和Syntax Method
  • .net redis定时_一场由fork引发的超时,让我们重新探讨了Redis的抖动问题
  • .net 无限分类