Python使用adb远程控制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)

远程控制

# -*- 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()

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

  • 1
    点赞
  • 37
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值