1

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.

1 Answer 1

1
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).

Sign up to request clarification or add additional context in comments.

2 Comments

print("returnvalue") sys.exit(0) doesn't works...$out = shell_exec("./mytest 2>&1") works but it returned only the last line, and, unfortunately, panda3d add 2 messages lines after my return value...
@stafanovinci I don't know your Python code, or your PHP code for that matter. I can't point out any errors in code I don't know about ;-). This is the error in the code that you posted...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.