在Python中嵌入Python [英] Embedding Python in Python

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

问题描述



任何人都知道在python中嵌入python的好方法吗?


现在在你告诉我'傻了之前,让我解释一下/>
我想做什么。


我想允许用户定义的可编写脚本的对象。我想要给他们修改我课程的部分。

我想禁止访问其他的

模块。


任何想法/例子?


-Robey


Anyone know a good way to embed python within python?

Now before you tell me that''s silly, let me explain
what I''d like to do.

I''d like to allow user-defined scriptable objects. I''d
like to give them access to modify pieces of my classes.
I''d like to disallow access to pretty much the rest of
the modules.

Any ideas/examples?

-Robey

推荐答案

你可能想要这样的东西:


globalDict = {}

exec(stringOfPythonCodeFromUser,globalDict)


globalDict现在是

stringOfPythonCodeFromUser中的任何内容的全局命名空间,因此您可以从中获取值并且选择性地导入

它们进入你的命名空间。


2004年8月18日星期三下午02:26:00 -0500,Robey Holderith写道:
You probably want something like this:

globalDict = {}
exec(stringOfPythonCodeFromUser, globalDict)

globalDict is now the global namespace of whatever was in
stringOfPythonCodeFromUser, so you can grab values from that and
selectivly import them into your namespace.

On Wed, Aug 18, 2004 at 02:26:00PM -0500, Robey Holderith wrote:

任何人知道在python中嵌入python的好方法吗?

现在在你告诉我'傻了之前,让我解释一下我想做什么。

我想允许用户定义的脚本化对象。我想让他们有机会修改我的课程。
我想不允许访问其余的模块。
<

-Robey

Anyone know a good way to embed python within python?

Now before you tell me that''s silly, let me explain
what I''d like to do.

I''d like to allow user-defined scriptable objects. I''d
like to give them access to modify pieces of my classes.
I''d like to disallow access to pretty much the rest of
the modules.

Any ideas/examples?

-Robey



Robey Holderith< robey@slash_dev_slash_random.org>写道:
Robey Holderith <robey@slash_dev_slash_random.org> writes:
任何人都知道在python中嵌入python的好方法吗?


No.

我想允许用户定义的可编写脚本的对象。我想让他们有机会修改我的课程。
我想不允许访问模块的其余部分。


在旧版本的b / b
版本的Python中有一个名为rexec / Bastion的功能,但由于它不安全而被删除了。

任何想法/例子?
Anyone know a good way to embed python within python?
No.
I''d like to allow user-defined scriptable objects. I''d
like to give them access to modify pieces of my classes.
I''d like to disallow access to pretty much the rest of
the modules.
There was a feature called rexec/Bastion for that purposes in older
version of Python, but it was removed because it was insecure.
Any ideas/examples?




在一个单独的过程(或单独的计算机)中运行敏感的东西

并允许敌意客户通过套接字进行沟通。



Run your sensitive stuff in a separate process (or separate computer)
and allow the hostile clients to communicate through sockets.


2004年8月18日星期三14:35:21 -0400,Phil Frost写道:
On Wed, 18 Aug 2004 14:35:21 -0400, Phil Frost wrote:
你可能想要这样的东西:

globalDict = {}
exec(stringOfPythonCodeFromUser,globalDict)

globalDict现在是 stringOfPythonCodeFromUser,所以你可以从中获取值,然后选择性地将它们导入你的命名空间。


所以使用它(带一点额外的读数)它看起来像我

可以做到这一点:


globalDict = {''__ builtin__'':< my modules here>}

exec(< pythonCodeFromUser>,globalDict)


这将禁止导入新模块并直接访问我的命名空间。然而它将允许访问


这是否安全?


保罗,您对此有何看法?


-Robey

2004年8月18日星期三下午02:26:00 -0500,Robey Holderith写道:
You probably want something like this:

globalDict = {}
exec(stringOfPythonCodeFromUser, globalDict)

globalDict is now the global namespace of whatever was in
stringOfPythonCodeFromUser, so you can grab values from that and
selectivly import them into your namespace.

So using this (with a little additional reading) it looks like I
can do this:

globalDict = {''__builtins__'': <my modules here>}
exec(<pythonCodeFromUser>, globalDict)

And that this will disallow both importing of new modules and direct
access to my namespace. It will however allow access to the

Would this be secure?

Paul, what''s your take on this?

-Robey
On Wed, Aug 18, 2004 at 02:26:00PM -0500, Robey Holderith wrote:

任何人都知道在python中嵌入python的好方法吗?

现在在你告诉我'愚蠢之前,让我解释一下我想做什么。

我想允许用户定义的脚本化对象。我想让他们有机会修改我的课程。
我想不允许访问其余的模块。

任何想法/例子?
-Robey

Anyone know a good way to embed python within python?

Now before you tell me that''s silly, let me explain
what I''d like to do.

I''d like to allow user-defined scriptable objects. I''d
like to give them access to modify pieces of my classes.
I''d like to disallow access to pretty much the rest of
the modules.

Any ideas/examples?

-Robey



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

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