如何在网站上嵌入Python解释器 [英] How to embed a Python interpreter on a website

查看:205
本文介绍了如何在网站上嵌入Python解释器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正尝试建立一个类似于 Codecademy 的教育编码网站,但坦率地说,我很茫然应该采取什么步骤.在Webapp中甚至包括一个简单的python解释器,我是否可以朝正确的方向发展?

I am attempting to build an educational coding site, similar to Codecademy, but I am frankly at a loss as to what steps should be taken. Could I be pointed in the right direction in including even a simple python interpreter in a webapp?

推荐答案

一个选项可能是使用PyPy创建沙盒python.这将限制某人可以执行的外部操作.

One option might be to use PyPy to create a sandboxed python. It would limit the external operations someone could do.

设置完成后,您的网站将获取代码源,并通过ajax将其发送到您的网络服务器,然后服务器将在沙盒化python实例的子进程中运行代码.如果花费的时间超过5秒,您也可以终止该过程.然后,您将输出返回给客户端.

Once you have that set up, your website would take the code source, send it over ajax to your webserver, and the server would run the code in a subprocess of a sandboxed python instance. You would also be able to kill the process if it took longer than say 5 seconds. Then you return the output back as a response to the client.

有关PyPy沙箱的信息,请参见以下链接:
http://doc.pypy.org/en/latest/sandbox.html
http://readevalprint.com/blog/python-sandbox-with-pypy.html

See these links for help on a PyPy sandbox:
http://doc.pypy.org/en/latest/sandbox.html
http://readevalprint.com/blog/python-sandbox-with-pypy.html

创建一个完全交互式的REPL将会更加艰巨.您将需要为服务器上的每个客户端保持一个解释器的生命.然后接受输入的ajax行",并通过与正在运行的进程进行通信来通过interp运行它们,并返回输出.

To create a fully interactive REPL would be even more involved. You would need to keep an interpreter alive to each client on your server. Then accept ajax "lines" of input and run them through the interp by communicating with the running process, and return the output.

总体而言,并非无关紧要.您需要一些强大的开发技能才能轻松地做到这一点.如果您只是在学习,您可能会发现此任务有些艰巨.

Overall, not trivial. You would need some strong dev skills to do this comfortably. You may find this task a bit daunting if you are just learning.

这篇关于如何在网站上嵌入Python解释器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆