stafanovinci
stafanovinci

Reputation: 13

Read output from a Python command in a PHP webapp

I'm trying to read python output from a php webapp.

I'm using $out = shell_exec("./mytest") in the php code to launch the application, and sys.exit("returnvalue") in the python application to return the value.

The problem is that $out doesn't contain my return value. Instead if I try with $out = shell_exec("ls"), $out variable contain the output of ls command.

If I run ./mytest from terminal it works and I can see the output on my terminal.

Upvotes: 1

Views: 83

Answers (1)

Martin Tournoij
Martin Tournoij

Reputation: 27852

sys.exit("returnvalue")

Using a string with sys.exit is used to indicate an error value. So this will show returnvalue in stderr, not stdout. shell_exec() only captures stdout by default.

You probably want to use this in your Python code:

print("returnvalue")
sys.exit(0)

Alternatively, you could also use this in your PHP code to redirect stderr to stdout.

$out = shell_exec("./mytest 2>&1");

(In fact, doing both is probably best, since having stderr disappear can be quite confusing if something unexpected happens).

Upvotes: 1

Related Questions