在PHP服务器上运行Python脚本

Mar*_*kum 5 php python

我正在运行Nginx Web服务器以及PHP-CGI。

我想知道是否有可能在PHP页面内执行Python脚本,从而允许将两种语言组合在一起。我已经尝试过很短一段时间,但是没有用,所以我不确定该怎么做。这是我使用的两个文件:

index.php

<body>

    <p>Hello! Here is a message: <?php exec('python hello.py'); ?></p>

</body>
Run Code Online (Sandbox Code Playgroud)

你好

print 'Hello World!'
Run Code Online (Sandbox Code Playgroud)

任何线索将不胜感激。

Mat*_*dge 5

exec将返回 shell 命令的输出,但您仍然需要将echo其返回到页面。以下代码应该适合您

<body>

    <p>Hello! Here is a message: <?php echo exec('python hello.py'); ?></p>

</body>
Run Code Online (Sandbox Code Playgroud)