Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Executing Python Script with PHP Variables

I am writing a simple application that uses information from a form, passes it through $_POST to a PHP script that executes a python script and outputs the results. The problem I am having is that my python script is not actually running with the arguments being passed in.

process3.php file:

<?php
     $start_word = $_POST['start'];
     $end_word = $_POST['end'];
     echo "Start word: ". $start_word . "<br />";
     echo "End word: ". $end_word . "<br />";
     echo "Results from wordgame.py...";
     echo "</br>";
     $output = passthru('python wordgame2.py $start_word $end_word');
     echo $output;
?>

Output:

Start word: dog
End word: cat
Results from wordgame.py...
Number of arguments: 1 arguments. Argument List: ['wordgame2.py']

At the top of my wordgame2.py, I have the following (for debugging purposes):

#!/usr/bin/env python
import sys
print 'Number of arguments:', len(sys.argv), 'arguments.'
print 'Argument List:', str(sys.argv)

Why isn't the number of arguments being passed = 3? (Yes, my form does send the data correctly.)

Any help is greatly appreciated!

Edit: I might add that it does run when I explicitly tell it the start and end word... something like this:

$output = passthru('python wordgame2.py cat dog');
echo $output
like image 799
Micah Avatar asked Nov 05 '13 04:11

Micah


People also ask

Can I run a Python script from PHP?

To run a Python script from PHP, we can use the shell_exec function. $command = escapeshellcmd('/usr/custom/test.py'); $output = shell_exec($command); echo $output; to call escapeshellcmd to escape the command string. Then we call shell_exec to run the $command .

Can we integrate Python with PHP?

You can execute python scripts using the exec() function in your php script.

How to execute a python script in PHP?

To run Python Script in PHP, we use “shell_exec” which returns all of the output streams as a string. The shell executes it, and the result can be returned as a string. It returns an error or no output at all if an empty value is passed. Today we guide you on how to execute a python script in PHP.

What are the requirements to run a PHP script in Python?

Also Python file must have correct privileges(execution for user www-data / apache if PHP script runs in browser or curl) and/or must be "executable". Also all commands into .pyfile must have correct privileges.

How to call a python script from the command line?

We can call the script with the command line program ‘php’, which is nothing but the PHP interpreter. As we can see, the output of Python script test.py was displayed.

How do I run a Python program from PHP server side?

Run a Python program from PHP PHP Server Side Programming Programming Python In PHP, the ‘shell_exec’ function can be used. It can be executed via the shell and the result can be returned as a string.


2 Answers

Update -

Now that I am aware of PHP, the mistake lies in using the single-quotes '. In PHP, single quoted strings are considered literals, PHP does not evaluate the content inside it. However, double quoted " strings are evaluated and would work as you are expecting them to. This is beautifully summarized in this SO answer. In our case,

$output = passthru("python wordgame2.py $start_word $end_word");

would work, but the following won't -

$output = passthru('python wordgame2.py $start_word $end_word');

Original answer -

I think the mistake lies in

$output = passthru("python wordgame2.py $start_word $end_word");

Try this

$output = passthru("python wordgame2.py ".$start_word." ".$end_word);
like image 195
shad0w_wa1k3r Avatar answered Oct 19 '22 14:10

shad0w_wa1k3r


Thank you for your contributions. I have figured out my problem with this simple fix:

$command = 'python wordgame2.py ' . $start_word . ' ' . $end_word;
$output = passthru($command);

In order for passthru to properly handle the php variables, it needs to be concatenated into the string before executing.

like image 38
Micah Avatar answered Oct 19 '22 14:10

Micah