Python | 获取iOS设备信息的轻量级框架

今天接着上一篇Python | 获取Android设备信息的轻量级框架,来讲讲

如何通过Python实现一个轻量级的库来获取电脑上连接的iOS设备信息。

这个库只有一个文件,通过封装libimobiledevice命令实现,返回的是一个包含所有设备信息的标准json格式的列表方便解析,下面简单介绍一下:

libimobiledevice命令封装

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
@staticmethod
def get_ios_devices():
devices = []
output = Shell.invoke('idevice_id -l')
config_file = os.path.join(os.path.dirname(__file__), 'ios_mapping.json')
with open(config_file, 'r') as f:
config = json.loads(f.read())

if len(output) > 0:
udids = output.strip('\n').split('\t')
for udid in udids:
dic = {"os_type": 'iOS', "uid": udid}
output = Shell.invoke('ideviceinfo -u %s -k ProductType' % udid)
device_type = config[output.strip('\n')]
brand = ''
# -1表示找不到 0表示下标
if device_type.find("iPhone") != -1:
brand = 'iPhone'
elif device_type.find("iPad") != -1:
brand = 'iPad'
elif device_type.find("iPod") != -1:
brand = 'iPod'

dic['brand'] = brand
dic['model'] = device_type

output = Shell.invoke('ideviceinfo -u %s -k ProductVersion' % udid)
dic['os_type'] = 'iOS'
dic['os_version'] = output.strip('\n')
dic['rom_version'] = output.strip('\n')

output = Shell.invoke('idevicename -u %s' % udid)
dic['device_name'] = output.strip('\n')
devices.append(dic)
return devices

设备信息数据结构

1
2
3
4
5
6
7
8
9
10
11
[
{
"uid": "xxxxxxxxxxxxxx1f8a4dcfaac1fd01",
"rom_version": "11.0.3",
"brand": "iPhone",
"device_name": "马飞的 iPhone",
"os_version": "11.0.3",
"model": "iPhone6s",
"os_type": "iOS"
}
]

注:有时候会报Couldn’t connect to lockdown这样的错误,执行下面命令即可:

1
2
3
4
$ brew uninstall ideviceinstaller
$ brew uninstall libimobiledevice
$ brew install --HEAD libimobiledevice
$ brew install ideviceinstaller

这个库我已经上传到Pypi仓库,源码在github:https://github.com/logan62334/python-apptoolkit,点击阅读原文可以访问


FullStackEngineer的公众号,更多分享

Share Comments