Python子进程popen管道

时间:2015-09-23 20:45:17

标签: python subprocess popen python-2.4

我正在尝试在python中发出命令并读取输入并将其解析回来。这是我试图发出的命令(例如在命令行上运行时)

-bash-3.2$ echo "passthru TST,1234" | ./vsh -a 127.0.0.1 -p 7000
PASSTHRU TST,1234
udp_send_command: timed out waiting for response
-bash-3.2$

我正在尝试使用python子进程

            vsh_command = ' "passthru ' + cmd + '" | ./vsh -a ' + ip_address + ' -p ' + port
            print vsh_command
            new_command = ['echo', vsh_command]
            print new_command
            proc = subprocess.Popen(new_command,
                    stdout=subprocess.PIPE,
                    stderr=subprocess.PIPE,
                    stdin=subprocess.PIPE,
                    shell=True)
            print '1'
            out_2, err = proc.communicate()
            print out_2
            print '2'
            rcv = out_2.splitlines()
            print '3'
            for r in rcv:
                    print '     ' + r
            print '4'

`

 "passthru TST,1234" | ./vsh -a 127.0.0.1 -p 7000
['echo', ' "passthru TST,1234" | ./vsh -a 127.0.0.1 -p 7000']
1


2
3

4

是|子进程无法执行命令?如果是这样,还有另一种方法可以运行这个shell命令并获得输入吗?

UPDATE :(使用shell = true命令的单个字符串)

`vsh_command = 'echo "passthru ' + cmd + '" | ./vsh -a ' + ip_address + ' -p ' + port
            print vsh_command
            proc = subprocess.Popen(vsh_command,
                    stdout=subprocess.PIPE,
                    stderr=subprocess.PIPE,
                    stdin=subprocess.PIPE,
                    shell=True)
            print '1'
            out_2, err = proc.communicate()
            print out_2
            print '2'
            rcv = out_2.splitlines()
            print '3'
            for r in rcv:
                    print '     ' + r
            print '4'

`

运行(python脚本)

echo "passthru TST,1234" | ./vsh -a 127.0.0.1 -p 7000
1
PASSTHRU TST,1234

2
3
     PASSTHRU TST,1234
4
SUCCESS

仍然不是我想要的,看起来它只是做回声命令而不是管道

编辑#2

            p = Popen(["./vsh", "-a", ip_address, "-p", str(port)],stdin=PIPE, stdout=PIPE)
            out, err =  p.communicate("passthru TST,1234")
            print '----------'
            print out

输出:

-bash-3.2$ python mfd_ascii_test.py
udp_send_command: timed out waiting for response
----------
PASSTHRU TST,1234

-bash-3.2$

out是echo的结果,而不是'udp_send_command:等待响应超时'

3 个答案:

答案 0 :(得分:2)

如果要将输出从一个管道传输到另一个,可以将stdout从一个进程传递到另一个进程的stdin:

org.skife.jdbi.v2.sqlobject.customizers.SingleValueResult

如果你想使用竖线字符,你需要设置shell = True并传递一个字符串:

from subprocess import PIPE,Popen

p1 = Popen(["echo", "passthru TST,1234"],stdout=PIPE)

p2 = Popen(["./vsh", "-a", "127.0.0.1", "-p", "7000"],stdin=p1.stdout,stdout=PIPE,stderr=PIPE)
p1.stdout.close()
out, err = p2.communicate()

答案 1 :(得分:1)

要将字符串作为输入传递给子流程并捕获其输出,可以使用Popen.communicate() method

#!/usr/bin/env python2
from subprocess import Popen, PIPE, STDOUT

p = Popen(["./vsh", "-a", ip_address, "-p", str(port)],
          stdin=PIPE, stdout=PIPE, stderr=STDOUT)
output = p.communicate("passthru " + cmd)[0]

stderr=STDOUT将stderr合并到stdout。

设置stdout=PIPE以捕获标准输出,设置stderr=PIPE以捕获标准输出;否则communicate()会返回相应管道的None。如果进程可以在stdout外部写入,则stderr然后use pexpect or pty + select modules。这是pexpect.runu() and pty.spawn() code examples

答案 2 :(得分:-1)

你可以直接使用subprocess.check_output的字符串命令,如果你设置shell=True这样就更容易了(python 2.7)。

output = subprocess.check_output(" ".join(new_command), shell=True)

或者您可以使用subprocess.Popen(python< 2.7)。

output, err = subprocess.Popen(" ".join(new_command), stdout=subprocess.PIPE, shell=True).communicate()