python3 adb 獲取設備序列號的實現

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();

以上為個人經驗,希望能給大傢一個參考,也希望大傢多多支持WalkonNet。

推薦閱讀: