#! /usr/bin/python import cgi, cgitb cgitb.enable() HTML_Header = 'Content-type: text/html\n\n' HTML_file = 'LittleCalc.htm' # constants needed to recognize where the TextArea is, and replace it text_comment_start='' text_comment_end='' text_area_start='' answer_start='' answer_end='' # ------------------------------- Main ------------------------ def Main(): form = cgi.FieldStorage() f=open(HTML_file,'r') html=f.read() f.close() # get commands and calculate answer(s) if 'the_commands' in form: the_commands = form['the_commands'].value the_commands = the_commands.replace('\r','') # remove extraneous \r if the_commands.strip() == '': answer='No commands' else: answer = CalcAnswer(the_commands) else: the_commands='' answer='No commands' # modify the html and print new_commands = text_area_start+the_commands+text_area_end html = StringReplace(html,text_comment_start,text_comment_end,new_commands) html = StringReplace(html,answer_start,answer_end,answer) print HTML_Header+html # ------------------------------- StringReplace ----------------- def StringReplace(the_string, from_after, until_before, with_this): pos1 = the_string.find(from_after) + len(from_after) pos2 = the_string.find(until_before) new_string = the_string[:pos1] + with_this + the_string[pos2:] return new_string # ------------------------------- IsNumber ---------------------- def IsNumber(s): try: f=float(s) return True except: return False # ------------------------------ CalcAnswer --------------------- def CalcAnswer(commands): lines=commands.split('\n') #-------------------------------- # This is where your language interpreter code should go... # ------------------------------- Main()