What is the function of adb in python

ADB is a tool in the Android SDK. Using ADB, you can directly operate and manage the Android emulator or real Andriod devices.

The main functions of ADB are:

1、 Run Shell (command line) on Android device

2、 Manage port mapping for emulators or devices

3、 Upload/download files between computer and device

4、 Install the local APK software on the computer to the Android emulator or device

Python has two ways to call adb commands, one is os.system() and the other is os.popen().

The difference between the two is that the former cannot get the return value, and the latter can get the return value.

os.popen() returns a file object, which can be read directly using the read() method.

Content expansion:

How to call adb command in python

Two modules, os and subprocess, can be used to execute cmd commands in Python. The difference is that os is blocking and subprocess is non-blocking, so it is more suitable for us to use subprocess. Next, I will give a command to query the connected device to see how it is written in python. The command used is adb devices.

import subprocess
order='adb devices' #Get connected device
pi= subprocess.Popen(order,shell=True,stdout=subprocess.PIPE)
print pi.stdout.read() #Print result

In the actual printing result, you can see that the current computer is connected to three devices. Here we need to explain again that the result of the adb devices command is one-off, so we can use the read method to read data without any problem. However, some of the adb commands return results in real time, such as the command logcat that outputs mobile phone logs. The result will continue to print out the current device operation log information content. If we need to get the print result of this type of command in python, if we still use the read method, the waiting time for the return of the result will be very long, here we have to change One way to read the result is as follows.

import subprocess
order='adb logcat'
pi= subprocess.Popen(order,shell=True,stdout=subprocess.PIPE)for i initer(pi.stdout.readline,'b'):
print I

This printing effect is the same as the operation in cmd, and the log information is printed in real time. Here we use the readline method. In fact, this writing method is similar to reading files, single line reading and full content reading. Because the framework of pyapp has been basically written, I have the idea of writing this article, and share some of python's experience in processing adb commands. At present, the main difference between python in calling adb commands is these two points. The purpose is to find the required function commands to obtain the result data, and then process the returned data through python to achieve the purpose of automated testing. Everyone should use the adb command well, and one thing to note is the combination of various parameters of each command. For example, the implementation of pyapp supports multi-device connection, so when we perform adb command operations on a mobile phone, we need Bring -s plus the device number to indicate the specific device to be operated, otherwise the command will report an error.

For example, when we click on a device, the command should be written like this: adb -s 49dsd4554wdsa shell input tap 600 900, where '49dsd4554wdsa' is the device number, and '600 900' click on the screen coordinates. So you can see that after adding -s, you can easily operate multiple devices at the same time.

So far, this article about the functions of adb in python is introduced. For more detailed explanations of python adb functions, please search for the previous articles of ZaLou.Cn or continue to browse the related articles below. I hope you will support ZaLou.Cn more in the future. !

Recommended Posts

What is the function of adb in python
What is the id function of python
What is the function body of python
What is the use of Python
What is the scope of python variables
What is an anonymous function in Python
What is the prospect of python development
What is introspection in python
What is object-oriented in python
What is the advantage of python over corporate language
The usage of wheel in python
What is list comprehension in python
Is there function overloading in python
​What are the numbers in Python?
The usage of tuples in python
Understanding the meaning of rb in python
What is the use of python's pip
What are the required parameters of python
The usage of Ajax in Python3 crawler
Where is the pip path of python3
What are the advantages of python language
python3 realizes the function of mask drawing
What is a sequence table in Python
Is there a helper function in python
How to use the round function in python
How to use the zip function in Python
Join function in Python
What does the tab key in python mean
Python implements the shuffling of the cards in Doudizhu
The meaning and usage of lists in python
How to use the format function in python
What is the difference between python and pycharm
The consequences of uninstalling python in ubuntu, very
What is the difference between synchronous and asynchronous Python?
Detailed explanation of the principle of Python function parameter classification
How to understand the introduction of packages in Python
What are the ways to open files in python
Consolidate the foundation of Python (4)
Consolidate the foundation of Python(7)
Subscripts of tuples in Python
Consolidate the foundation of Python(6)
Consolidate the foundation of Python(5)
What is Python variable scope
Consolidate the foundation of Python (3)
How to find the area of a circle in python
Learn the hard core operation of Python in one minute
Minimalism is the soul of Python | Python code to find bugs (10)
Summary of logarithm method in Python
Use of Pandas in Python development
How Python implements the mail function
Python handles the 4 wheels of Chinese
What does rc1 mean in python
Use of numpy in Python development
What does def in python do
Python simulation of the landlord deal
Detailed usage of dictionary in Python
Usage of os package in python
Everything in Python is an object
What does np do in python
Talking about the modules in Python
The premise of Python string pooling