来自php的python arg值

时间:2014-11-14 21:07:06

标签: php python

我坚持使用python脚本的web界面,问题是我需要从用户那里读取输入,然后在python脚本上将它们作为参数值运行,但它不能正常运行我的代码

的index.php

 <html>
 <body>

 <form action="programa.php" method="post">
   Name: <input type="text" name="longitud"><br>
   E-mail: <input type="text" name="angulo"><br>
   <input type="submit" value="correr">
 </form>

 

programa.php

<?php $longitud=$_POST["longitud"];

    $angulo=$_POST["angulo"]; ?>
    <h1>Corriendo programa con  <?php echo $_POST["longitud"]; ?> mm de longitud<br>
     y  <?php echo $_POST["angulo"]; ?> grados de angulo</h1>
                    <br>
                    <br>
                    <br>
                    <?php 
    $a=exec('sudo python /var/www/python/programa.py $longitud $angulo');
    echo $a;
    ?>

我访问了错误日志并打印了这个

Traceback (most recent call last):
File "/var/www/python/programa.py", line 7, in <module>
script,long_barra,angulo = argv
ValueError: need more than 1 value to unpack
[Mon Nov 10 22:36:53 2014] [error] [client 192.168.1.3] PHP Notice:  Undefined 
index:   longitud in /var/www/Maquina/programa.php on line 22
[Mon Nov 10 22:36:53 2014] [error] [client 192.168.1.3] PHP Notice:  Undefined index: 
angulo in /var/www/Maquina/programa.php on line 24
[Mon Nov 10 22:36:53 2014] [error] [client 192.168.1.3] PHP Notice:  Undefined index: 
longitud in /var/www/Maquina/programa.php on line 25
[Mon Nov 10 22:36:53 2014] [error] [client 192.168.1.3] PHP Notice:  Undefined index: 
angulo in /var/www/Maquina/programa.php on line 26
Traceback (most recent call last):
File "/var/www/python/programa.py", line 7, in <module>
script,long_barra,angulo = argv
ValueError: need more than 1 value to unpack
Traceback (most recent call last):
File "/var/www/python/programa.py", line 7, in <module>
script,long_barra,angulo = argv
ValueError: need more than 1 value to unpack

programa.php上的脚本读取了post值,但没有将argv传递给python脚本,我该如何解决呢?

提前

1 个答案:

答案 0 :(得分:0)

您的参数未传递给python的原因是exec()调用中的单引号。这会向python发送文字字符串“$ longitudinal”和“$ angulo”,而不是所述变量的值。相反,使用双引号,PHP将用它们的值替换变量。

因此,请将exec()电话改为:

$a=exec("sudo python /var/www/python/programa.py {$longitud} {$angulo}");
//      ^                 notice the double quotes                    ^    

在这种情况下,花括号不是必需的,但为了便于阅读,我建议使用它们。

或者,您可以直接从$_POST访问这些值,而无需将值分配给变量,如下所示:

$a=exec("sudo python /var/www/python/programa.py {$_POST['longitud']} {$_POST['angulo']}");