Yes, it's possible.
See This demo. Click on the menu icon at the top right, and then the "WiFi setup" button.
HTML Web-page:
When the page is opened, the ReqWifiList() JavaScript script creates an AJAX "GET" request for the list of networks. When the response comes back, the ShowWiFiNetworks(wifiNetListStr) script is called, which lists the networks.
// Request a list of WiFi networks
function ReqWifiList() {
// Constants
var GetSrvc = '/getWiFiList';
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState==4 && xmlhttp.status==200){
// Show the networks
ShowWiFiNetworks(xmlhttp.responseText);
}
}
xmlhttp.open("GET", GetSrvc, true);
xmlhttp.send();
}
// Show the available WiFi Networks
function ShowWiFiNetworks(wifiNetListStr) {
// Show the networks
}
Web server (Flask):
from flask import Flask, render_template, request
import lithiumate_data_logger
app = Flask(__name__)
# Show the page
@app.route('/')
def index():
return render_template('index.html')
# Handle a request from the page for the list of WiFi networks
@app.route('/getWiFiList', methods=['GET'])
def getWiFiList():
srvResp = 'Fail'
if request.method == 'GET':
srvResp = lithiumate_data_logger.getWiFiList()
return srvResp
if __name__ == '__main__':
app.run(debug=True, host='0.0.0.0')
Python script:
def getWiFiList():
"""Get a list of WiFi networks"""
wifiNetworkList = ''
# Presently connected network
connectedNetworkNameResponse = ''
try:
connectedNetworkNameResponse = subprocess.check_output(['sudo','iwgetid'])
break
except subprocess.CalledProcessError as e:
print 'ERROR get connected network: '
print e
connectedNetworkNameStr = re.findall('\"(.*?)\"', connectedNetworkNameResponse) #" Find the string between quotes
wifiNetworkList = connectedNetworkNameStr[0] # [0] returns the first one
# Available networks
availableNetworksResponse = ''
try:
availableNetworksResponse = subprocess.check_output(['sudo','iw','dev','wlan0','scan'])
break
except subprocess.CalledProcessError as e:
print 'ERROR get list of networks: '
print e
print availableNetworksResponse
availableNetworksLines = availableNetworksResponse.split('\n')
for availableNetworksLine in availableNetworksLines:
if 'SSID' in availableNetworksLine:
# Typical line:
# SSID: elithion belkin
essid = availableNetworksLine.replace('SSID:','').strip()
wifiNetworkList = wifiNetworkList + ',' + essid
return wifiNetworkList
Note that the response string is of the form: 'connected SSID, an SSID, an SSID, an SSID,... an SSID'