标签: restrictedpython

RestrictedPython:在用户指定的代码中调用其他函数?

使用Yuri Nudelman 的代码和自定义_import定义来指定要限制的模块是一个很好的基础,但是当自然地调用内部函数时,user_code由于必须将所有内容列入白名单,是否有任何方法可以允许调用其他用户定义的函数?尽管 Jupyter 似乎不能直接嵌入到 Web 界面中,但对其他沙箱解决方案开放。

from RestrictedPython import safe_builtins, compile_restricted
from RestrictedPython.Eval import default_guarded_getitem

def _import(name, globals=None, locals=None, fromlist=(), level=0):
    safe_modules = ["math"]
    if name in safe_modules:
       globals[name] = __import__(name, globals, locals, fromlist, level)
    else:
        raise Exception("Don't you even think about it {0}".format(name))

safe_builtins['__import__'] = _import # Must be a part of builtins

def execute_user_code(user_code, user_func, *args, **kwargs):
    """ Executed user code in restricted env
        Args:
            user_code(str) - String containing the unsafe …
Run Code Online (Sandbox Code Playgroud)

python restrictedpython

6
推荐指数
1
解决办法
1235
查看次数

标签 统计

python ×1

restrictedpython ×1