Python使用adb远程控制android设备

发布时间:2025-01-02 18:49

科技实用:如何在Windows电脑上远程控制Android设备 #生活技巧# #数码产品使用技巧# #科技资讯与评测#

本地基本操作

# -*- coding: utf-8 -*- import os import sys import config def control(argv): devices = config.devices if argv[1] == "1": for device in devices: try: result = os.popen("adb connect "+device[0]).read() if 'unable' in result: print u"%s-%s连接失败"%(device[0],device[1]) elif 'connected' in result: print u"%s-%s连接成功"%(device[0],device[1]) except: print u"%s-%s连接失败"%(device[0],device[1]) elif argv[1] == "2": for device in devices: try: print os.popen("adb disconnect "+device[3]).read() except: print u"%s-%s连接失败"%(device[0],device[1]) elif argv[1] == "3": for device in devices: try: os.popen("adb -s " + device[0] + " shell input keyevent 164") except: print u"%s-%s连接失败"%(device[0],device[1]) elif argv[1] == "4": for device in devices: try: os.popen("adb -s " + device[0] + " shell input keyevent 24") except: print u"%s-%s连接失败"%(device[0],device[1]) elif argv[1] == "5": for device in devices: try: os.popen("adb -s " + device[0] + " shell input keyevent 26") os.popen("adb -s " + device[0] + " shell am start -n com.xbw.arukas/com.xbw.arukas.ui.ADActivity") except: print u"%s-%s连接失败"%(device[0],device[1]) elif argv[1] == "6": if len(sys.argv)==2: print "请输入安装软件路径\n" else: for device in devices: try: os.popen("adb -s " + device[0] + " install -r " + argv[2]) except: print u"%s-%s连接失败"%(device[0],device[1]) elif argv[1] == "7": if len(sys.argv)==2: print "请输入卸载软件包名\n" else: for device in devices: try: os.popen("adb -s " + device[0] + " uninstall " + argv[2]) except: print u"%s-%s连接失败"%(device[0],device[1]) else: print u"参数错误" if __name__ == '__main__': devices = config.devices if len(sys.argv)==1: print u"请输入参数\n1.连接adb\n2.断开adb\n3.静音\n4.响铃\n5.启动一次广告\n6.安装软件\n7.卸载软件" else: control(sys.argv)

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667

远程控制

# -*- coding: utf-8 -*- import json import os import config from urlparse import parse_qs from wsgiref.simple_server import make_server import requests # 定义函数,参数是函数的两个参数,都是python本身定义的,默认就行了。 def application(environ, start_response): # 定义文件请求的类型和当前请求成功的code start_response('200 OK', [('Content-Type', 'application/json')]) # environ是当前请求的所有数据,包括Header和URL,body,这里只涉及到get # 获取当前get请求的所有数据,返回是string类型 params = parse_qs(environ['QUERY_STRING']) # 获取get中key为name的值 url = params.get('adb', [''])[0] result = None if url == 'info': #devices = getDevicesAll() devices = config.devices result = getInfoAll(devices) elif url == 'shot': device = params.get('device', [''])[0] result = getScreencap(device) elif url == 'ctrl': ml = params.get('ml', [''])[0] if ml == "open": ip = params.get('ip', [''])[0] result = connectAdb("%s:5555"%(ip),ip) elif ml == "close": ip = params.get('ip', [''])[0] result = disconnectAdb(ip) dic = {'result': result,'code':'10000'} # 组成一个数组,数组中只有一个字典 return [json.dumps(dic,ensure_ascii=False)] def uploadQiniu(bys): url = 'http://push.ecfun.cc/petpet/Server/src/app/qiniu/upload.php' img_file= {'img': bys}#此处img是服务器端post文件字段 data_result = requests.post(url, {}, files=img_file) return data_result def convert_img(path): result = "" with open(path, "r") as f: bys = f.read() bys_ = bys.replace(b"\r\n",b"\n") # 二进制流中的"\r\n" 替换为"\n" json_data = uploadQiniu(bys_).json() result = str(json_data['url']) with open(path, "w") as f: f.write(bys_) return result def connectAdb(dname,ip): state = "" try: result = os.popen("adb -s "+ dname +" connect "+ ip ).read() if 'unable' in result: state = 'unable' elif 'connected' in result: state = 'connected' except: state = 'failed' return state def disconnectAdb(ip): try: os.popen("adb disconnect "+ip) except: pass return 'disconnect' def getScreencap(dName): path_result = "%s/screenshot/%s"%(os.path.split(os.path.realpath(__file__))[0],dName) if not os.path.exists(path_result): os.makedirs(path_result) shell = "adb -s "+dName+" shell screencap -p > "+path_result+"/screen.png" try: os.popen(shell) except: pass return convert_img("%s/screen.png"%(path_result)) def getDevicesAll(): devices = [] try: for dName_ in os.popen("adb devices"): if "\t" in dName_: devices.append(dName_.split("\t")[0]) devices.sort(cmp=None, key=None, reverse=False) except: pass return devices def get_wifi_state(dName): state = "not connect" wifi = "4G" try: for wifi_ in os.popen("adb -s " + dName + " shell dumpsys wifi"): if "enabled" in wifi_: state = "connect" if "disabled" in wifi_: state = "not connect" if "ssid" == wifi_[0:4] and state == "connect": wifi = wifi_.split("=")[1].replace('\r\n','') except: pass return wifi def getInfoAll(devices): info = [] for i in range(0,len(devices)): model = "" barrey = "" try: for model_ in os.popen("adb -s "+ devices[i][0] +" shell getprop ro.product.model"): model = model_.replace('\r\n', '') for battery_ in os.popen("adb -s " + devices[i][0] + " shell dumpsys battery"): if "level" in battery_: barrey = battery_.split(":")[1].strip() .replace('\r\n', '') if barrey == "": info.append({"id":str(i+1),"device":devices[i][0],"model":devices[i][2],"barrey":"0%","wifi":"-","state":"offline"}) else: info.append({"id":str(i+1),"device":devices[i][0],"model":model,"barrey":barrey+"%","wifi":get_wifi_state(devices[i][0]),"state":"online"}) except: pass return info if __name__ == "__main__": port = 80 httpd = make_server("0.0.0.0", port, application) print "serving http on port {0}...".format(str(port)) httpd.serve_forever()

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127

通过内网穿透实现远程控制
效果图
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

网址:Python使用adb远程控制android设备 https://www.yuejiaxmz.com/news/view/629086

相关内容

3种方式自动化控制APP
史上最全的用Python操控手机APP攻略!建议收藏!
一篇清晰易懂的Python操控手机APP攻略!
Android App 压力测试
鸿蒙ADB工具:一键解锁设备潜能!
手机 自动化 python
解锁科技新篇章:一招教你轻松远程启动Android手机,告别等待,尽享便捷生活!
三种方式自动化控制APP
揭秘Android远程控制:安全便捷的软件选择与使用技巧
软硬兼施,让废旧的 Android 手机变身家庭监控

随便看看