繁体   English   中英

Python脚本没有用PHP执行

[英]Python script is not executing with PHP

Python命令没有显示任何结果。 它只显示空白。

whoami命令运行成功。 但是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;

?>

蟒蛇:

echo "Hello World"

使用此:( 2>&1 in shell_exec

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

>&是将流重定向到另一个文件描述符的语法 - 0是stdin, 1是stdout, 2是stderr。

2指的是进程的第二个文件描述符,即stderr。

>表示重定向。

&1表示重定向的目标应与第一个文件描述符的位置相同,即stdout。

我测试了你的代码。 你的PHP代码工作,这是错误的python代码。

test.py的内容替换为:

print("Hello World")

如果您使用python3,或:

print "Hello World"

如果你使用python2。

如果你从命令行( php myfile.php )运行你的php脚本,你会看到python解释器引发的无效的php myfile.php错误。
由于您可能在服务器上运行脚本,因此应检查错误日志以查看错误。 错误日志位置取决于您的服务器,例如我的apache 2 localhost服务器位于/var/log/apache2/error.log

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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