开发者

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

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


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜