Arda
Arda

Reputation: 10929

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.

Upvotes: 2

Views: 159

Answers (2)

gomad
gomad

Reputation: 1039

You should eschew the use of os.system() in favor of the more modern subprocess module.

You might especially look at popen.communicate().

Upvotes: 2

Adam Wagner
Adam Wagner

Reputation: 16107

You can use getoutput from the commands package for this simple case.

from commands import getoutput

response = getoutput('php myscript.php')

Upvotes: 3

Related Questions