I\'m writing a Python program for running user-uploaded arbitrary (and thus, at the worst case, unsafe, erroneous and crashing) code on a Linux server. The security question
Instead of trying to detect when the child process is waiting for an input, you can use the linux script command. From the man page for script:
The script utility makes a typescript of everything printed on your terminal.
You can use it like this if you were using it on a terminal:
$ script -q
So in Python you can try giving this command to the Popen routine instead of just .
Edit: I made the following program:
#include
int main() {
int i;
scanf("%d", &i);
printf("i + 1 = %d\n", i+1);
}
and then ran it as follows:
$ echo 9 > infile
$ script -q output ./a.out < infile
$ cat output
9
i + 1 = 10
So I think it can be done in Python this way instead of using the stdout, stderr and stdin flags of Popen.