简体   繁体   English

在网络服务器上使用php运行python脚本

[英]Running a python script with php on a web server

I've got my website hosted at one.com and I'd like to run a simple python script with php that returns 'hello' but I get no result. 我的网站托管在one.com上,我想用php运行一个简单的python脚本,该脚本返回“ hello”,但没有任何结果。 It worked perfectly on local with Mamp. 它与Mamp一起在本地完美运行。 What should I configure for it to work online ? 我应该如何配置才能使其在线工作?

My php code: 我的PHP代码:

 <?php $result = shell_exec('python test.py'); echo $result; ?> 

The python script python脚本

 print('hello') 

First of all make sure that the python script is executable on the server. 首先,请确保python脚本在服务器上可执行。 For safety reasons I would remove the space in the filename as well so make it just test.py for now. 出于安全原因,我也将删除文件名中的空格,因此暂时将其设置为test.py。 You can make it executable by simple changing the permissions using chmod +x test.py 您可以使用chmod +x test.py通过简单地更改权限来使其可执行。

Then, you can try the following lines of code in php 然后,您可以在php中尝试以下代码行

<?php 

$command = escapeshellcmd('/usr/custom/test.py');
$output = shell_exec($command);
echo $output;

?>

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

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