远程运行python脚本的更好方法

2024-05-18 10:08:06 发布

您现在位置:Python中文网/ 问答频道 /正文

我在远程机器上有一个python脚本,我想从本地机器执行它。它需要几个参数,这就是如果我在那台机器上运行它的方法。

python python_parallel.py --num=10 --ssh=/home/user1/path/file.txt

目前,我的本地计算机中有一个运行上述脚本的python代码:

from optparse import OptionParser
parser.add_option("-n", "--num", type="int", dest="num_spice",help="Enter the number")
parser.add_option("-s", "--ssh", dest="ssh_txt",help="Enter the path to the text file")
num_spice=options.num_spice
ssh_txt=options.ssh_txt

(options, args) = parser.parse_args()

os.system('ssh user1@10.100.10.201 python /home/user1/path/python_parallel.py --num=%s --ssh=%s' %(num_spice, ssh_txt) )

有更好的办法吗? 我尝试了这个link的解决方案,但是它给了我一个错误“ImportError:No module named ssh”


Tags: thepathpytxt脚本机器parserhome
2条回答

你考虑过用Fabric吗?它真的很容易使用。

我建议您查看plumbum模块来执行此类操作。

这是一种运行本地命令的非常酷和简单的方法,您可以非常容易地(通过上下文管理器)对远程命令执行同样的操作。

相关问题 更多 >