Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How is http://shell.appspot.com/ executing code online?

Tags:

python

I'm building a website which allows you to write python code online just like http://shell.appspot.com/ or http://ideone.com. Can someone please provide me some help how I can achieve this?

like image 372
ajay singh Avatar asked Sep 02 '11 09:09

ajay singh


2 Answers

First of all you can browse their source code. The main file is only 321 lines !

Basically it keeps a separate global dict for each user session, then uses compile and exec to 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

Edit: Not using Google App Engine would make things much more complicated.But you can take a look at pysandbox

like image 146
Mihai Stan Avatar answered Oct 31 '22 20:10

Mihai Stan


I am new to python but hopefully following links could help you acheive ur goal, BaseHTTPServer (http://docs.python.org/library/basehttpserver.html) library can be used for handling web requests and ipython (http://ipython.org) for executing python commands at the backend.

like image 42
avasal Avatar answered Oct 31 '22 20:10

avasal