Is there a way for PHP to pass HTML form variables (POST) directly to a python script called with "passthru" in PHP without PHP having to know the variable names?
Can PHP pass the HTTP POST request to python so that i.e. the cgi
module in python will read it like it was passed "from the web"?
Basic setup: Joomla CMS, with the jumi module.
NB! Python 2.4, PHP 5.3.3 on RedHat 5.9 - only standard packages.
One of the jumi applications is a python script which creates a form, and also handles the POST variables.
Reason: A lot more python knowledge in our shop, and the python script does a lot in the background.
In the jumi PHP script I have:
<?php
$var1 = $_REQUEST['myVar1'];
$var2 = $_REQUEST['myVar2'];
echo passthru('/usr/bin/python /var/www/html/joomla/jumi/portal-ldap.py '.$myVar1.' '.$myVar2);
?>
The python scripts then uses sys.arvg
to process these variables.
It works - by all means, but it also means that any additinal variables must be known to both scripts.
Is there a way for PHP to pass the form variables directly to the python script in a way so that the "cgi" module in Python can process the variables as it would if I ran the python script with a framework/cgi/mod_python/...?
Python handling form variables:
import cgi
form = cgi.FieldStorage()
var1 = form.getvalue("myVar1", "nothing")
var2 = form.getvalue("myVar2", "nothing")
If you absolutely want to read the values through
cgi.FieldStorage
, you could run the python script on an HTTP server, preferably limiting its access tolocalhost
. Executing the call over HTTP would also be a lot more secure than passing the parameters topassthrough()
unparsed.Alternatively, you could change your python script to use the
getopt
module, which makes it easier to define the argument in key-value pairs like this (paths omitted for brevity):If you decide to keep using
passthgough()
, do remember to escape all the arguments withescapeshellarg()
.Edit: You could also
json_encode($_REQUEST)
and pass it as a parameter, thenjson.loads()
it in Python to get the array as a dictionary.