これまでのまとめ
ソースの配置
wifi_menu.py
contents
└ index.html
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
シンプル HTTP サーバー(WIFI設定用)Python3版
Usage:
./wifi_menu.py []
"""
import os
import sys
import http.server as s
import json
import logging
import configparser
import hashlib
import base64
PORT = 80
PAGE_ID = ''
PAGE_PASSWORD = 'xxxxxxxx'
PAGE_KEY = '' # BASIC認証用
# WIFIの設定ファイルの場所
WIFI_CONF_PATH = "/etc/wpa_supplicant/wpa_supplicant.conf"
# WIFIの作業用ファイルの場所
WIFI_TMP_PATH = "../wpa.conf"
CONF_WIFI_SEC_NM = "wificonnect"
#--------------------------------------------------------------------------
# HTTPリクエスト対応処理
#--------------------------------------------------------------------------
class MyHandler(s.BaseHTTPRequestHandler):
def do_AUTHHEAD(self):
self.send_response(401)
self.send_header("WWW-Authenticate", "Basic realm=\"Please enter your user information\"")
self.send_header("Content-type", "text/html")
self.end_headers()
return
def do_GET(self):
global PAGE_KEY
chk_pass = False
if (self.headers.get("Authorization") == None):
self.do_AUTHHEAD()
self.wfile.write('no auth user received'.encode("utf-8"))
pass
elif self.headers.get("Authorization") == "Basic " + PAGE_KEY:
chk_pass = True
else:
self.do_AUTHHEAD()
self.wfile.write((self.headers.get("Authorization")).encode("utf-8"))
self.wfile.write('not authenticated'.encode("utf-8"))
pass
if (self.path == "/"):
self.path = "/index.html"
if (chk_pass == True):
try:
send_reply = False
if (self.path.endswith(".html")):
mimetype = "text/html"
send_reply = True
if (self.path.endswith(".jpg")):
mimetype = "image/jpg"
send_reply = True
if (self.path.endswith(".gif")):
mimetype = "image/gif"
send_reply = True
if (self.path.endswith(".js")):
mimetype = "application/javascript"
send_reply = True
if (self.path.endswith(".css")):
mimetype = "text/css"
send_reply = True
if (send_reply == True):
cur_dir = os.getcwd() # カレントフォルダ取得
logging.info(cur_dir)
tar_file = cur_dir + self.path
if (os.path.isfile(tar_file)):
f = open(tar_file, encoding='utf-8')
txt = f.read()
f.close()
self.send_response(200)
self.send_header("Content-type", mimetype)
self.end_headers()
logging.info("GET request,\nPath: %s\nHeaders:\n%s\n", str(self.path), str(self.headers))
#self.wfile.write("GET request for {}" . format(self.path).encode("utf-8"))
self.wfile.write(txt.encode("utf-8"))
return
except IOError:
self.send_error(404, 'File Not Found: %s' % self.path)
def do_POST(self):
global WIFI_CONF_PATH, WIFI_TMP_PATH, CONF_WIFI_SEC_NM
# リクエスト取得
content_len = int(self.headers.get("content-length"))
body = json.loads(self.rfile.read(content_len).decode("utf-8"))
logging.info("post")
# レスポンス処理
act = body["act"]
if (act == "write"):
# wifi書き込み
wifi_ssid = str(body["wifi_ssid"])
wifi_psk = str(body["wifi_psk"])
wifi_scan_ssid = str(body["wifi_scan_ssid"])
wifi_key_mgmt = str(body["wifi_key_mgmt"])
# 作業設定を読み込む
config = configparser.ConfigParser()
config.read(WIFI_TMP_PATH)
r_wifi_ssid = ""
if (config.has_option(CONF_WIFI_SEC_NM, "wifi_ssid")):
r_wifi_ssid = config.get(CONF_WIFI_SEC_NM, "wifi_ssid")
r_wifi_psk = ""
if (config.has_option(CONF_WIFI_SEC_NM, "wifi_psk")):
r_wifi_psk = config.get(CONF_WIFI_SEC_NM, "wifi_psk")
r_wifi_scan_ssid = ""
if (config.has_option(CONF_WIFI_SEC_NM, "wifi_scan_ssid")):
r_wifi_scan_ssid = config.get(CONF_WIFI_SEC_NM, "wifi_scan_ssid")
r_wifi_key_mgmt = ""
if (config.has_option(CONF_WIFI_SEC_NM, "wifi_key_mgmt")):
r_wifi_key_mgmt = config.get(CONF_WIFI_SEC_NM, "wifi_key_mgmt")
bef_wifi_cnf = 'network={\n' + \
' ssid="' + r_wifi_ssid + '"\n' + \
' psk="' + r_wifi_psk + '"\n'
if (r_wifi_scan_ssid != ''):
bef_wifi_cnf += ' scan_ssid=1\n'
if (r_wifi_scan_ssid != ''):
bef_wifi_cnf += ' key_mgmt=' + r_wifi_key_mgmt + '\n'
bef_wifi_cnf += '}\n'
if (os.path.exists(WIFI_CONF_PATH)):
f = open(WIFI_CONF_PATH, 'w+', encoding='utf-8')
contents = f.read()
logging.info(contents)
contents = contents.replace(bef_wifi_cnf, "") # 前回の設定をクリアする
f.write(contents)
f.close()
if (True):
f = open(WIFI_CONF_PATH, 'a', encoding='utf-8')
f.write('network={\n')
f.write(' ssid="' + wifi_ssid + '"\n')
f.write(' psk="' + wifi_psk + '"\n')
# ステルス時、書き出す
if (wifi_scan_ssid != ''):
f.write(' scan_ssid=1\n')
# WPA時は、書き出さない
if (wifi_key_mgmt != ''):
f.write(' key_mgmt=' + wifi_key_mgmt + '\n')
f.write('}\n')
f.close()
# 作業設定の書き出し
if (not config.has_section(CONF_WIFI_SEC_NM)):
config.add_section(CONF_WIFI_SEC_NM)
config.set(CONF_WIFI_SEC_NM, "wifi_ssid", wifi_ssid)
config.set(CONF_WIFI_SEC_NM, "wifi_psk", wifi_psk)
config.set(CONF_WIFI_SEC_NM, "wifi_scan_ssid", wifi_scan_ssid)
config.set(CONF_WIFI_SEC_NM, "wifi_key_mgmt", wifi_key_mgmt)
f = open(WIFI_TMP_PATH, "w+", encoding='utf-8')
config.write(f)
f.close()
ret = {
"ret": "1"
}
else:
# 読込
config = configparser.ConfigParser()
config.read(WIFI_TMP_PATH)
r_wifi_ssid = ""
if (config.has_option(CONF_WIFI_SEC_NM, "wifi_ssid")):
r_wifi_ssid = config.get(CONF_WIFI_SEC_NM, "wifi_ssid")
r_wifi_psk = ""
if (config.has_option(CONF_WIFI_SEC_NM, "wifi_psk")):
r_wifi_psk = config.get(CONF_WIFI_SEC_NM, "wifi_psk")
r_wifi_scan_ssid = ""
if (config.has_option(CONF_WIFI_SEC_NM, "wifi_scan_ssid")):
r_wifi_scan_ssid = config.get(CONF_WIFI_SEC_NM, "wifi_scan_ssid")
r_wifi_key_mgmt = ""
if (config.has_option(CONF_WIFI_SEC_NM, "wifi_key_mgmt")):
r_wifi_key_mgmt = config.get(CONF_WIFI_SEC_NM, "wifi_key_mgmt")
ret = {
"wifi_ssid": r_wifi_ssid,
"wifi_psk": r_wifi_psk,
"wifi_scan_ssid": r_wifi_scan_ssid,
"wifi_key_mgmt": r_wifi_key_mgmt,
"ret": "1"
}
self.send_response(200)
self.send_header("Access-Control-Allow-Origin", "*")
self.send_header("Content-type", "application/json;charset=utf-8")
self.end_headers()
ret_json = json.dumps(ret, sort_keys=False, indent=4, ensure_ascii=False)
self.wfile.write(ret_json.encode("utf-8"))
#--------------------------------------------------------------------------
# サーバ起動
#--------------------------------------------------------------------------
def run(server_class=s.HTTPServer, handler_class=MyHandler, port=PORT):
global PAGE_ID, PAGE_PASSWORD, PAGE_KEY
logging.basicConfig(level=logging.INFO)
# BASIC認証用のキーを作成
secret = '{}:{}'.format(PAGE_ID, PAGE_PASSWORD)
encodedBytes = base64.b64encode(secret.encode("utf-8"))
PAGE_KEY = str(encodedBytes, "utf-8")
#logging.info('page_key: ' + PAGE_KEY)
server_address = ("", port) # IPを指定した場合、IP外からアクセスが不能
httpd = server_class(server_address, handler_class)
logging.info("Starting httpd...\n")
try:
print("Starting server, use to stop")
httpd.serve_forever()
except KeyboardInterrupt:
pass
httpd.server_close()
logging.info("Stopping httpd...\n")
if (__name__ == "__main__"):
os.chdir(os.path.join(os.path.dirname(__file__), 'contents')) # カレントフォルダ設定
if (len(sys.argv) == 2):
run(port=int(sys.argv[1]))
else:
run()
wifi_menu.py
index.html
<!doctype html> <html lang="ja"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <title>Configuration</title> <style type="text/css"> .tbl_main { margin: 20px; padding: 10px; background: #eee; border-radius: 10px; } .tbl_main, TD, TH { padding: 0px 5px 0px 5px; } .lrg_ttl { font-size: 22px; font-weight: bold; } .sml_ttl { font-size: 11px; } .sub_ttl { font-size: 17px; font-weight: bold; } .fld_ttl { padding: 0px 5px 0px 15px; font-size: 15px; } .input_l { width: 400px; } .set_button { padding: 5px; margin: 15px 0px 10px 0px; width: 100%; font-size: 20px; font-weight: bold; } </style> <script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script> <script> (window.onload = function() { axios.post('/', { "act": "get" }) .then(function (response) { var result = document.getElementById('result1'); document.getElementById('wifi_ssid').value = response.data['wifi_ssid']; document.getElementById('wifi_psk').value = response.data['wifi_psk']; var wifi_scan_ssid = response.data['wifi_scan_ssid']; if (wifi_scan_ssid == "1") document.getElementById('wifi_scan_ssid').checked = true; document.getElementById('wifi_key_mgmt').value = response.data['wifi_key_mgmt']; result.innerHTML = response.data['ret']; }) .catch(function (error) { console.log(error) }); })(); function setWrite() { if (window.confirm("Are you sure you want to update it?")) { var wifi_scan_ssid = ""; if (document.getElementById('wifi_scan_ssid').checked) wifi_scan_ssid = "1"; axios.post('/', { "act": "write", "wifi_ssid": document.getElementById('wifi_ssid').value, "wifi_psk": document.getElementById('wifi_psk').value, "wifi_scan_ssid": wifi_scan_ssid, "wifi_key_mgmt": document.getElementById('wifi_key_mgmt').value }) .then(function (response) { var result = document.getElementById('result2'); result.innerHTML = response.data['ret']; alert("Finished"); }) .catch(function (error) { console.log(error) }); } } </script> </head> <body> <center> <table class="tbl_main"> <tr> <td colspan="2" align="center"> <div style="padding: 10px 0px 0px 0px;"><span class="lrg_ttl">Configuration Menu</span></div> <div style="margin: -5px 0px 0px 0px;"><span class="sml_ttl">©2019 Sherpa CO., LTD. ver:1.1a</span></div> </td> </tr> <tr> <td colspan="2"> <hr /> <span class="sub_ttl">Wifi Connect Section</span> </td> </tr> <tr> <td class="fld_ttl">SSID</td> <td> <input type="text" class="input_l" name="wifi_ssid" id="wifi_ssid"> </td> </tr> <tr> <td class="fld_ttl">Password</td> <td> <input type="text" class="input_l" name="wifi_psk" id="wifi_psk"> </td> </tr> <tr> <td class="fld_ttl">Scan SSID</td> <td> <input type="checkbox" name="wifi_scan_ssid" id="wifi_scan_ssid" value="1"> </td> </tr> <tr> <td class="fld_ttl">Key Mgmt</td> <td> <input type="text" class="input_l" name="wifi_key_mgmt" id="wifi_key_mgmt"><br /> <span style="font-size: 11px;">WPA( set empty ) - WEP( WPA-PSK, WPA-EAP, NONE )</style> </td> </tr> <tr> <td colspan="2"> <input type="button" class="set_button" value="Write" onclick="javascript:setWrite()"> </td> </tr> <tr> <td colspan="2" align="center"> <table> <tr> <td class="fld_ttl">Return Value</td> <td> <div id="result2"></div> </td> </tr> </table> </td> </tr> </table> </center> </body> </html>設定をWrite後、再起動すれば適用されます。
0 件のコメント:
コメントを投稿