使用浏览器中的shell_exec将参数从php传递到python [英] passing arguments from php to python using shell_exec from browser

查看:299
本文介绍了使用浏览器中的shell_exec将参数从php传递到python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的PHP脚本,该脚本将几个变量传递给shell_exec命令并运行python脚本。当我从外壳运行此php脚本时,它工作正常。但是,当我从浏览器运行php脚本时,它将导致服务器崩溃。我已经尝试了exec和shell_exec php函数。为什么无论是从外壳程序还是从浏览器运行脚本都如此重要?

I have a simple php script that passes a couple variables to and runs a python script with the shell_exec command. When I run this php script from the shell it works just fine. However, when I run the php script from the browser, it causes the server to crash. I've tried this with both exec and shell_exec php functions. Why would it matter whether I run the script form the shell or the browser?

记录下来,我可以从浏览器成功运行其他命令,例如 tar- xcpvf path.tar.gz

For the record, I can run other commands from the browser successfully, such as "tar -xcpvf path.tar.gz"

以下是浏览器出现问题的php脚本:

Here is the php script that has trouble from the browser:

    <?php
    $inputs = array(    
    'location' => "Los Angeles",
    'date' => '11/01/2012',
    );
    $cmd = 'python simple.py '.$inputs['location'].' '.$inputs['date'];
    $results = shell_exec($cmd);
    echo $results;      
    ?>

任何帮助将不胜感激。谢谢!

Any help will be much appreciated. Thanks!

推荐答案

没有错误日志,我不能确定,但​​是看起来您没有引用参数。因此,您要传递给shell_exec的实际命令是:

Without the error log, I can't be certain, but it looks like you're not quoting your parameters. So the actual command you're passing into shell_exec is:

python simple.py Los Angeles 11/01/2012

(大概)想要的是这样的:

What you're (presumably) wanting is this:

python simple.py "Los Angeles" "11/01/2012"

要实现这一点,您的$ cmd行应如下所示:

To get that, your $cmd line should look like this:

$cmd = 'python simple.py "'.$inputs['location'].'" "'.$inputs['date'].'"';

这篇关于使用浏览器中的shell_exec将参数从php传递到python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆