如何从PHP文件运行python文件?

时间:2022-06-18 13:32:12

I have python script which I wan to load in my html via .php so, i have added this code in my test.php.

我有python脚本,我要通过.php加载到html中,所以我在我的test.php中添加了这个代码。

<?php
$command = escapeshellcmd("/var/www/html/ledon.py");
$output =  shell_exec($command);
echo $output;
?>

Shell_exec function works as if I put ls -l instead of $command it display all files. So I am not sure what I am doing wrong with escapeshellcmd function. Thanks.

Shell_exec函数的工作方式就像我将ls -l而不是$命令显示所有文件一样。所以我不确定我在逃避现实的功能上做错了什么。谢谢。

3 个解决方案

#1


0  

With Python you have to invoke the interpreter by calling a script with 'python somescript.py'. Keeping in mind that I have very little knowledge of PHP, I would assume that you would need to do: $command = escapeshellcmd("python /var/www/html/ledon.py"); assuming that your shell has a link to wherever Python is installed.

使用Python,您必须通过调用带有“Python somescript.py”的脚本调用解释器。记住,我对PHP的了解很少,我认为您需要做的是:$command = escapeshellcmd(“python /var/www/html/ledon.py”);假设您的shell与安装Python的任何地方都有链接。

#2


0  

From the official doc

从官方文档

Just a quick reminder for those trying to use shell_exec on a unix-type platform and can't seem to get it to work. PHP executes as the web user on the system (generally www for Apache), so you need to make sure that the web user has rights to whatever files or directories that you are trying to use in the shell_exec command. Other wise, it won't appear to be doing anything.

对于那些试图在unix平台上使用shell_exec的人来说,这只是一个快速的提醒,而且似乎无法让它正常工作。PHP作为系统的web用户执行(通常是Apache的www),因此您需要确保web用户有权使用您在shell_exec命令中使用的任何文件或目录。另一个明智的做法是,它似乎不会做任何事情。

Python file should have correct privileges (execution for user www-data / apache if PHP script runs in browser or through curl) and/or must be "executable". Also all commands in .py file must have correct privileges.

Python文件应该具有正确的特权(如果PHP脚本在浏览器中运行,或者通过curl运行)和/或必须是“可执行的”,那么它就可以为用户www-data / apache执行。另外,.py文件中的所有命令都必须具有正确的特权。

You might want to use sudo and add an exception to your script to the sudoers file

您可能想要使用sudo,并向sudoers文件中添加一个异常。

#3


0  

Your shell command is wrong. You need to specify which program to run on the file - i.e. to run it with Python, you need to run it with Python like so:

您的shell命令是错误的。您需要指定要在文件上运行哪个程序——也就是说,要用Python运行它,您需要用Python来运行它:

python /var/www/html/ledon.py

However, you also need to use system rather than shell_exec.

但是,您还需要使用system而不是shell_exec。

Thus, your full PHP code should be:

因此,完整的PHP代码应该是:

<?php
$command = escapeshellcmd("python /var/www/html/ledon.py");
$output =  system($command);
echo $output;
?>

#1


0  

With Python you have to invoke the interpreter by calling a script with 'python somescript.py'. Keeping in mind that I have very little knowledge of PHP, I would assume that you would need to do: $command = escapeshellcmd("python /var/www/html/ledon.py"); assuming that your shell has a link to wherever Python is installed.

使用Python,您必须通过调用带有“Python somescript.py”的脚本调用解释器。记住,我对PHP的了解很少,我认为您需要做的是:$command = escapeshellcmd(“python /var/www/html/ledon.py”);假设您的shell与安装Python的任何地方都有链接。

#2


0  

From the official doc

从官方文档

Just a quick reminder for those trying to use shell_exec on a unix-type platform and can't seem to get it to work. PHP executes as the web user on the system (generally www for Apache), so you need to make sure that the web user has rights to whatever files or directories that you are trying to use in the shell_exec command. Other wise, it won't appear to be doing anything.

对于那些试图在unix平台上使用shell_exec的人来说,这只是一个快速的提醒,而且似乎无法让它正常工作。PHP作为系统的web用户执行(通常是Apache的www),因此您需要确保web用户有权使用您在shell_exec命令中使用的任何文件或目录。另一个明智的做法是,它似乎不会做任何事情。

Python file should have correct privileges (execution for user www-data / apache if PHP script runs in browser or through curl) and/or must be "executable". Also all commands in .py file must have correct privileges.

Python文件应该具有正确的特权(如果PHP脚本在浏览器中运行,或者通过curl运行)和/或必须是“可执行的”,那么它就可以为用户www-data / apache执行。另外,.py文件中的所有命令都必须具有正确的特权。

You might want to use sudo and add an exception to your script to the sudoers file

您可能想要使用sudo,并向sudoers文件中添加一个异常。

#3


0  

Your shell command is wrong. You need to specify which program to run on the file - i.e. to run it with Python, you need to run it with Python like so:

您的shell命令是错误的。您需要指定要在文件上运行哪个程序——也就是说,要用Python运行它,您需要用Python来运行它:

python /var/www/html/ledon.py

However, you also need to use system rather than shell_exec.

但是,您还需要使用system而不是shell_exec。

Thus, your full PHP code should be:

因此,完整的PHP代码应该是:

<?php
$command = escapeshellcmd("python /var/www/html/ledon.py");
$output =  system($command);
echo $output;
?>