How does http://shell.appspot.com/ execute code online?

I am creating a website that allows you to write Python code on the web, for example http://shell.appspot.com/ or http://ideone.com . Can someone please provide me some help, how can I achieve this?

+5
source share
2 answers

First of all, you can view the source code . The main file is just 321 lines!

It basically stores a separate global dict for each user session, then uses compileit execto run the code and return the result.

# log and compile the statement up front
try:
  logging.info('Compiling and evaluating:\n%s' % statement)
  compiled = compile(statement, '<string>', 'single')
except:
  self.response.out.write(traceback.format_exc())
  return

and

  # run!
  old_globals = dict(statement_module.__dict__)
  try:
    old_stdout = sys.stdout
    old_stderr = sys.stderr
    try:
      sys.stdout = self.response.out
      sys.stderr = self.response.out
      exec compiled in statement_module.__dict__
    finally:
      sys.stdout = old_stdout
      sys.stderr = old_stderr
  except:
    self.response.out.write(traceback.format_exc())
    return

: Google App Engine . pysandbox

+6

python, , , , BaseHTTPServer (http://docs.python.org/library/basehttpserver.html) - ipython (http://ipython.org) python .

+2

All Articles