i have problem , i want to passing variable from php to python . why if my word contains space , in python cannt receive this words . Example Code :
<?php
$variable = "Test Variable Contains Space";
echo shell_exec("C:\Users\HP\AppData\Local\Programs\Python\Python37-32\python.exe C:\Users\HP\AppData\Local\Programs\Python\Python37-32\hasil.py $variable ");
?>
in my python code :
import sys
variable = str(sys.argv[1])
print variable
the result i have from this code jut this words "Test" .
2 Answers 2
Bash commands interpret arguments as separated by spaces, therefore, Test Variable Contains Space seems like 4 different arguments. To fix this, you need to surround it with double quotes:
... \"$variable\" ");
Full code:
<?php
$variable = "Test Variable Contains Space";
echo shell_exec("C:\Users\HP\AppData\Local\Programs\Python\Python37-32\python.exe C:\Users\HP\AppData\Local\Programs\Python\Python37-32\hasil.py \"$variable\" ");
?>
answered Nov 26, 2018 at 2:44
Ethan
4,4054 gold badges28 silver badges49 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
Please add "." after url part is completed.
echo shell_exec("C:\Users\HP\AppData\Local\Programs\Python\Python37-32\python.exe C:\Users\HP\AppData\Local\Programs\Python\Python37-32\hasil.py". $variable);
answered Nov 26, 2018 at 4:25
Madhuri Patel
1,24812 silver badges24 bronze badges
Comments
default
escapeshellargis usually the best option. Albeit cmd might mess with it, Python usually employs a standard shell argument tokenizer.