Page 1 of 1

Pass PHP Variable to python?

Posted: Sat Jul 20, 2019 8:52 pm
by beemurt
Good Evening

I am trying to pass a PHP variable into my python script. However I keep getting the following error.

Traceback (most recent call last):
File "alarmTest.py", line 29, in <module>
currUID = int(sys.argv[1])
IndexError: list index out of range

My PHP excerpt:

Code: Select all


		$command = system("'python alarmTest.py'  > /dev/null 2>&1 &").$UID;
		$output1 = shell_exec($command);
		echo $output1;
My Python excerpt:

Code: Select all

import sys
import os

currUID = int(sys.argv[1])
print (currUID)

sql_Query = ("SELECT * FROM oh_users WHERE id = %s" % currUID)
        cursor = mySQLconnection.cursor()
        cursor.execute(sql_Query)
        telNo = int(cursor.fetchone()[0])  # parse from tuple to int

If I change the index in the python code to - currUID = int(sys.argv[0]) - I get following Error code:

alarmTest.py
('Error while connecting to oh_Database-:', ProgrammingError(1054, "1054 (42S22): Unknown column 'alarmTest.py' in 'where clause'", u'42S22'))

So It seems it is maybe just the way I am trying to pass the Var?

Any help at all is much appreciated

PS. $UID (in the php) is a session variable. I am not sure if that makes a difference? But I can access it in the PHP script so should still be able to pass it?
Bee

Re: Pass PHP Variable to python?

Posted: Sat Jul 20, 2019 9:01 pm
by Andyroo
I would expect the parameters to be in the string

Code: Select all

"python alarmTest.py parm1 parm2 > /dev/null 2>&1"
Not sure though, it’s a long time since I hacked php around :lol:

Re: Pass PHP Variable to python?

Posted: Sat Jul 20, 2019 9:05 pm
by beemurt
Evening Andyroo

Thanks for the reply.

I tried as suggested. No luck. Still the same error.

Re: Pass PHP Variable to python?

Posted: Sat Jul 20, 2019 9:18 pm
by Andyroo
Try the system() command https://www.php.net/manual/en/function. ... ellcmd.php

You may not need the escapeshellcmd part if you trust your data...

Re: Pass PHP Variable to python?

Posted: Sat Jul 20, 2019 9:57 pm
by beemurt
Again no Luck!

Re: Pass PHP Variable to python?

Posted: Sat Jul 20, 2019 10:08 pm
by Andyroo
This looks wrong but voted as a fix here:

Code: Select all

exec ( "/path/to/python/script.py $var1 $var2 $var3" );

Re: Pass PHP Variable to python?

Posted: Sat Jul 20, 2019 11:49 pm
by beemurt
Evening/Morning Andy

I unfortunately cant use just exec as it hangs up my php while python runs and I need it to all run in the background and not interfere with any other processes.

You where right! I have it working now. I had tried so many different things that I had accidentally left in single quotes ('') around the file path which was throwing it off when I tried as you suggested. So you where right the first time! Thank you

Also encase anyone else stumbles across this with the same error, I had to remove the parse in python. It didn't like it. But it seems to be working OK and reading it in as an int anyway which is what i wanted.

Thanks again Andy!

Code below for anyone that needs it

php

Code: Select all

$command = system("python alarmTest.py $UID > /dev/null 2>&1 &");
		$output1 = shell_exec($command);
		echo $output1;
python

Code: Select all

rid = sys.argv[1]

Re: Pass PHP Variable to python?

Posted: Sun Jul 21, 2019 12:55 am
by Andyroo
:)