python解析wifi SSID数据

时间:2012-01-16 15:15:14

标签: python parsing wifi

我目前有以下内容。但是我注意到如果SSID包含一个空格,那么排序就会消失:

import subprocess
import csv

process = subprocess.Popen(['/System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport', '-s'], stdout=subprocess.PIPE)
stdout, stderr = process.communicate()

reader = csv.DictReader(stdout.decode('ascii').splitlines(),
                        delimiter=' ', skipinitialspace=True,
                        fieldnames=['wifi_name', 'mac', 'strength'])

for row in reader:
    print(row)

我的目标是存储SSID,mac地址和信号强度,以便将其与电子表格进行比较。

1 个答案:

答案 0 :(得分:5)

来自airport --help

-x        --xml                Print info as XML

将以Apple的XML格式输出属性列表的信息,这些信息应该可以更可靠地解析。 Python甚至附带了一个plistlib模块,它可以将XML解析为Python字典:

#!/usr/bin/env python
from subprocess import Popen, PIPE
from plistlib import readPlist
from pprint import pprint

AirportPath = '/System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport'

proc = Popen([AirportPath, '-s', '-x'], stdout=PIPE)

ssid_data = readPlist(proc.stdout)
pprint(ssid_data)