control.cgi.in 1.89 KB
Newer Older
1 2
#!{{ python_executable }}

3
import os
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
import cgi
import cgitb
import ConfigParser

cgitb.enable(display=0, logdir="/tmp/cgi.log")

# Headers
print "Content-Type: text/html"
print

# Body content
form = cgi.FieldStorage()
if "password" not in form:
  print """<html>
  <body>
  <h1>This is the monitoring interface</h1>
  <p>Please enter the monitor_password in the next field to access the data</p>
  <form action="/{{ this_filename }}" method="post">
    Password : <input type="password" name="password">
    <input type="submit" value="Access">
  </form></body></html>"""

elif form['password'].value != '{{ password }}':
  print "<html><body><h1>Error</h1><p>Wrong password</p></body></html>"
  
else:
30 31 32 33 34
  config_file = "{{ config_cfg }}"
  if not os.path.exists(config_file):
    print "Your software does <b>not</b> embed 0-knowledge. \
    This interface is useless in this case"
    exit(0)
35
  parser = ConfigParser.ConfigParser()
36
  parser.read(config_file)
37 38 39 40 41 42 43
  if len(form) > 1:
    for name in form:
      if name != 'password':
        parser.set('public', name, form[name].value)
    with open("{{ config_cfg }}", 'w') as file:
      parser.write(file)
  print "<html><body>"
44
  print "<h1>Values that can be defined by the user :</h1>"
45 46 47 48 49 50 51
  print "<form action=\"/control.cgi\" method=\"post\">"
  print "<input type=\"hidden\" name=\"password\" value=\"{{ password }}\">"
  for option in parser.options("public"):
    print "%s : <input type=\"text\" name=\"%s\" \
    value=\"%s\"><br>"% (option, option, parser.get('public', option))
  print "<input type=\"submit\" value=\"Save\"></form>"
  
52
  print "<h1>Other values :</h1>"
53 54 55 56 57 58
  for section in parser.sections():
    if section != 'public':
      for option in parser.options(section):
        print "<b>%s</b> : value=\"%s\"<br>" % (option,
        parser.get(section, option))
  
59
  print "<br><br><p><a href=\"/monitor.cgi\">Monitor interface</a></p>"
60
  print "</body></html>"