【问题标题】:Passing variables with additional parameters from PHP to Python将带有附加参数的变量从 PHP 传递到 Python
【发布时间】:2019-04-11 16:46:39
【问题描述】:

我需要将一个多字参数从 PHP 传递到 Python。

只传递一个单词没有问题,但是,如何添加额外的参数才能成功解决?

我尝试了几种方法来传递附加参数,但都没有成功,它会导致 500 Internal Server Error 或 python 脚本没有响应。使用 2>&1 也不会返回任何错误消息。

有六个实例,其中附加参数是可选的或成功响应所必需的。

我可以发送单字参数:

$command = escapeshellcmd("python /home/nova_api.py 'getwithdrawalhistory' 2>&1");

这将返回正确的响应:

{"items":[],"message":"Your trade history with recent first","page":1,"pages":0,"perpage":100,"status":"success","total_items":0} 

我尝试将附加参数从 PHP 传递到 Python:

$command = escapeshellcmd("python /home/nova_api.py 'getwithdrawalhistory, { \'page\': 1 }' 2>&1");

$command = escapeshellcmd("python /home/nova_api.py 'getwithdrawalhistory, { \'page\': \'1\' }' 2>&1");

$command = escapeshellcmd("python /home/nova_api.py 'getwithdrawalhistory, { \'page\': \"1\" }' 2>&1");

所有这三个结果都是一个空白页 - 没有响应,也没有返回错误。

【问题讨论】:

    标签: php python shell escaping


    【解决方案1】:

    您通过转义做正确的事情,但使用escapeshellcmd 不是您想要的。它没有参数的概念,因此无法按您期望的方式工作。 PHP 为 shell 命令提供a method to quote and escape individual arguments

    <?php
    $cmd = "python";
    $args = [
        "/home/nova_api.py",
        "getwithdrawalhistory, { 'page': 1 }",
    ];
    // just a fancy way of avoiding a foreach loop
    $escaped_args = implode(" ", array_map("escapeshellarg", $args));
    $command = "$cmd $escaped_args 2>&1";
    

    【讨论】:

    • 此代码执行相同的操作,但仍会导致空白页或无响应。我相信这与将整数从 php 传递到 python 有关。作为一个简单的解决方法,我使用了四个步骤; 1 - 在 php \'getwithdrawalhistory\', { \'page\': 1 } 中准备命令,2 - 使用 ('fwrite($fp, 'print api_query( \'getwithdrawalhistory\', { \'page\': 1 } )');') , 3 - 执行命令并将结果返回到 php 文件, 4 - 从 python 文件中删除准备好的命令 from php unset($lines[$last] );.
    猜你喜欢
    • 2019-03-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-09
    相关资源
    最近更新 更多