How to 'catch' stdin, stdout, stderr of a program in Python?

entropyfeverone :

I have an sql.exe which has a command line interface, takes as input an sql query and prints the results. I need to write a program in Python, which will generate the sql commands, pass them as input to this program and read the output.

For exercise, I have written a toy program in python, adder.py:

if __name__ == '__main__':
    while True:
        try:
            line = input()
            a, b = line.strip().split()
            c = int(a) + int(b)
            print(c)
        except EOFError:
            exit()
        except:
            continue

I have created a in.txt file:

10 5
7 3
1 a
2 1

I execute from cmd: -$ python adder.py < in.txt > out.txt and the out.txt is:

15
10
3

My question is, how can I stream / buffer the input and the output, to constantly communicate with the program, and not have to create and read from files again and again ?

ePi272314 :

Use bash pipes and two different functions in your code

Try to rethink your program with this approach.

./mycode.py -gen | sqlite3 | ./mycode.py -proc
  1. Your program will accept one command-line argument that selects the behavior
    (generate the input or process the output from SQL)

  2. The character | is the pipeline operand in Bash

  3. The function that process the SQL output must read the stdin

Command-line arguments

You must read and validate the command-line arguments and choose to execute two different functions.
See here How to read/process command line arguments?

Bash pipelines

About pipeline in bash

command1 | command2

The standard output of command1 is connected via a pipe to the standard input of command2.
Example: echo "my password" | shasum -a 256

Read from the stdin

See here few examples of how to read the stdin How do you read from stdin?

Example code

See here an example code. Below an example output (tested in macOS and Ubunto using SQLite):

> ./mycode.py -gen
SELECT 0 + 0;
SELECT 1 + 2;
SELECT 2 + 4;
...

> ./mycode.py -gen | sqlite3
0
3
6
...

> # ./mycode.py -proc multiply by 10 the stdin
> ./mycode.py -gen | sqlite3 | ./mycode.py -proc
0
30
60
...

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=194385&siteId=1