在运行时添加Python脚本

发布于 2024-09-29 20:06:57 字数 306 浏览 5 评论 0原文

我正在开始使用 python 并致力于一个项目,我希望能够做的一件事是在运行时动态下载并运行脚本。

总体思路是能够连接到服务器,按需下载 python 脚本,然后运行刚刚下载的脚本,而无需重新启动程序或将特定脚本硬编码到程序中。

我正在使用的程序使用 Python 进行脚本编写和执行,使用 C++ 进行其他代码(例如渲染和数学)。

我希望能够使用 python 运行从服务器下载的脚本(例如用户生成的内容)。

我想知道这是否可能,或者我是否应该在这种情况下使用另一种脚本语言(可能是 LUA)?

谢谢, Th3飞男孩

I am beginning python and working on a project, and one thing I would like to be able to do is download and run a script dynamically at runtime.

The general idea is to be able to connect to a server, download a python script on demand, and run that script that was just downloaded without having to restart the program or hard code that specific script in to the program.

The program I'm working with uses Python for scripting and execution, and C++ for the other code such as rendering and math.

I would like to be able to use python to run scripts that have been downloaded from a server (for things such as user generated content).

I was wondering if that was possible, or if I should use another scripting language (possibly LUA) for that situation?

Thanks,
Th3flyboy

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

情话已封尘 2024-10-06 20:06:57

你绝对可以做到这一点。使用urllib下载或生成脚本,然后导入。只要您将脚本下载到 PYTHONPATH,它每次都会发挥作用。

例如:

from urllib import urlretrieve
urlretrieve('http://www.mysite.com/myscript.py', '/home/me/script.py')
import script

您还可以根据模板自行生成脚本,从在线提取数据(可能是 xml 或从数据库)并使用 Python 的文本处理功能进行必要的调整。

You can definitely do this. Use urllib to download or generate the script, then import it. It'll work like a charm every time, as long as you download the script to your PYTHONPATH.

For example:

from urllib import urlretrieve
urlretrieve('http://www.mysite.com/myscript.py', '/home/me/script.py')
import script

You can also generate the script yourself from a template, pulling data from online (perhaps in xml or from a database) and using Python's text processing capabilities to make the necessary adjustments.

顾冷 2024-10-06 20:06:57

这是很有可能的,尽管非常不安全。

您需要使用 urllib2.urlopen 下载,然后使用 < 执行结果a href="http://docs.python.org/reference/simple_stmts.html#the-exec-statement" rel="nofollow">exec。由于安全问题,我不愿意进一步解释。

编辑:有关安全问题的解释。

在 Python (CPython) 中,目前无法拥有沙箱。结果是任何不受信任的代码都可以访问 Python 在您的计算机上(针对特定用户)所具有的所有功能。通过使用专门的用户和更好的操作系统级解决方案可以部分解决这个问题。然而它们很复杂。

沙箱的一个例子是 Java 限制小程序完全访问机器的方式。

Lua 经常用于游戏编程。著名的例子是魔兽世界。主要原因之一是它可以沙箱。决定允许不受信任的代码的功能并不简单,但至少可以比 Python 更容易地保护它。

This is quite possible, though hugely insecure.

You need to download using urllib2.urlopen and then executing the result using exec. I am reluctant to explain further because of the security implications.

Edit: An explanation about the security issues.

In Python (CPython) currently there is no way to have a sandbox. The result is any untrusted code has access to all the capabilities Python has on your machine (for the specific user). This can be partially solved by using a specialised user, and better operating system level solutions. They are complicated, however.

An example of sandboxing is the way Java restrict applets from having total access to the machine.

Lua is frequently used in games programming. The famous example is World of Warcraft. One of the main reasons is that it can be sandboxed. Deciding the capabilities to allow the untrusted code is not simple, but at least it can be secured much easier than Python.

合约呢 2024-10-06 20:06:57

任何允许运行外部脚本的编程语言都应该能够完成您的工作。这实际上意味着所有语言。几乎所有动态语言都可以。

使用 python,有多种方法可以连接到服务器并获取 url。为了执行,您可以使用子进程模块

Any programming language that allows to run an external script should be able to do your job. That practically means all languages. Almost all dynamic languages will do.

With python, there are multitude of ways to connect to a server and fetch the url. For execution, you could use subprocess module

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文