简体   繁体   中英

Python script is not executing with PHP

Python command doesn't showing any result. It just showing Blank.

whoami command is running successfully. But there is some problem with python.

PHP:

<?php

echo 'before<br />';

$my_command = escapeshellcmd('whoami');
$command_output = shell_exec($my_command);
echo $command_output;

echo '<br />&nbsp;New Command<br />';

$my_command = escapeshellcmd('python test.py');
$command_output = shell_exec($my_command);
echo $command_output;

?>

Python:

echo "Hello World"

Use this: ( 2>&1 in shell_exec )

$command_output = shell_exec('python path\test.py 2>&1');
echo $command_output;

>& is the syntax to redirect a stream to another file descriptor - 0 is stdin, 1 is stdout, and 2 is stderr.

2 refers to the second file descriptor of the process, ie stderr.

> means redirection.

&1 means the target of the redirection should be the same location as the first file descriptor, ie stdout.

I tested your code. Your php code works, it's the python code which is wrong.

Replace the content of test.py with:

print("Hello World")

if you use python3, or:

print "Hello World"

if you use python2.

If you run your php script from command line ( php myfile.php ) you would have seen the invalid sintax error raised by the python interpreter.
As you probably run your script on your server, you should check the error logs to see the error. Error log position depends on your server, for example on my apache 2 localhost server is at /var/log/apache2/error.log .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM