繁体   English   中英

Python 和 CGI 下拉菜单

[英]Python and CGI dropdown

我也是 CGI 和 python 的新手。 我想为覆盆子创建 SSID select 页面,当我 select 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()

导致问题的线路是

cmd1 = ["sudo","iwlist","wlan0","scan"] #["iwlist", interface, "scan"]

如果您跟踪错误日志,您将看到通常的 sudo 警告消息。 Apache2 不作为用户 pi 运行,而是作为 www-data 运行。 Sudo 不是必需的, iwlist scan也不需要它。

tail -f /var/log/apache2/error.log
[Mon Jul 27 21:02:51.811904 2020] [cgi:error] [pid 486] [client 192.168.1.200:12152] AH01215:     #1) Respect the privacy of others.: /usr/lib/cgi-bin/wifi.cgi
[Mon Jul 27 21:02:51.813228 2020] [cgi:error] [pid 486] [client 192.168.1.200:12152] AH01215:     #2) Think before you type.: /usr/lib/cgi-bin/wifi.cgi
[Mon Jul 27 21:02:51.815429 2020] [cgi:error] [pid 486] [client 192.168.1.200:12152] AH01215:     #3) With great power comes great responsibility.: 

上面的消息搞砸了你的 HTML。 当您从命令行运行脚本时,它是作为用户 pi 的,它可以在没有提示的情况下执行 sudo。 当相同的脚本作为 CGI 脚本运行时,它是用户 www-data,当使用 sudo 调用时,它会在 HTML output 中间发出标准 sudo 警告。

更改上面的“cmd1 =”行并删除“sudo”

cmd1 = ["iwlist","wlan0","scan"] #["iwlist", interface, "scan"]

如果您的 HTML 正常,它应该可以工作(注意您有一些缩进问题)。

暂无
暂无

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

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