Solved

PHP front end to execute python scripts

Posted on 2009-05-14
7
1,854 Views
Last Modified: 2013-12-12

Hi Experts,
I need some help with a small web front end (PHP) to execute my many python scripts and display the output in the webpage, the problem I cannot get my head around is how i execute the script and map this to the output display.

My small uncompleted example code is below and any help would be greatly appreciated.

cheers

<?PHP
 

$in_scripts     = $_POST['scripts'];

?>

<html><head><title>TEST PHP Front end for python scripts</title>

</head><b>Front end for running python scripts<b><body>

<form action='' method='post'>

</label><br />
 

<?php

        $scripts = array (

                        "pythonscript1",

                        "pythonscript2",

                );

?>Python scripts:&nbsp;
 

        <select name="scripts" id="scripts" value=''>

                        <?php

                                while($nt=each($scripts))

                                {

                                        if ($nt[1] == $in_scripts)

{$scripts_checked="selected";} else {$scripts_checked="";}

                                        echo "<option value='$nt[1]'

$scripts_checked>$nt[1]</option><BR>";}

?>

        </select>

</td>

<BR>

<input type="submit" value="Execute script" />

</form>

<?php

 

if(isset($_REQUEST['SIPT'])) {

  echo <<<END

<pre>Script output 

END;
 

  if ($in_scripts == "pythonscript1") { echo "<BR>pythonscript1";}

    if ($in_scripts == "pythonscript2") { echo "<BR>pythonscript2";}

} 

?>

</body>

</html>

Open in new window

0
Comment
Question by:wayboo
7 Comments
 
LVL 14

Expert Comment

by:shobinsun
ID: 24392359
Hi,

I think you want this:

From the following you get the idea.

Use exec() for running python scripts.

Hope this will help you.

Regards
<?PHP

 

$in_scripts     = $_POST['scripts'];

?>

<html><head><title>TEST PHP Front end for python scripts</title>

</head><b>Front end for running python scripts<b><body>

<form action='' method='post'>

</label><br />

 

<?php

        $scripts = array (

                        "pythonscript1",

                        "pythonscript2",

                );

?>Python scripts: 

 

        <select name="scripts" id="scripts" value=''>

                        <?php

                                while($nt=each($scripts))

                                {

                                        if ($nt[1] == $in_scripts)

{$scripts_checked="selected";} else {$scripts_checked="";}

                                        echo "<option value='$nt[1]'

$scripts_checked>$nt[1]</option><BR>";}

?>

        </select>

</td>

<BR>

<input type="submit" value="Execute script" />

</form>

<?php

 

  if ($in_scripts == "pythonscript1") { 

echo "<BR>pythonscript1";

exec("myprogram.py");

}

    if ($in_scripts == "pythonscript2") { 

echo "<BR>pythonscript2";

exec("myprogram1.py");

}
 

?>

</body>

</html>

Open in new window

0
 

Author Comment

by:wayboo
ID: 24408168
Hi Shobinsun,
Thanks for this but it does not display the output through the webpage, I have made a few modification and get some output but it's not all of it, could you take a look?

cheers

<?PHP
 

$in_scripts     = $_POST['scripts'];

$output = exec("/var/www/testmsan.py");

$output2 = exec("/var/www/testmsan2.py");

?>

<html><head><title>TEST PHP Front end for python scripts</title>

</head><b>Front end for running python scripts<b><body>

<form action='' method='post'>

</label><br />

 

<?php

        $scripts = array (

                        "pythonscript1",

                        "pythonscript2",

                );

?>Python scripts: 

 

        <select name="scripts" id="scripts" value=''>

                        <?php

                                while($nt=each($scripts))

                                {

                                        if ($nt[1] == $in_scripts)

{$scripts_checked="selected";} else {$scripts_checked="";}

                                        echo "<option value='$nt[1]'

$scripts_checked>$nt[1]</option><BR>";}

?>

        </select>

</td>

<BR>

<input type="submit" value="Execute script" />

</form>

<?php
 

  if ($in_scripts == "pythonscript1") { 
 
 

echo "<BR>$output";
 

}

    if ($in_scripts == "pythonscript2") { 
 

echo "<BR>$output2";

}

 

?>

</body>

</html>

Open in new window

0
 

Expert Comment

by:iamkorvus
ID: 24435263
Just try using system() instead of exec() -- it outputs the results by default.  Depending on your output, you may also want to stick it in a <pre>...</pre> block to make sure the output isn't interpreted as HTML if it is not supposed to be.

http://us2.php.net/manual/en/function.system.php
0
 
LVL 4

Accepted Solution

by:
manavsi earned 500 total points
ID: 24439049
well, did u try PiP???

Link: www.csh.rit.edu/~jon/projects/pip/

I think this is what you need... :)

HTH
Manavsi
0
 
LVL 1

Expert Comment

by:tristan625
ID: 24467298
a few pointers hope they help
1) import cgi into your scripts,this way you can directly return the output as desired
2) convert your scripts in web services
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Dictionaries contain key:value pairs. Which means a collection of tuples with an attribute name and an assigned value to it. The semicolon present in between each key and values and attribute with values are delimited with a comma.  In python we can…
Article by: Swadhin
Introduction of Lists in Python: There are six built-in types of sequences. Lists and tuples are the most common one. In this article we will see how to use Lists in python and how we can utilize it while doing our own program. In general we can al…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now