简体   繁体   English

python解析wifi SSID数据

[英]python parsing wifi SSID data

I currently have the following. 我目前有以下内容。 But I have noticed that if a SSID includes a space the ordering goes off: 但是我注意到如果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)

My objective is to get the SSID, mac address and signal strength stored so it can be compared to a spreadsheet. 我的目标是存储SSID,mac地址和信号强度,以便将其与电子表格进行比较。

From airport --help : airport --help

-x        --xml                Print info as XML

Which will output the information in Apple's XML format for property lists, which should be parsable more reliably. 这将以Apple的XML格式输出属性列表的信息,这些信息应该可以更可靠地解析。 Python even comes with a plistlib module which can parse the XML into a Python dictionary: 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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM