georges
georges

Reputation: 175

Python subprocess popen pipe

I am trying to issue a command in python and read the input and parse it back. This is the command i am trying to issue (example when running on the command line)

-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$

I am trying to use python subprocess

            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

Is a | command not possible with subprocess? if so is there another way that i can run this shell command and get the input?

UPDATE: (With single string for shell=true command)

`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'

`

Run (python script)

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

2
3
     PASSTHRU TST,1234
4
SUCCESS

Still not quite what im looking for, looks like its just doing the echo command not the pipe

EDIT #2

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

Output:

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

-bash-3.2$

out is the result of the echo, not the 'udp_send_command: timed out waiting for response'

Upvotes: 1

Views: 5095

Answers (3)

Tibox
Tibox

Reputation: 837

You can use directly a string command with subprocess.check_output if you set shell=True it's easier this way (python 2.7).

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

Or you can use subprocess.Popen (python < 2.7).

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

Upvotes: -1

jfs
jfs

Reputation: 414207

To pass a string as an input to a subprocess and to capture its output, you could use 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 merges stderr into stdout.

Set stdout=PIPE to capture stdout, set stderr=PIPE to capture stderr; otherwise communicate() returns None for the corresponding pipes. If the process may write outside stdout, stderr then use pexpect or pty + select modules. Here's pexpect.runu() and pty.spawn() code examples.

Upvotes: 1

Padraic Cunningham
Padraic Cunningham

Reputation: 180401

If you want to pipe the output from one to the other, you can pipe the stdout from one process to the stdin of another:

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()

If you wanted to use the pipe character, you would need to set shell=True and pass a single string:

from subprocess import check_output

out = check_output('echo "passthru TST,1234" | ./vsh -a 127.0.0.1 -p 7000',shell=True)

Upvotes: 2

Related Questions