3

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!

2 Answers 2

10

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

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks LLingas! I assume the code snip should be put in a PHP page, right? Any recommendations for a tool to generate such a page?
0

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.

2 Comments

Thanks jcubic! That would require me to have some knowledge of javascript and/or ajax. Isn't there a framework in Python to do it?
@DgAn You can create CGI script with a form (html), and in response to a form (in python) you can write code that will use the API the same as your cli script.

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.