I need help on this x-x
assuming my php has 3 variables
<?php
$var1 = "1";
$var2 = "2";
$var3 = "3";
?>
So on the "exec" part im a bit stuck and confused, how do i pass those 3 variable into my python script, and how do i receive those 3 variables in my python script?
Jan Hančič
54k17 gold badges99 silver badges101 bronze badges
asked Dec 7, 2012 at 6:50
Natsume
9013 gold badges15 silver badges22 bronze badges
-
my python script will be script to insert data into sqlite, but for this you can just assume the python script is to print out the variables received. I will edit it later :DNatsume– Natsume2012年12月07日 06:55:30 +00:00Commented Dec 7, 2012 at 6:55
1 Answer 1
You just pass them as command line arguments:
exec ( "/path/to/python/script.py $var1 $var2 $var3" );
Then in your Python script you can read them like this:
import sys
print sys.argv[1] # first parameter
print sys.argv[2] # second parameter
print sys.argv[3] # third parameter
Btw: sys.argv[0] contains the name of your script, that is why you start with index 1.
http://www.tutorialspoint.com/python/python_command_line_arguments.htm
answered Dec 7, 2012 at 6:54
Jan Hančič
54k17 gold badges99 silver badges101 bronze badges
Sign up to request clarification or add additional context in comments.
3 Comments
Natsume
If my python script is in the same dir of the php script can i run it like
exec ( "script.py $var1 $var2 $var3" ); ?Jan Hančič
Probably. Why don't you just try it yourself :) ?
KJ9
Hello. is that work? I can't execute python script on server using php variable
default