运行bash命令到python脚本

时间:2016-03-21 15:07:56

标签: python bash shell

如何在python脚本中运行以下命令?

import subprocess
var = subprocess.check_output(echo "show stat" | socat unix-connect:/var/run/haproxy/admin.sock stdio '| grep ' webservers,BACKEND' | cut -d , -f8', shell=True)
var=int(var)

我试过这个

url = "http://examplesite.com/accounts.aspx?page=" & Request.QueryString("page") & "&p=1"

但这不起作用!

1 个答案:

答案 0 :(得分:0)

尝试Popen

from subprocess import Popen, PIPE

p1 = Popen(["echo", "show stat"], stdout=PIPE)
p2 = Popen(["socat", "unix-connect:/var/run/haproxy/admin.sock", "stdio"], stdin=p1.stdout)
p3 = Popen(["grep", "webservers,BACKEND"], stdin=p2.stdout)
p4 = Popen(["cut", "-d", ",", "-f8"], stdin=p3.stdout)