Reputation: 71
I'm pretty new to Python and Web applications, so I apologize for possible terms/keywords mix up.
I have a Python script that runs a json-rpc server, exporting some management APIs of a device. I've written another Python script to implement a CLI control application (using Python's cmd module). Everything is run in an Ubuntu12.04 machine.
Now, I would like to replace the CLI application with a very simple web page (Is it called a web application? Or a web service? Maybe a WSGI? Something else?). The page should present a way to call an API, with arguments set by the user, and possibly display the result. What's the best way (min learning curve and min development time...) to proceed?
Should I write some PHP code (from searching OS, it seems a popular way to go) and call another Python script from it? Can I do it entirely with Python (probably yes, but will it be easy?)?
Any hints/directions/suggestions will be much appreciated, thanks!
Upvotes: 3
Views: 6916
Reputation: 109
You can run a python script via php, which outputs to the browser.
Basically, you have to call the python script from php this way:
$command = "python /path/to/python_script.py 2>&1";
$pid = popen( $command,"r");
while( !feof( $pid ) )
{
echo fread($pid, 256);
flush();
ob_flush();
usleep(100000);
}
pclose($pid);
Note: if you run any time.sleep()
in your python code, it will not output the results on the browser.
For full codes working, visit How to execute python script from php and show output on browser
Upvotes: 10
Reputation: 66560
You can use javascript and ajax for this if the API and your page are on the same server. There are few json-rpc library written in javascript, the list is on Wikipedia.
Upvotes: 0