使用 PHP 变量执行 Python 脚本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19781768/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Executing Python Script with PHP Variables
提问by Micah
I am writing a simple application that uses information from a form, passes it through $_POST to a PHP script that executes a python script and outputs the results. The problem I am having is that my python script is not actually running with the arguments being passed in.
我正在编写一个简单的应用程序,它使用表单中的信息,通过 $_POST 将其传递给执行 python 脚本并输出结果的 PHP 脚本。我遇到的问题是我的 python 脚本实际上并没有在传入参数的情况下运行。
process3.php file:
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;
?>
Output:
输出:
Start word: dog
End word: cat
Results from wordgame.py...
Number of arguments: 1 arguments. Argument List: ['wordgame2.py']
At the top of my wordgame2.py, I have the following (for debugging purposes):
在我的 wordgame2.py 顶部,我有以下内容(用于调试):
#!/usr/bin/env python
import sys
print 'Number of arguments:', len(sys.argv), 'arguments.'
print 'Argument List:', str(sys.argv)
Why isn't the number of arguments being passed = 3? (Yes, my form does send the data correctly.)
为什么传递的参数数量不是 3?(是的,我的表单确实正确发送了数据。)
Any help is greatly appreciated!
任何帮助是极大的赞赏!
Edit: I might add that it does run when I explicitly tell it the start and end word... something like this:
编辑:我可能会补充说,当我明确告诉它开始和结束词时,它确实会运行......像这样:
$output = passthru('python wordgame2.py cat dog');
echo $output
采纳答案by shad0w_wa1k3r
Update -
更新 -
Now that I am aware of PHP, the mistake lies in using the single-quotes '
. In PHP, single quoted strings are considered literals, PHP does not evaluate the content inside it. However, double quoted "
strings are evaluated and would work as you are expecting them to. This is beautifully summarized in this SO answer. In our case,
现在我知道 PHP,错误在于使用单引号'
. 在 PHP 中,单引号字符串被视为文字,PHP 不会评估其中的内容。但是,双引号"
字符串会被评估,并且会按照您的预期工作。这在这个 SO answer 中得到了很好的总结。在我们的例子中,
$output = passthru("python wordgame2.py $start_word $end_word");
would work, but the following won't -
会工作,但以下不会 -
$output = passthru('python wordgame2.py $start_word $end_word');
Original answer -
原答案——
I think the mistake lies in
我认为错误在于
$output = passthru("python wordgame2.py $start_word $end_word");
Try this
尝试这个
$output = passthru("python wordgame2.py ".$start_word." ".$end_word);
回答by Micah
Thank you for your contributions. I have figured out my problem with this simple fix:
感谢你的贡献。我已经通过这个简单的修复解决了我的问题:
$command = 'python wordgame2.py ' . $start_word . ' ' . $end_word;
$output = passthru($command);
In order for passthru to properly handle the php variables, it needs to be concatenated into the string before executing.
为了让 passthru 正确处理 php 变量,它需要在执行之前连接成字符串。