客户端的纯蟒蛇实现

pure-python-adb的Python项目详细描述


这是adb客户机的纯python实现。

您可以使用它与adb服务器(而不是设备/模拟器上的adb守护进程)通信。

当您使用adb命令时

https://raw.githubusercontent.com/Swind/pure-python-adb/master/docs/adb_cli.png

现在您可以使用pure python adb作为adb命令行连接到adb服务器

https://raw.githubusercontent.com/Swind/pure-python-adb/master/docs/adb_pure_python_adb.png

这个包支持adb命令行工具的大部分功能。

  1. ADB设备
  2. 亚行外壳
  3. ADB前进
  4. adb拉/推
  5. ADB安装/卸载

要求

python 2.7+/python 3.6+

安装

$pip install -U pure-python-adb

示例

连接到ADB服务器并获取

版本
fromadb.clientimportClientasAdbClient# Default is "127.0.0.1" and 5037client=AdbClient(host="127.0.0.1",port=5037)print(client.version())>>>39

连接到设备

fromadb.clientimportClientasAdbClient# Default is "127.0.0.1" and 5037client=AdbClient(host="127.0.0.1",port=5037)device=client.device("emulator-5554")

列出所有设备(adb设备)并在所有设备上安装/卸载APK

fromadb.clientimportClientasAdbClientapk_path="example.apk"# Default is "127.0.0.1" and 5037client=AdbClient(host="127.0.0.1",port=5037)devices=client.devices()fordeviceindevices:device.install(apk_path)# Check apk is installedfordeviceindevices:print(device.is_installed("example.package"))# Uninstallfordeviceindevices:device.uninstall("example.package)

亚行外壳

fromadb.clientimportClientasAdbClient# Default is "127.0.0.1" and 5037client=AdbClient(host="127.0.0.1",port=5037)device=client.device("emulator-5554")device.shell("echo hello world !")
defdump_logcat(connection):whileTrue:data=connection.read(1024)ifnotdata:breakprint(data.decode('utf-8'))connection.close()fromadb.clientimportClientasAdbClient# Default is "127.0.0.1" and 5037client=AdbClient(host="127.0.0.1",port=5037)device=client.device("emulator-5554")device.shell("logcat",handler=dump_logcat)

逐行读取logcat

fromadb.clientimportClientdefdump_logcat_by_line(connect):file_obj=connect.socket.makefile()forindexinrange(0,10):print("Line {}: {}".format(index,file_obj.readline().strip()))file_obj.close()connect.close()client=Client()device=client.device("emulator-5554")device.shell("logcat",handler=dump_logcat_by_line)

截图

fromadb.clientimportClientasAdbClientclient=AdbClient(host="127.0.0.1",port=5037)device=client.device("emulator-5554")result=device.screencap()withopen("screen.png","wb")asfp:fp.write(result)

推动

fromadb.clientimportClientasAdbClientclient=AdbClient(host="127.0.0.1",port=5037)device=client.device("emulator-5554")device.push("example.apk","/sdcard/example.apk")

fromadb.clientimportClientasAdbClientclient=AdbClient(host="127.0.0.1",port=5037)device=client.device("emulator-5554")device.shell("screencap -p /sdcard/screen.png")device.pull("/sdcard/screen.png","screen.png")

如何运行测试用例

准备

  1. 安装Docker
  2. 安装docker compose
pip install docker-compose
  1. 修改test/conftest.py

将adb主机的值更改为“模拟器”

adb_host="emulator"
  1. 运行测试用例
docker-compose up

结果

Starting purepythonadb_emulator_1 ... done
Recreating purepythonadb_python_environment_1 ... done
Attaching to purepythonadb_emulator_1, purepythonadb_python_environment_1
emulator_1            | + echo n
emulator_1            | + /home/user/android-sdk-linux/tools/bin/avdmanager create avd -k system-images;android-25;google_apis;x86 -n Docker -b x86 -g google_apis --device 8 --force
Parsing /home/user/android-sdk-linux/emulator/package.xmlParsing /home/user/android-sdk-linux/patcher/v4/package.xmlParsing /home/user/android-sdk-linux/platform-tools/package.xmlParsing /home/user/android-sdk-linux/platforms/android-25/package.xmlParsing /home/user/android-sdk-linux/system-images/android-25/google_apis/x86/package.xmlParsing /home/user/android-sdk-linux/tools/package.xml+ echo hw.keyboard = true
emulator_1            | + adb start-server
emulator_1            | * daemon not running; starting now at tcp:5037
python_environment_1  | ============================= test session starts ==============================
python_environment_1  | platform linux -- Python 3.6.1, pytest-3.6.3, py-1.5.4, pluggy-0.6.0
python_environment_1  | rootdir: /code, inifile:
python_environment_1  | collected 27 items
python_environment_1  |
emulator_1            | * daemon started successfully
emulator_1            | + exec /usr/bin/supervisord
emulator_1            | /usr/lib/python2.7/dist-packages/supervisor/options.py:298: UserWarning: Supervisord is running as root and it is searching for its configuration file in default locations (including its current working directory); you probably want to specify a "-c" argument specifying an absolute path to a configuration file for improved security.
emulator_1            |   'Supervisord is running as root and it is searching '
emulator_1            | 2018-07-07 17:19:47,560 CRIT Supervisor running as root (no user in config file)
emulator_1            | 2018-07-07 17:19:47,560 INFO Included extra file "/etc/supervisor/conf.d/supervisord.conf" during parsing
emulator_1            | 2018-07-07 17:19:47,570 INFO RPC interface 'supervisor' initialized
emulator_1            | 2018-07-07 17:19:47,570 CRIT Server 'unix_http_server' running without any HTTP authentication checking
emulator_1            | 2018-07-07 17:19:47,570 INFO supervisord started with pid 1
emulator_1            | 2018-07-07 17:19:48,573 INFO spawned: 'socat-5554' with pid 74
emulator_1            | 2018-07-07 17:19:48,574 INFO spawned: 'socat-5555' with pid 75
emulator_1            | 2018-07-07 17:19:48,576 INFO spawned: 'socat-5037' with pid 76
emulator_1            | 2018-07-07 17:19:48,578 INFO spawned: 'novnc' with pid 77
emulator_1            | 2018-07-07 17:19:48,579 INFO spawned: 'socat-9008' with pid 78
emulator_1            | 2018-07-07 17:19:48,582 INFO spawned: 'emulator' with pid 80
emulator_1            | 2018-07-07 17:19:49,607 INFO success: socat-5554 entered RUNNING state, process has stayed up for > than 1 seconds (startsecs)
emulator_1            | 2018-07-07 17:19:49,607 INFO success: socat-5555 entered RUNNING state, process has stayed up for > than 1 seconds (startsecs)
emulator_1            | 2018-07-07 17:19:49,607 INFO success: socat-5037 entered RUNNING state, process has stayed up for > than 1 seconds (startsecs)
emulator_1            | 2018-07-07 17:19:49,607 INFO success: novnc entered RUNNING state, process has stayed up for > than 1 seconds (startsecs)
emulator_1            | 2018-07-07 17:19:49,608 INFO success: socat-9008 entered RUNNING state, process has stayed up for > than 1 seconds (startsecs)
emulator_1            | 2018-07-07 17:19:49,608 INFO success: emulator entered RUNNING state, process has stayed up for > than 1 seconds (startsecs)
python_environment_1  | test/test_device.py ..............                                       [ 51%]
python_environment_1  | test/test_host.py ..                                                     [ 59%]
python_environment_1  | test/test_host_serial.py ........                                        [ 88%]
python_environment_1  | test/test_plugins.py ...                                                 [100%]
python_environment_1  |
python_environment_1  | ------------------ generated xml file: /code/test_result.xml -------------------
python_environment_1  | ========================= 27 passed in 119.15 seconds ==========================
purepythonadb_python_environment_1 exited with code 0
Aborting on container exit...
Stopping purepythonadb_emulator_1 ... done

更多信息

android调试桥的纯node.js客户端

adbkit

0.1.0(2018-06-23)

  • pypi上的第一个版本。

0.1.6(2019-01-21)

  • Fix#4 push不像equiv adb push from命令行那样保留原始的timestap
  • 修复6转发列表也应检查序列号
  • 修正8:adb/command/host/\u init.py在解析“设备”数据时会出现异常

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
通过GWT JSNI传递Java对象时发生javascript错误   java Spring启动项目wildfly服务器失败   java如何进行会话来存储Id?   javajpa/hibernate如何通过带注释的外键映射元素集合   Java将字节[]转换为双[],反之亦然   eclipse显示Java双值   java如何正确读取socket数据包   是否存在用于集合处理的Java库?   javacom。太阳jna。无法将指针强制转换为com。太阳jna。站台win32。温迪夫。LPARAM   java Eclipse插件开发。我可以在插件中使用已经存在的jar文件吗?   分析Java错误:找不到符号变量   java如何使按钮摆动以进行大的处理   如何通过传递bucket name和file name从java中的minio服务器下载多个文件   JavaScrollPane JavaFX使其滚动更多?   java无法加载C:\Users\username\AppData\Local\Android\Sdk\buildtools\28.0.3\lib\dx。罐子   JavaSwing中是否有一个“组框”等价物?   java使用slf4j和log4j与单独使用log4j相比,是否存在性能开销?   java模式和匹配:格式化文本文件   JTable行增长的java问题