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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With