我有一个 PHP 脚本 (news-generator.php),当我包含它时,它会抓取一堆新闻条目并打印出来。现在,我正在为我的网站 (CGI) 使用 Python。当我使用 PHP 时,我在“新闻”页面上使用了这样的东西:
<?php
print("<h1>News and Updates</h1>");
include("news-generator.php");
print("</body>");
?>
(为了简单起见,我删减了示例。)
有没有办法让 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
最佳答案
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
关于php - 如何在 Python 中包含 PHP 脚本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1060436/