Passing Value from PHP Script to Python Script

passing parameters to python script via php

So obviously you can't drop an array into a string and expect output, but similarly you shouldn't just drop a string into a command either. You need to escape your values to prevent possible problems from arising due to special characters, whitespace etc.

Just run through the array and build a command line string like this:

$item = array(
'--native-libs-dir' => "/home/gdqupqmy/quikklyLib/",
'--type' => "template0001style1",
'--data'=> "12345",
'--join' => "horizontal",
'--output-file' => "/home/gdqupqmy/quikklyLib/saavr-12345.svg"
);
$args = "";
foreach ($item as $k=>$v) {
$args .= " $k " . escapeshellarg($v);
}
$cmd = "/usr/bin/python /full/path/to/quikkly-generate-code.py $args";
$result = exec($cmd, $output, $return);
printf("Command exited with code %d, full output follows: %s", $return, print_r($output,1));

This ensures all values are quoted and escaped. (I've made the assumption that the array keys are fixed values and so are not dangerous, if this is not the case they can be run through escapeshellarg as well.)

passing php variable to python file

Don't put quotes around the function call. That turns it into a literal string, not a call to the function. Windows pathnames don't use a / before the drive letter, so the path should start with C:. And there shouldn't be a space after Project.

echo shell_exec("C:/xampp/htdocs/Ai_Edutech_trial_project/eclipse_workspace/Project/check.py '$para1' '$para2'");

Also, if you're just going to echo the result, you can use the passthru function instead.



Related Topics



Leave a reply



Submit