我也是 CGI 和 python 的新手。我想为覆盆子创建 SSID 选择页面,当我选择 SSID 并输入密码时,它存储在 wpa_supplicant.conf 中并重新启动,但是我无法显示带有可用 SSID 的下拉框。下拉列表是空的,只有 NA 存在,但如果我在终端中打印它会显示所有 SSID。这是我使用的代码(我在这个论坛中找到了其中的一部分:),谢谢大家)
#!/usr/bin/env python3
import os
import sys
import subprocess
import cgi, cgitb
cgitb.enable()
global data
data = []
#interface = "wlan0"
def get_name(cell):
return matching_line(cell,"ESSID:")[1:-1]
rules={"Name":get_name
}
columns=["Name"]
def matching_line(lines, keyword):
# "Returns the first matching line in a list of lines. See match()"
for line in lines:
matching=match(line,keyword)
if matching!=None:
return matching
return None
def match(line,keyword):
line=line.lstrip()
length=len(keyword)
if line[:length] == keyword:
return line[length:]
else:
return None
def parse_cell(cell):
global data
parsed_cell={}
for key in rules:
rule=rules[key]
parsed_cell.update({key:rule(cell)})
data.append(rule(cell))
return parsed_cell
def main():
global data
vsebina ="\t<option value = \"Select SSID\" selected>NA</option>"
cells=[[]]
parsed_cells=[]
cmd1 = ["sudo","iwlist","wlan0","scan"] #["iwlist", interface, "scan"]
proc = subprocess.Popen(cmd1,stdout=subprocess.PIPE, universal_newlines=True)
out, err = proc.communicate()
for line in out.split("\n"):
cell_line = match(line,"Cell ")
if cell_line != None:
cells.append([])
line = cell_line[-27:]
cells[-1].append(line.rstrip())
cells=cells[1:]
for cell in cells:
parsed_cells.append(parse_cell(cell))
#print(data)
for ssi in data:
vsebina = vsebina + "\n\t<option value =\"" + ssi + "\">" + ssi + "</option>"
print("Content-Type: text/html\n\n")
print( """
<html>
<head>
<title>Test CGI Form</title>
</head>
<body>
<h1>SSID select</h1>
<p>Test</p>
<form action = "aa1.py" method = "post">
<select name = "dropdown">
""")
print(vsebina)
print("""
</select>
<input type = "submit" value = "Submit" />
</form>
""")
form = cgi.FieldStorage()
if "dropdown" in form:
command = form["dropdown"].value
print("<p>You select : " + command + "</p>")
print("""
</body>
</html>
""")
main()