ale*_*cwh 7 php python scripting integration execution
我有一个PHP脚本(news-generator.php),当我包含它时,它会抓取一堆新闻并打印出来.现在,我正在使用Python作为我的网站(CGI).当我使用PHP时,我在"新闻"页面上使用了类似的内容:
<?php
print("<h1>News and Updates</h1>");
include("news-generator.php");
print("</body>");
?>
Run Code Online (Sandbox Code Playgroud)
(为简单起见,我减少了这个例子.)
有没有办法让Python执行脚本(news-generator.php)并返回可以跨平台工作的输出?那样,我可以这样做:
page_html = "<h1>News and Updates</h1>"
news_script_output = php("news-generator.php") //should return a string
print page_html + news_script_output
Run Code Online (Sandbox Code Playgroud)
nos*_*klo 10
import subprocess
def php(script_path):
p = subprocess.Popen(['php', script_path], stdout=subprocess.PIPE)
result = p.communicate()[0]
return result
# YOUR CODE BELOW:
page_html = "<h1>News and Updates</h1>"
news_script_output = php("news-generator.php")
print page_html + news_script_output
Run Code Online (Sandbox Code Playgroud)