3

I need to run a python program with command line argument from my php script

This is my code

<?php
$output=shell_exec('python /path/to/python_from_php.py input1 input2');
echo $output;
?>

I am getting the contents inside shell_exec function as the output. I tried using only

$output=exec('python /path/to/python_from_php.py input1 input2');

I am getting only the second input as my output. But what I want is I should be able to print the input contents through my python script. If I remove the "echo $output" line I am not getting any output. My python code

import sys
a=(sys.argv)
for i in (sys.argv):
 print i
halfpastfour.am
5,9333 gold badges48 silver badges63 bronze badges
asked May 7, 2015 at 11:13
4
  • 3
    Check this question out: stackoverflow.com/questions/19735250/… Commented May 7, 2015 at 11:24
  • tried that its giving the string inside the exec function as the output ,not working Commented May 7, 2015 at 11:30
  • according to @Indra suggestion, seems that you need passthru(link) instead of exec Commented May 7, 2015 at 11:30
  • tried even passthru not making any difference same output Commented May 7, 2015 at 11:33

3 Answers 3

1

this code gives you the output from shell

 <?php
 $output = shell_exec('ls -lart');
 echo "<pre>$output</pre>";
 ?>

from this reference php also ensure php is not running in safe mode but you could try just call the python script from php

 <?php
 $my_command = escapeshellcmd('python path/to/python_file.py');
 $command_output = shell_exec($my_command);
 echo $command_output;
 ?>
answered May 7, 2015 at 11:44
Sign up to request clarification or add additional context in comments.

Comments

0

As OP wants to:

But what I want is I should be able to print the input contents through my python script. If I remove the "echo $output" line I am not getting any output.

should be used passthru, description

<?php
passthru('python /path/to/python_from_php.py input1 input2');
?>

Check, that you php user have access to run python and your python script.

Live demo is here

answered May 7, 2015 at 11:49

Comments

0

One option is to use passthru(), but if you don't want to manage the output buffer, and having the output in an array is desirable, you can add an output parameter to the exec command as so:

<?php
error_reporting(E_ALL);
$command = escapeshellcmd('/usr/bin/python /path/to/python_from_php.py arg1 arg2');
exec($command, $output);
for ($l=0; $l < count($output); ++$l) {
 echo $output[$l]."\n";
};
answered May 7, 2015 at 12:09

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.