python 远程执行任意python代码 - 可以做到吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/536370/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Execute arbitrary python code remotely - can it be done?
提问by Salim Fadhley
I'm working on a grid system which has a number of very powerful computers. These can be used to execute python functions very quickly. My users have a number of python functions which take a long time to calculate on workstations, ideally they would like to be able to call some functions on a remote powerful server, but have it appear to be running locally.
我正在研究一个具有许多非常强大的计算机的网格系统。这些可用于非常快速地执行 python 函数。我的用户有许多 python 函数,它们在工作站上计算需要很长时间,理想情况下,他们希望能够在远程强大的服务器上调用一些函数,但它似乎在本地运行。
Python has an old function called "apply" - it's mostly useless these days now that python supports the extended-call syntax (e.g. **arguments), however I need to implement something that works a bit like this:
Python 有一个名为“apply”的旧函数——现在 Python 支持扩展调用语法(例如 **arguments),现在它几乎没用了,但是我需要实现一些像这样工作的东西:
rapply = Rapply( server_hostname ) # Set up a connection
result = rapply( fn, args, kwargs ) # Remotely call the function
assert result == fn( *args, **kwargs ) #Just as a test, verify that it has the expected value.
Rapply should be a class which can be used to remotely execute some arbitrary code (fn
could be literally anything) on a remote server. It will send back the result which the rapply
function will return. The "result" should have the same value as if I had called the function locally.
Rapply 应该是一个类,可用于fn
在远程服务器上远程执行一些任意代码(实际上可以是任何代码)。它将发回rapply
函数将返回的结果。“结果”应该具有与我在本地调用该函数相同的值。
Now let's suppose that fn
is a user-provided function I need some way of sending it over the wire to the execution server. If I could guarantee that fn was always something simple it could could just be a string containing python source code... but what if it were not so simple?
现在让我们假设这fn
是一个用户提供的函数,我需要某种方式将它通过网络发送到执行服务器。如果我能保证 fn 总是很简单,它可能只是一个包含 python 源代码的字符串……但如果它不那么简单呢?
What if fn
might have local dependencies: It could be a simple function which uses a class defined in a different module, is there a way of encapsulating fn
and everything that fn
requires which is not standard-library? An ideal solution would not require the users of this system to have much knowledge about python development. They simply want to write their function and call it.
如果fn
可能有本地依赖怎么办:它可能是一个简单的函数,它使用在不同模块中定义的类,是否有封装的方法fn
以及fn
不是标准库的所有要求?理想的解决方案不需要该系统的用户对 Python 开发有太多了解。他们只是想编写他们的函数并调用它。
Just to clarify, I'm not interested in discussing what kind of network protocol might be used to implement the communication between the client & server. My problem is how to encapsulate a function and its dependencies as a single object which can be serialized and remotely executed.
澄清一下,我对讨论可以使用哪种网络协议来实现客户端和服务器之间的通信不感兴趣。我的问题是如何将函数及其依赖项封装为可以序列化和远程执行的单个对象。
I'm also not interested in the security implications of running arbitrary code on remote servers - let's just say that this system is intended purely for research and it is within a heavily firewalled environment.
我也对在远程服务器上运行任意代码的安全影响不感兴趣——我们只是说这个系统纯粹是为了研究,它是在一个防火墙很严密的环境中。
采纳答案by Jim Carroll
回答by S.Lott
It sounds like you want to do the following.
听起来您想要执行以下操作。
Define a shared filesystem space.
Put ALL your python source in this shared filesystem space.
Define simple agents or servers that will "execfile" a block of code.
Your client then contacts the agent (REST protocol with POST methods works well for
this) with the block of code. The agent saves the block of code and does anexecfile
on that block of code.
定义共享文件系统空间。
将您所有的 python 源代码放在这个共享文件系统空间中。
定义将“execfile”一段代码的简单代理或服务器。
然后,您的客户端
使用代码块联系代理(带有 POST 方法的 REST 协议对此很有效)。代理保存代码块并对该execfile
代码块执行操作。
Since all agents share a common filesystem, they all have the same Python library structure.
由于所有代理共享一个公共文件系统,它们都具有相同的 Python 库结构。
We do with with a simple WSGI application we call "batch server". We have RESTful protocol for creating and checking on remote requests.
我们使用一个简单的 WSGI 应用程序,我们称之为“批处理服务器”。我们有用于创建和检查远程请求的 RESTful 协议。
回答by ?ukasz
回答by Ali Afshar
You could use a ready-made clustering solution like Parallel Python. You can relatively easily set up multiple remote slaves and run arbitrary code on them.
您可以使用现成的集群解决方案,如Parallel Python。您可以相对轻松地设置多个远程从站并在其上运行任意代码。
回答by Ash
You could use a SSH connection to the remote PC and run the commands on the other machine directly. You could even copy the python code to the machine and execute it.
您可以使用到远程 PC 的 SSH 连接并直接在另一台机器上运行命令。你甚至可以将 python 代码复制到机器上并执行它。
回答by Amalraj Victory Raja
Syntax:
句法:
cat ./test.py | sshpass -p 'password' ssh user@remote-ip "python - script-arguments-if-any for test.py script"
猫./test.py | sshpass -p 'password' ssh user@remote-ip "python - test.py 脚本的脚本参数-if-any"
1) here "test.py" is the local python script. 2) sshpass used to pass the ssh password to ssh connection
1)这里的“test.py”是本地python脚本。2)sshpass用于将ssh密码传递给ssh连接