【问题标题】:Running a python script with php on a web server在 Web 服务器上使用 php 运行 python 脚本
【发布时间】:2017-11-21 22:15:23
【问题描述】:

我的网站托管在 one.com,我想用 php 运行一个简单的 python 脚本,它返回“hello”,但我没有得到任何结果。 它在本地与 Mamp 完美配合。 我应该如何配置才能使其在线工作?

我的php代码:

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

python 脚本

print('hello')

【问题讨论】:

标签: php python webserver


【解决方案1】:

首先确保 python 脚本在服务器上是可执行的。出于安全原因,我也会删除文件名中的空格,所以现在只使用 test.py。您可以通过使用chmod +x test.py 更改权限来使其可执行

然后,你可以在php中尝试以下几行代码

<?php 

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

?>

【讨论】:

    猜你喜欢
    • 2014-12-03
    • 1970-01-01
    • 1970-01-01
    • 2019-11-17
    • 2017-03-22
    • 1970-01-01
    • 2015-11-11
    • 2015-05-08
    • 2011-09-30
    相关资源
    最近更新 更多