0

Possible Duplicate:
Start local PHP script w/ local Python script

How do you execute a php file and then view the output of it?

os.system("php ./index.php")

Does not work only returns 0

Community
  • 1
  • 1
JamesM-SiteGen
  • 798
  • 2
  • 10
  • 25
  • 2
    possible duplicate of [Start local PHP script w/ local Python script](http://stackoverflow.com/questions/2931061/start-local-php-script-w-local-python-script). Will lead you to http://stackoverflow.com/questions/89228/how-to-call-external-command-in-python/92395#92395 – Gordon Oct 01 '10 at 08:36
  • no i want the output of the file not the error code 0 – JamesM-SiteGen Oct 01 '10 at 08:46
  • So you are saying you have tried *all* of the possible solutions given in the linked duplicates including os.system, os.popen, the subprocess and the command module and it's not working? Are you sure this is not a pebkac error? – Gordon Oct 01 '10 at 09:06
  • reading a file that it outputs to it not good enough as i am working on a python web-server so none of them will work is this case.. unless i don't care about sending the wrong content to the users.. so not enough.. – JamesM-SiteGen Oct 01 '10 at 09:14
  • yet you accepted an answer that is clearly given in the linked answers – Gordon Oct 01 '10 at 09:25
  • What sort of out put are you looking for, would that be on a web page or just on the console. You have to give us enough information to help you. – Helen Neely Oct 01 '10 at 09:17
  • web page is what i was after. – JamesM-SiteGen Oct 01 '10 at 09:19

2 Answers2

2

What you need is the os.popen function. It runs the command and returns the stdout pipe for that commands output. You can also capture the stdin, stderr by using os.popen2, popen3

import os

outp = os.popen('php ./index.php')
text = outp.read() # this will block until php finishes
                   # and returns with all output in text
print text
Rajan
  • 893
  • 1
  • 7
  • 14
0

If you're working under Windows, you can use os.startfile().

Bertolt
  • 846
  • 1
  • 11
  • 26