python3 adb 获取设备序列号的实现

编辑: admin 分类: python 发布时间: 2021-12-24 来源:互联网

python3 adb 获取设备序列号

import subprocess
def deal_cmd(cmd):
	pi = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE)
	# print(pi.stdin.read())
	return pi.stdout.read()
 
def deal_result():
	result = deal_cmd('adb devices')
	result = result.decode("utf-8")
	if result.startswith('List of devices attached'):
		# 查看连接设备
		result = result.strip().splitlines()
		# 查看连接设备数量
		device_size = len(result)
		if device_size > 1:
			device_list = []
			for i in range(1, device_size):
				device_detail = result[1].split('\t')
				if device_detail[1] == 'device':
					device_list.append(device_detail[0])
				elif device_detail[1] == 'offline':
					print(device_detail[0])
					return False, '连接出现异常,设备无响应'
				elif device_detail[1] == 'unknown':
					print(device_detail[0])
					return False, '没有连接设备'
			return True, device_list
		else:
			return False, "没有可用设备"

Python 获取设备名及地址

1. 查询本机的设备名及IP地址,打开Python 3.6(32-bit),输入:

import  socket
host_name = socket.gethostname()
print(" Host name: %s" %host_name)
print(" IP address: %s" %socket.gethostbyname(host_name))

2. 查询本地的设备名及IP地址,使用内置的类方法,定义成一个独立的函数print_device_info()

import  socket
def  print_device_info():
      host_name = socket.gethostname()
      print(" Host name: %s"  %host_name)
      print(" IP address: %s"  %socket.gethostbyname(host_name))
if  __name__=='__main__':
      print_device_info();

3. 查询远程设备名及IP地址,定义一个函数print_device_remote_info()

import  socket
def  print_device_remote_info():
      host_name = 'home.lenovo'
      print(" Host name: %s"  %host_name)
      print(" IP address: %s"  %socket.gethostbyname(host_name))
if  __name__=='__main__':
      print_device_remote_info();

以上为个人经验,希望能给大家一个参考,也希望大家多多支持hwidc。