简体   繁体   English

从PHP执行Python脚本

[英]Execution of a Python Script from PHP

I am making an android application in which I am first uploading the image to the server and on the server side, I want to execute a Python script from PHP. 我正在制作一个android应用程序,首先将图像上传到服务器,然后在服务器端,我想从PHP执行Python脚本。 But I am not getting any output. 但是我没有任何输出。 When I access the Python script from the command prompt and run python TestCode.py it runs successfully and gives the desired output. 当我从命令提示符下访问Python脚本并运行python TestCode.py它将成功运行并提供所需的输出。 I'm running Python script from PHP using the following command: 我正在使用以下命令从PHP运行Python脚本:

$result = exec('/usr/bin/python /var/www/html/Source/TestCode.py');

echo $result

However, if I run a simple Python program from PHP it works. 但是,如果我从PHP运行一个简单的Python程序,它将起作用。

PHP has the permissions to access and execute the file. PHP有权访问和执行文件。

Is there something which I am missing here? 我这里缺少什么吗?

exec('/usr/bin/python /var/www/html/Source/TestCode.py', $output);

var_dump($output);

2nd Parameter of exec will give output exec的第二个参数将给出输出

EDIT: 编辑:

exec('/usr/bin/python /var/www/html/Source/TestCode.py 2>&1', $output);

2>&1 - redirecting stderr to stdout . 2>&1stderr重定向到stdout Now in case of any error too, $output will be populated. 现在如果出现任何错误,也将填充$output

  1. First Check your python PATH using "which python" command and check result is /usr/bin/python. 首先使用“哪个python”命令检查您的python PATH,检查结果是/ usr / bin / python。

  2. Check your "TestCode.py" if you have written #!/usr/bin/sh than replace it with #!/usr/bin/bash. 如果您已编写#!/ usr / bin / sh,而不是将其替换为#!/ usr / bin / bash,请检查“ TestCode.py”。

  3. Than run these commands exec('/usr/bin/python /var/www/html/Source/TestCode.py', $result); 比运行这些命令exec('/ usr / bin / python /var/www/html/Source/TestCode.py',$ result); echo $result 回声$结果

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

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