Python Sandbox

158 views
Skip to first unread message

Liang Sun

unread,
Sep 27, 2012, 4:23:43 AM9/27/12
to pyth...@googlegroups.com
以前有用C语言实现过Sandbox,但是最近,想要实现一个Python的Sandbox,主要就是限制运行Python代码,让它们不能访问外部命令,不能访问本地文件,不能访问网络,对于内存和运行时间也可以进行控制。
问题是Python天生不支持Sandbox。
在网上转了一圈,大概有三种实现方法:1.使用Java或其他语言配合Python实现,因为Java有Security包,可以用Jython。 2.可以用操作系统的chroot控制。 3. 可以实现一个虚拟机。
但是对于具体的实现,以及各个方案的比较,他们的优点和缺点,都没有把握,因此在这里请教各位高手,有没有实现过Python Sandbox的?或者可以指点迷津。谢谢!

金浩

unread,
Sep 27, 2012, 5:09:31 AM9/27/12
to pyth...@googlegroups.com
可以参考最初版本的gae的sdk是怎么实现部分限制功能的。

--
邮件来自: `CPyUG`华蟒用户组(中文Python技术邮件列表)
规则: http://code.google.com/p/cpyug/wiki/PythonCn
发言: pyth...@googlegroups.com
退订: python-cn+...@googlegroups.com (向此发空信即退!)
详情: http://code.google.com/p/cpyug/wiki/CpyUg
严正: 理解列表! 智慧提问! http://wiki.woodpecker.org.cn/moin/AskForHelp

jjx

unread,
Sep 27, 2012, 9:55:49 PM9/27/12
to pyth...@googlegroups.com
zope 内部python运行不是受限的吗?易度应该也是参考这个的吧,没研究过,不知道说的对不对

Jingchao Hu

unread,
Sep 27, 2012, 10:12:58 PM9/27/12
to pyth...@googlegroups.com

虽然CPython没有Sandbox,不过Pypy有

http://doc.pypy.org/en/latest/sandbox.html
 
Such a pypy-c is a secure sandbox: it is safe to run any untrusted Python code with it. The Python code cannot see or modify any local file except via interaction with the external process. It is also impossible to do any other I/O or consume more than some amount of RAM or CPU time or real time.

不知道够用不
 
On Thursday, September 27, 2012 4:23:43 PM UTC+8, Liang Sun wrote:

Jingchao Hu

unread,
Sep 27, 2012, 10:20:56 PM9/27/12
to pyth...@googlegroups.com

或者可以看看这个?

https://github.com/haypo/pysandbox


 * Deny access to the file system
 * Deny importing Python modules
 * Deny exiting Python
 * Deny access to stdin, stdout or stderr
 * Deny some builtin symbols like execfile(), reload() or KeyboardInterrupt
 * Deny execution of arbitrary bytecode (creation of arbitrary code object)

Liang Sun

unread,
Sep 28, 2012, 12:19:06 PM9/28/12
to pyth...@googlegroups.com
多谢各位,那个PyPy的Sandbox听起来不错啊, 我准备试试那个。

在 2012年9月28日星期五UTC+8上午10时20分56秒,Jingchao Hu写道:
Reply all
Reply to author
Forward
0 new messages