【发布时间】:2013-11-05 04:04:36
【问题描述】:
我正在编写一个简单的应用程序,它使用来自表单的信息,通过 $_POST 将其传递给执行 python 脚本并输出结果的 PHP 脚本。我遇到的问题是我的 python 脚本实际上并没有使用传入的参数运行。
process3.php 文件:
<?php
$start_word = $_POST['start'];
$end_word = $_POST['end'];
echo "Start word: ". $start_word . "<br />";
echo "End word: ". $end_word . "<br />";
echo "Results from wordgame.py...";
echo "</br>";
$output = passthru('python wordgame2.py $start_word $end_word');
echo $output;
?>
输出:
Start word: dog
End word: cat
Results from wordgame.py...
Number of arguments: 1 arguments. Argument List: ['wordgame2.py']
在 wordgame2.py 的顶部,我有以下内容(用于调试目的):
#!/usr/bin/env python
import sys
print 'Number of arguments:', len(sys.argv), 'arguments.'
print 'Argument List:', str(sys.argv)
为什么传递的参数数量不 = 3? (是的,我的表单确实正确发送了数据。)
非常感谢任何帮助!
编辑:我可能会补充说,当我明确告诉它开始和结束词时它确实会运行......像这样:
$output = passthru('python wordgame2.py cat dog');
echo $output
【问题讨论】:
-
当你在你的php文件中
echo 'python wordgame2.py $start_word $end_word'时你看到了什么? -
添加该行后,它现在输出: 起始词:狗 结束词:cat 结果来自 wordgame.py... python wordgame2.py dog cat 参数数量:1 个参数。参数列表:['wordgame2.py']
-
我认为错误在于
$output = passthru('python wordgame2.py $start_word $end_word');试试这个$output = passthru('python wordgame2.py '.$start_word." ".$end_word);也许,python 由于 $ 关键字而拒绝参数(或者可能 $ 正在调用 shell 进行变量替换)
标签: php python forms variables python-2.7