Last active
August 12, 2019 14:17
-
-
Save shukriadams/922a4a916f136d48ef1f6d019f06cef2 to your computer and use it in GitHub Desktop.
Web service that returns list of wifi access points. Intended for Raspberry pi.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # This script starts a simple HTTP server which replies on ":8081/accesspoints", and returns JSON for an array of objects with | |
| # describe wifi access points. Works on Python 2.x. | |
| # | |
| # Packages required : | |
| # pip install -Iv flask_restful==0.3.7 | |
| from flask import Flask | |
| from flask_restful import Api, Resource, reqparse | |
| import os | |
| import re | |
| import json | |
| port=8081 | |
| endpoint='/accesspoints' | |
| debug=False | |
| app = Flask(__name__) | |
| api = Api(app) | |
| # returns first item in string array, stripped of whitespace. If array is empty, returns empty string | |
| def firstOrNone(list): | |
| if list is None or len(list) == 0: | |
| return '' | |
| return list[0].strip() | |
| class GetAccessPoints(Resource): | |
| def get(self): | |
| accessPoints = os.popen('iwlist wlan0 scan | egrep "Cell|ESSID|Signal|Rates"').read() | |
| # split accessPoints string into array using "Cell " as divider | |
| accessPoints = accessPoints.split('Cell ') | |
| result = [] | |
| for accessPoint in accessPoints: | |
| data = {} | |
| data['SSID'] = firstOrNone(re.findall(r"ESSID:\"(.*)\"\n", accessPoint)) | |
| data['bitrate'] = firstOrNone(re.findall(r"Bit Rates:(.*)\n", accessPoint)) | |
| data['level'] = firstOrNone(re.findall(r"Signal level=(.*)/", accessPoint)) | |
| # couldn't figure out clean regex to parse out quality %, done in two steps | |
| data['quality'] = firstOrNone(re.findall(r"Quality=(.*) Sig", accessPoint)) | |
| data['quality'] = firstOrNone(re.findall(r"(.*)/", data['quality'])); | |
| result.append(data) | |
| return json.dumps(result), 200 | |
| api.add_resource(GetAccessPoints, endpoint) | |
| app.run(host='0.0.0.0', port=port, debug=debug) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Daemonize python script with this. | |
| # Create file /etc/systemd/system/wifistatus.service, copy this script's content to it. then run : | |
| # sudo systemctl daemon-reload | |
| # sudo systemctl start wifistatus | |
| # sudo systemctl enable wifistatus | |
| [Unit] | |
| Description=wifi status service | |
| After=network.target | |
| StartLimitIntervalSec=0 | |
| [Service] | |
| WorkingDirectory=/srv/wifistatus | |
| ExecStart=/usr/bin/python2.7 app.py | |
| Restart=always | |
| StandardOutput=syslog | |
| StandardError=syslog | |
| SyslogIdentifier=giveaway | |
| User=pi | |
| [Install] | |
| WantedBy=multi-user.target | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment