#!/Library/Frameworks/Python.framework/Versions/Current/bin/python
# L-19 MCS 275 Wed 27 Feb 2008 : run_python.py
import cgi
form = cgi.FieldStorage()
print "Content-Type: text/plain\n"
error = False
try:
   c = form['code'].value
   print "your code is \n\n" + c
except KeyError:
   print "please enter code"
   error = True

if not error:
   file = open('/tmp/code.py','w')
   file.write(c)
   file.close()
   print "\n... running the code ...\n"
   from os import system
   r = 'python /tmp/code.py > /tmp/out'
   s = system(r)
   if s != 0:
      print "the code has an error"
   else:
      file = open('/tmp/out','r')
      s = file.readlines()
      print "the output is \n\n", s[0]
