public

exec_sandbox.py

  • Download Gist
Execute Python Script in Sandbox
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
import sys
from cStringIO import StringIO
 
sys.path = [
'/opt/pypy-sandbox',
'/opt/pypy-sandbox/lib_pypy',
'/opt/pypy-sandbox/lib-python/modified-2.5.2',
'/opt/pypy-sandbox/lib-python/2.5.2',
'/opt/pypy-sandbox/lib-python/2.5.2/plat-linux2'
]
 
from pypy.translator.sandbox import pypy_interact
 
TIMEOUT = 5
SANDBOX_BIN = 'pypy-sandbox'
 
def exec_sandbox(code):
sandproc = pypy_interact.PyPySandboxedProc(
SANDBOX_BIN,
['-c', code,'--timeout',str(TIMEOUT)]
)
 
try:
code_output = StringIO()
sandproc.interact(stdout=code_output, stderr=code_output)
return code_output.getvalue()
except:
sandproc.kill()
finally:
sandproc.kill()
 
return 'Error, could not evaluate'

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.