PHP in python through bash
As I am messing around with Python, I wanted to write an application that would have its own web server. I am currently using a written code from this website for http server: Python Web Server and this is the direct link of the script
This script can handle html but I wanted to add php to it too. Since I know that in Ubuntu I can run php commands within the terminal, I wanted to implement the same logic to this script.
I added these lines:
import os
os.system('php '+filepath)
However my plan didn't go as well as planned... The " <? echo 'hello world'; ?>
" that was in my test.php echoed the string to the terminal that ran the webserver.py and it only returned the output message " 0
" to the web browser.
Is there a way to capture the output of the php file into a string in bash so that I can make the python webserver script output that string with os.system
?
I'm using Ubuntu 11.10.
对于这种简单的情况,您可以使用commands
包中的getoutput
。
from commands import getoutput
response = getoutput('php myscript.php')
You should eschew the use of os.system() in favor of the more modern subprocess module.
You might especially look at popen.communicate().
链接地址: http://www.djcxy.com/p/42698.html下一篇: 通过bash在python中使用PHP