gpt4 book ai didi

python - http ://shell. appspot.com/执行代码如何在线?

转载 作者:太空狗 更新时间:2023-10-30 00:51:57 25 4
gpt4 key购买 nike

我正在建立一个网站,让您可以在线编写 Python 代码,就像 http://shell.appspot.com/http://ideone.com .有人可以为我提供一些帮助吗?

最佳答案

首先你可以浏览他们的source code .主文件只有 321 行!

基本上它为每个用户 session 保留一个单独的全局字典,然后使用 compileexec 运行代码并返回结果。

# 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

  # 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

关于python - http ://shell. appspot.com/执行代码如何在线?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7281348/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com