封装python使用cmd命令函数,使用第三方库subprocess库,使用前没有的话需要安装,pip install subprocess。
import subprocess
import time
def use_cmd(commd_str):
p = subprocess.Popen(commd_str, shell=True)
p.wait()
time.sleep(0.5)
对于手机,使用adb命令前, 要先和电脑处于同一局域网,连上同一个路由器的wifi。
电脑连接手机的ip
def connect_phone(ip_connect):
#ip_connect = "xxx.xxx.x.xxx:xxxx"
connect_command = f"adb connect {ip_connect}"
use_cmd(connect_command)
安装app命令
def install_app(app_path):
#app_path="X:\\xxx\\xxx"
install_command=f"adb install -r {app_path}"
use_cmd(install_command)
启动app命令,acti是app的"包名/活动名"
def start_acti(acti):
start_command = f"adb shell am start -W {acti}"
use_cmd(start_command)
获取app的包名和活动名,可以用于启动app。
def show_packname():
commd="adb shell dumpsys window | findstr mCurrentFocus"
use_cmd(commd)
应用调试,显示错误信息
def throw_err():
throw_command="adb logcat AndroidRuntime:E *:S"
use_cmd(throw_command)
显示连接的设备
def show_devices():
commd="adb devices"
use_cmd(commd)
传送电脑文件到手机
def send_file(computerfile_path,phone_path):
commd=f"adb {computerfile_path} /storage/emulated/0/{phone_path}"
use_cmd(commd)