0

I have an account on Gator. I am trying to run a php script as follows

;$command = escapeshellcmd('./simple.py 2>&1'); $output = shell_exec($command); echo $output

I would like to get as output anything that the python script prints to screen, but the output is empty no matter what I try (I use standard print in python

jkally
  • 794
  • 2
  • 9
  • 35
  • Did you try https://stackoverflow.com/questions/35817074/calling-python-from-within-php-using-shell-exec ? Might be stderr. – riddler Oct 30 '18 at 16:14
  • No. Still no output. btw my python scipt writes to a file. I can read the file in php and return its content, so both scripts run. Just something really dumb about directing the output – jkally Oct 30 '18 at 16:26
  • 1
    it's just one of the hazards of PHP, it does many dumb things. most of the standard functions are broken by design. – Jasen Oct 30 '18 at 19:42

2 Answers2

0

Use my method as a reference.

I have this two files

run.php

mkdir.py

Here, I've created a html page which contains GO button. Whenever you press this button a new folder will be created in directory whose path you have mentioned.

run.php

<html>
 <body>
  <head>
   <title>
     run
   </title>
  </head>

   <form method="post">

    <input type="submit" value="GO" name="GO">
   </form>
 </body>
</html>

<?php
    if(isset($_POST['GO']))
    {
        shell_exec("python /var/www/html/lab/mkdir.py");
        echo"success";
    }
?>

mkdir.py

#!/usr/bin/env python    
import os    
os.makedirs("thisfolder");
SMshrimant
  • 638
  • 9
  • 15
0

$command = escapeshellcmd('./simple.py 2>&1');

escapeshellcmd is a form of surrender, it's php stupidity, it gets used as a DWIM band-aid when the programmer has lost control of the command-line. It's breaking your stderr redirection.

Use escapeshellarg where you need to escape shell stuff.

$command = escapeshellarg('./simple.py') . ' ' . '2>&1';

Jasen
  • 11,837
  • 2
  • 30
  • 48