monkeywrench
monkeywrench

Reputation: 23

Subprocess writing stdin and reading stdout python 3.4

I am writing a script which would run a Linux command and write a string (up to EOL) to stdin and read a string (until EOL) from stdout. The easiest illustration would be cat - command:

p=subprocess.Popen(['cat', '-'], stdin=subprocess.PIPE, stdout=subprocess.PIPE)
stringin="String of text\n"
p.stdin.write=(stringin)
stringout=p.stout.read()
print(stringout)

I aim to open the cat - process once and use it to write a string multiple times to its stdin every time getting a string from its stdout.

I googled quite a bit and a lot of recipes don't work, because the syntax is incompatible through different python versions (I use 3.4). That is my first python script from scratch and I find the python documentation to be quite confusing so far.

Upvotes: 0

Views: 2026

Answers (2)

monkeywrench
monkeywrench

Reputation: 23

Thank you for your solution Salva. Unfortunately communicate() closes the cat - process. I did not find any solution with subprocess to communicate with the cat - without having to open a new cat - for every call. I found an easy solution with pexpect though:

import pexpect

p = pexpect.spawn('cat -')
p.setecho(False)

def echoback(stringin):
    p.sendline(stringin)
    echoback = p.readline()
    return echoback.decode();

i = 1
while (i < 11):
    print(echoback("Test no: "+str(i)))
    i = i + 1

In order to use pexpect Ubuntu users will have to install it through pip. If you wish to install it for python3.x, you will have to install pip3 (python3-pip) first from the Ubuntu repo.

Upvotes: 2

Salva
Salva

Reputation: 6837

Well you need to communicate with the process:

from subprocess import Popen, PIPE
s = Popen(['cat', '-'], stdin=PIPE, stdout=PIPE, stderr=PIPE)
input = b'hello!' # notice the input data are actually bytes and not text
output, errs = s.communicate(input)

To use unicode strings, you would need to encode() the input and decode() the output:

from subprocess import Popen, PIPE
s = Popen(['cat', '-'], stdin=PIPE, stdout=PIPE, stderr=PIPE)
input = 'España'
output, errs = s.communicate(input.encode())
output, errs = output.decode(), errs.decode()

Upvotes: 0

Related Questions