简体   繁体   中英

Python and CGI dropdown

I'm prete new in CGI and python as well. I would like to create SSID select page for raspberry, that when I select SSID and enter password this is stored in wpa_supplicant.conf and restart, but I have a problem to show drop-down box with available SSID's. Drop-down is empty only NA is present, but if I print this in terminal is showing all SSID. This is the code which I use (I found part of that in this forum:) and thx to all)

#!/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()

The line that is causing the problem is

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

If you tail the error log you will see the usual sudo warning messages. Apache2 does not run as user pi but as www-data. Sudo isn't required nor does iwlist scan need it.

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.: 

The above messages are screwing up your HTML. When you ran your script from the command line it was as user pi which can executes sudo without prompts. When the same script is run as a CGI script it is as user www-data and when invoked with sudo it issues the standard sudo warning right in the middle of your HTML output.

Change the above "cmd1 =" line and remove "sudo"

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

It should then work if your HTML is OK (Note you have some indent problems).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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