-2

I have a database on backendless cloud service which has no support for python.

So i'm establishing the connection between the python code and the database using php files.

The insertion is working fine as there is no response from the php file to the python.

However in the retrieving i need the php file to echo the output and the python script to read this echo value. How can i do this?

Thanks.

user3091639
  • 43
  • 3
  • 9

3 Answers3

0

Use the subprocess module to execute the PHP code from python.

Assuming that your PHP script write to standard output subprocess.check_output() would be the easiest, or you could use subprocess.Popen() if you need better control of the child process.

If you are using Python >= 3.5 then you can use subprocess.run().

mhawke
  • 84,695
  • 9
  • 117
  • 138
0

To read from stdout with python you can use subprocess module

import subprocess
p = subprocess.Popen("ls",stdout=subprocess.PIPE) # in your case, "ls" change with php command
print p.communicate()  # here you will get ls output

But I don't think that this is a good solution to solve your problem. A better solution will be to use php as REST service. And then with python use it as REST.

import json
import urllib2
json.load(urllib2.urlopen("htt://yourphpapplicationaddress.com"))
dpa
  • 427
  • 4
  • 16
0

try to use pipe, for example:

php test.php | grep 'xxx' python text.python

that's it, python can get the echo data of php from sys.argv

Alex Lu
  • 914
  • 7
  • 5