Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I communicate between two python scripts?

Tags:

python

I have a 3d party python script which takes input from the command line. The relevant code from this script (input.py) looks like the following:

import sys

def chooseinput():
    valid_inputs = ('a', 'b')
    inp = raw_input('Enter choice (%s): ' % "/".join(valid_inputs))
    if inp not in valid_inputs:
        sys.stderr.write("Unsupported input %s\n" % inp)
        return
    print 'You chose ' + '\'' + inp + '\''
    return inp

if __name__ == "__main__":
    chooseinput()
    # do something with the input...
    chooseinput()
    # do something with the input...

I'm trying to write another python script (harness.py) to generate the inputs for the above script.

import subprocess

def harness():
    p = subprocess.Popen(['python', 'input.py'], stdin=subprocess.PIPE)
    p.stdin.write('a')
    p.stdin.write('b')

if __name__ == '__main__':
    harness()

From the command line, I run:

$ python harness.py
Enter choice (a/b): Enter choice (a/b): Traceback (most recent call last):
  File "input.py", line 13, in <module>
    chooseinput()
  File "input.py", line 5, in chooseinput
    inp = raw_input('Enter choice (%s): ' % "/".join(valid_inputs))
EOFError: EOF when reading a line

If I only have one input in the first script, then I can make the second script work by removing the second write call. If the first script requires more than one input, then I get the above error.

like image 417
Ravi Avatar asked Dec 07 '25 12:12

Ravi


2 Answers

Try:

p.stdin.write('a\n')
p.stdin.write('b\n')
like image 65
linuts Avatar answered Dec 10 '25 01:12

linuts


linuts answer works well in your simple example, but for the benefit of future readers, I would strongly recommend against using this methodology for communicating between Python scripts.

This method is a throwback to when few other options were available. Pexpect, bless its heart, may indeed be a good program, but it merely puts a happy face on a miserable interface technique. Command-line control such as this is often timing dependent, quickly making it tedious and error-prone. Use it only when you have no other choice.

Python brings many much more powerful methods to scripting. Unless you don't have access to script internals (with Python, you almost always do) you should instead write your harness.py script to import the 3-party script as a library and control it programmatically by calling its methods/functions directly.

Your current predicament may not allow it, but with Python scripts, command-line communication should be the last choice, not the first.

like image 30
JS. Avatar answered Dec 10 '25 02:12

JS.