Pure Python ADB Client

0.3.0.dev0 · active · verified Fri Apr 17

pure-python-adb provides a pure Python implementation of the Android Debug Bridge (ADB) client protocol. It allows Python applications to interact with ADB servers and connected Android devices programmatically, enabling tasks like shell commands, file transfers, and application management. The current version is 0.3.0.dev0, indicating active development with potential for API changes. Release cadence is irregular, as typical for a development branch.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart connects to the ADB server, lists connected devices, and performs basic operations like executing shell commands, pushing, and pulling files. Ensure an ADB server is running and a device is connected and authorized for it to work correctly.

from ppadb.client import Client as AdbClient
import os

# Ensure ADB server is running on 127.0.0.1:5037
# (e.g., run 'adb start-server' in your terminal)

host = os.environ.get('ADB_HOST', '127.0.0.1')
port = int(os.environ.get('ADB_PORT', 5037))

try:
    # Connect to the ADB server
    client = AdbClient(host=host, port=port)

    # List connected devices
    devices = client.devices()

    if devices:
        device = devices[0]
        print(f"Connected to device: {device.serial}")

        # Execute a shell command
        result = device.shell("echo Hello from Android")
        print(f"Shell command output: {result.strip()}")

        # Example: Push a file (create a dummy file first)
        with open('test_file.txt', 'w') as f:
            f.write('This is a test file.\n')
        device.push('test_file.txt', '/sdcard/test_file.txt')
        print("Pushed test_file.txt to /sdcard/")

        # Example: Pull a file
        device.pull('/sdcard/test_file.txt', 'downloaded_test_file.txt')
        print("Pulled test_file.txt to downloaded_test_file.txt")

        os.remove('test_file.txt')
        os.remove('downloaded_test_file.txt')

    else:
        print("No ADB devices found. Make sure a device is connected and authorized.")

except Exception as e:
    print(f"An error occurred: {e}")
    print("Please ensure the ADB server is running (e.g., 'adb start-server') and accessible.")

view raw JSON →