Redirecting console output in python

149 views Asked by At

I am working on a project where I am invoking a python script from an Expect script. The python script runs a CLI. I want to change the default behavior of "print" statement to send some string to the Expect script instead of sending it to terminal so that I can perform tasks based on the to be printed string. 1)Is it possible? Currently I am using the following Expect script

spawn -noecho ./add.exp
set id1 $spawn_id
spawn -noecho ./sub.exp
set id2 $spawn_id
spawn -noecho ./mul.exp
set id3 $spawn_id
spawn -noecho ./div.exp
set id4 $spawn_id
spawn -noecho python cli.py
set id $spawn_id
interact {
    -input $id -output $user_spawn_id
    -o
    -input $id "add" {
    return
    send -i $id "add\r"
    -input $id1 -output $id
    -input $id -output $user_spawn_id
    }
    -input $id "sub" {
    return
    send -i $id "sub\r"
    -input $id2 -output $id
    -input $id -output $user_spawn_id
    }
    -input $id "mul" {
    return
    send -i $id "mul\r"
    -input $id3 -output $id
    -input $id -output $user_spawn_id
    }
    -input $id "div" {
    return
    send -i $id "div\r"
    -input $id4 -output $id
    -input $id -output $user_spawn_id
    }
    -input $id -output $user_spawn_id
    -input $user_spawn_id -output $id
}

In the python script I am simply printing the "add" string.

2)If not possible then how can I achieve the above mentioned task?

3)Since I am new to Expect there can be some mistake in my code.

Please help me..

0

There are 0 answers