2

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.

2
  • oh, you are really far away from doing this. Read a little bit about wsgi, cgi and web servers first. Commented Nov 2, 2011 at 21:07
  • I thought so at first too but when I did it like Adam Wagner said it worked like a charm! :)) Commented Nov 2, 2011 at 21:11

2 Answers 2

3

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

from commands import getoutput

response = getoutput('php myscript.php')
Sign up to request clarification or add additional context in comments.

Comments

2

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

You might especially look at popen.communicate().

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.