开发者

Executing python code from script and getting interpreter-style output

I need to execute code from my python script and take interpreter-style output like it's done here.

I am creating website on GAE using django, it must run user-entered开发者_开发技巧 code and print interpreter-style output as text.


there is code.InteractiveInterpreter available, but I think you can take an inspiration in the following simpler example:

import code

exprs = [
    'd = {}',
    'd',
    'd["x"] = 1',
    'd',
   ]

for e in exprs:
    print '>>> %s' % e
    cmd = code.compile_command(e)
    r = eval(cmd)
    if r:
        print repr(r)

producing the following output:

>>> d = {}
>>> d
{}
>>> d["x"] = 1
>>> d
{'x': 1}


There is an open-source console for app engine that does what you want (if I understood the question correctly). Have a look at it: http://con.appspot.com/console/

Instructions to integrate it with your application are available here.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜