douangzhao4108 2013-07-10 08:12
浏览 70
已采纳

将表单变量从PHP传递给Python(Joomla中的jumi内部)

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")
  • 写回答

2条回答 默认 最新

  • doqvzh345334 2013-07-10 09:37
    关注

    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 to localhost. Executing the call over HTTP would also be a lot more secure than passing the parameters to passthrough() 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):

    python portal-ldap.py --myvar1 nothing --myvar2 nothing
    

    If you decide to keep using passthgough(), do remember to escape all the arguments with escapeshellarg().

    Edit: You could also json_encode($_REQUEST) and pass it as a parameter, then json.loads() it in Python to get the array as a dictionary.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?