Running on a local Apache setup on my machine, and I use python outside of Laravel.
Using Laravel 5.4, I have made and registered the command. I can call it fine using php artisan python:printIt.
I am getting a success on running the command. . . but the file is not writing! Here is the console message:
$ php artisan python:printIt In command handle for printIt. Running command. Command is : C:\Users\Cortland\Miniconda3\python.exe C:^\wamp64^\www^\cmrd^\cgi-bin^\py-test^\printer.pySuccess. Here is the return value: This message came from the python script.1
The python script is fine when run through the python shell or command line. Here, must be running the script because it has the correct print message. However, the exec return value is a 1, which is failure.
The failure is confirmed in that no file 'pyfyle.txt' exists.
What am I doing incorrectly, and how can it be fixed? The real application will be more involved, but have to make sure I can operate basic script calls first.
The Laravel command class:
printIt.php
class printIt extends Command
{
protected $signature = 'python:printIt';
protected $description = 'Run the test python command.';
public function __construct()
{
parent::__construct();
}
public function handle()
{
echo "In command handle for printIt.\n";
exec("where python", $path);
$cmd = base_path('cgi-bin\py-test\printer.py');
// "path/to/python command"
$run = $path[0] . ' ' . escapeshellcmd($cmd);
try {
echo "Running command.\n";
echo "Command is : " . $run;
$res = exec($run, $out, $res);
if ($res == 1) {
throw \Exception("Could not run the sript");
} else {
echo "Success. Here is the return value: ", print_r($res);
}
} catch (\Exception $e) {
echo "Error running command. It says " . $e->getMessage();
}
}
}
The python script:
printer.py
with open('pyfyle.txt', 'w', encoding='utf-8') as f:
f.write('new text here.')
f.close
// Edit: error, incorrect call to close: change f.close to f.close()
print("This message came from the python script..")
ls -alshows me-rw-r--r-- 1 Naltroc 197121 150 Jul 1 18:55 printer.py. So that means owner: read write, group: read, others: read if memory serves. In general, Is it safe to set others to execute? The script can only be called through the controller or command interface, so it seems safe . . .