#!/Library/Frameworks/Python.framework/Versions/Current/bin/python # L-37 MCS 275 Wed 14 Apr 2010 : show_hello_world3.py # This CGI script prints the HTML code like in show_hello_world2.html # and as in show_hello_world2.py display the code. import cgi Lv = {'0':'C', '1':'Java', '2':'Python'} def code(L): """ Returns the string for code in the language L. """ if L == '0': c = '#include \n\n' \ + 'int main(void)\n' \ + '{\n' \ + ' printf(\"Hello World!\\n\");\n' \ + ' return 0;\n' \ + '}\n' elif L == '1': c = 'public class HelloWorld\n' \ + '{\n' \ + ' public static void main(String[] args)\n' \ + ' {\n' \ + ' System.out.print(\"Hello World!\");\n' \ + ' System.out.println();\n' \ + ' }\n' \ + '}\n' else: c = 'print \"Hello World!\"' return c def PrintHeader(): """ Prints header of web page. """ print """ MCS 275 Review 2: show hello world """ def PrintForm(): """ Prints the input form to screen. """ print "

Show Hello World!

" print """

See the code to show Hello World! displayed in

""" def PrintCode(L): """ Prints the code to screen. """ print "

Hello World! in %s

" % Lv[L] print "" print "

" def main(): """ prints the form or the code """ print "Content-Type: text/html\n" PrintHeader() form = cgi.FieldStorage() if form.has_key('language'): L = form['language'].value PrintCode(L) else: PrintForm() print "" if __name__=="__main__": main()