0

I'm using python version 2.7.9 and when I try reading a line from a Popen process it's stuck until the process ends. How can I read from stdin before it ends?

If the input is '8200' (correct password) then it prints the output. But if the password is changed from '8200' so there is no output, why?

subprocess source code:

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
    char password[10];
    int num;
    do
    {
        printf("Enter the password:");
        scanf("%s", &password);

        num = atoi(password);

        if (num == 8200)
            printf("Yes!\n");
        else
            printf("Nope!\n");
    } while (num != 8200);

    return 0;
}

Python source:

from subprocess import Popen, PIPE

proc = Popen("Project2", shell=True, stdin=PIPE,stdout=PIPE,stderr=PIPE)
#stdout_data = proc.communicate(input='8200\r\n')[0]
proc.stdin.write('123\r\n')
print proc.stdout.readline()
null
  • 5
  • 1
  • 6

1 Answers1

0

If you change your printf to

printf("Enter the password:\n");

and add a flush

fflush (stdout);

the buffer is flushed. Flushing means the data is written even if the buffer is not full yet. we needet to add a \n to force a new line, because python will buffer all input until it reads a \n in

proc.stdout.readline();

in python we added a readline. it then looked like this:

proc = Popen("Project2", shell=True, stdin=PIPE,stdout=PIPE,stderr=PIPE)
proc.stdout.readline()
proc.stdin.write('123\r\n')
print proc.stdout.readline()

this is what is happening:

  1. python runs the subprocess
  2. subprocess write "Enter the password:\n"
  3. python reads the line "Enter the password:" and does nothing with it
  4. python writes "123" to the subprocess
  5. the subprocess reads 123
  6. the subprocess will check if 123 is 8200, which is false and will answer with "Nope!"
  7. "Nope!" is read by python and printed to stdout with the last line of code
Chris K.
  • 1,060
  • 8
  • 10
  • Please try adding fflush(stdout); after printf – Chris K. Apr 22 '15 at 15:31
  • Yes, we are in the direction ..but now the output is: 'Enter the password:' I want it to print :"Yes!" or "Nope!" (I added fflush (stdout); only after the first printf) – null Apr 22 '15 at 15:36
  • You could print the enter password to std err with fprintf (stderr, "Enter password"); and yes and no wit fprintf (stdout, "Yes"); – Chris K. Apr 22 '15 at 15:41
  • I cannot change the source code of the subprocess.. Right now it's just an exercise, I want to attach to another software, so I do not want to make such changes. – null Apr 22 '15 at 15:52
  • how did you add the fflush then? anyways, you could read a line in python without doing anything with it. eg 'proc.stdout.readline()' before proc.stdin.write – Chris K. Apr 22 '15 at 16:08
  • Thank you, the problem was solved. Now what should I do? to mark your first message the as correct? Write what I did to solve the problem? I'm new here and did not really know – null Apr 22 '15 at 16:33
  • This is my first day, too ;-) anyways, do not mark an answer correct that is not. Yes, you can update your question with the answer or if it was one of my suggestions you can tell me which one it was and i will update my answer, which you can then mark as correct. – Chris K. Apr 22 '15 at 16:36
  • @null I agree with Chris that it is fair to let him convert a comment into a question and you mark it as accepted, and possibly upvote. If you solved it by yourself, it is 100% OK to answer your own question and accept it. – Magnilex Apr 22 '15 at 17:03
  • I added fflush (stdout); After all printf. And I did what you said: 'proc.stdout.readline ()' before proc.stdin.write But, I want to know why it solved the problem? – null Apr 22 '15 at 17:14