[英]Using subprocess with fab in python
I am new to Python and trying to use subprocess for running a script inside another script. 我是Python的新手,并尝试使用子进程在另一个脚本中运行脚本。 I have found several resources online that were very close but unfortunately couldn't help me run my code without error yet.
我在网上找到了几个非常接近的资源,但遗憾的是无法帮助我运行我的代码而没有错误。
Here is what I want to do: In my script1 (main script), I am creating a fabfile.py (script2). 这是我想要做的:在我的script1(主脚本)中,我正在创建一个fabfile.py(script2)。 This script2 or fabfile.py needs to be executed from script1.
需要从script1执行此脚本2或fabfile.py。 After some research, I figured out that execfile and os.systems are not good options therefore I decided to use subprocess.
经过一番研究,我发现execfile和os.systems不是很好的选择因此我决定使用子进程。 (Reference: How can I make one python file run another? )
(参考: 如何让一个python文件运行另一个? )
Here is what I am doing but not working: 这是我正在做的但没有工作:
from os.path import expanduser
home = expanduser("~")
import os
os.getcwd()
desk = "/Desktop"
path = str(home)+str(desk)
f = open("fabfile.py","w") # Creating a fabfile.py
f.write("from fabric.api import run \ndef task1(): \n run('ls')")
import subprocess
host = raw_input("Enter the host IP with username e.g. root@10.0.0.2:")
p1 = subprocess.Popen(['fab', '-f path/fabfile.py', '-H host'],stdout=subprocess.PIPE)
output = p1.communicate()
print output
Note: In the line 注意:在行中
p1 = subprocess.Popen(['fab', '-f path/fabfile.py', '-H host'],stdout=subprocess.PIPE)
I have tried MANY different formats - quote and double quotes placement, $
and %
for variable etc etc. but none seem to be working. 我尝试了很多不同的格式 - 引用和双引号放置,
$
和%
变量等等,但似乎没有工作。 Any idea what am I doing wrong? 知道我做错了什么吗? The examples I see are usually simple with no variables being used as arguments.
我看到的例子通常很简单,没有变量被用作参数。
Separate flags 单独的旗帜
p1 = subprocess.Popen(['fab', '-f', path+'/fabfile.py', '-H', host],stdout=subprocess.PIPE)
When joining paths, its a better idea to use os.path.join() 加入路径时,最好使用os.path.join()
fab_file = os.path.join(os.path.expanduser("~"), "Desktop", 'fabfile.py')
I figured out that there is one more issue, that is, inputing password. 我发现还有一个问题,就是输入密码。 Because even after correcting variable issue, I am getting error (a different one).
因为即使纠正了变量问题,我也会收到错误(另一个错误)。 I think instead of using fab, I can simply do,
我认为不是使用fab,我可以简单地做,
from subprocess import Popen, PIPE
host = raw_input("Enter the host IP with user e.g. root@10.0.0.2:")
conn1 = Popen(['ssh',host,'ls'], stdin=PIPE)
conn1.communicate('password')
Reference: Use subprocess to send a password 参考: 使用子进程发送密码
Code might not be 100% correct, but something like this should work 代码可能不是100%正确,但这样的事情应该有效
import fabric.api as api
from path.my-other-fabfile import my_ssh_connection_blah
api.ls()
host = raw_input("Enter the host IP with username e.g. root@10.0.0.2:")
env['host']=host
my_ssh_connection_blah()
Python won't interpolate the variables path
and host
for you, you need to do it explicitly. Python不会为您插入变量
path
和host
,您需要明确地执行它。
Change: 更改:
p1 = subprocess.Popen(['fab', '-f path/fabfile.py', '-H host'],stdout=subprocess.PIPE)
to: 至:
p1 = subprocess.Popen(['fab', '-f ' + path + '/fabfile.py', '-H ' + host],stdout=subprocess.PIPE)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.