PHP 执行 Python“脚本”
这适用于执行操作并返回输出的脚本,但不适用于 CherryPy。
<?php
// execute your Python script from PHP
$command = escapeshellcmd('myPythonScript.py');
$output = shell_exec($command);
echo $output;
// take response content to embed it into the page
?>
Run Code Online (Sandbox Code Playgroud)
PHP 访问 Python/CherryPy 服务的网站
import cherrypy
class HelloWorld(object):
def index(self):
return "Hello World!"
index.exposed = True
cherrypy.quickstart(HelloWorld())
Run Code Online (Sandbox Code Playgroud)
这将启动 ahttp://localhost:8080并且您应该看到Hello world!.
现在您可以通过访问它的 来访问 CherryPy 的输出localhost:port。性能不佳,但有效。
<?php
$output = file_get_contents('http://localhost:8080/');
echo $output;
?>
Run Code Online (Sandbox Code Playgroud)
Joomla + Ajax 访问 Pyhton/CherryPy 服务的网站
另一种解决方案是不使用 PHP 来获取内容,而是从客户端进行获取。基本上,您可以对 CherryPy 提供的网站使用 Ajax 请求,以获取其内容并将其嵌入到 Joomla 提供的页面的 dom 中。
// add jQuery Ajax reqeust from your Joomla page to CherryPy
$.ajax({
url: "https://localhost:8080/", // <-- access the 2nd served website
type: 'GET',
success: function(res) {
//console.log(res);
alert(res);
$("#someElement").html(res);
}
});
Run Code Online (Sandbox Code Playgroud)