Jeremy Talus
Jeremy Talus

Reputation: 125

python calling command issue

I have a little issue with a python script, the script getdata from some web socket, getting Json value, the result is send to a php script using os.system like that :

os.system('echo \' '+ result +' \' |  php sort.php')

but the result is echoed and not piped to the php script

for debug purpose the php script only do that :

$input = readfile("php://stdin");
$data = json_decode($input, true);
var_dump($data['input']);

any idea where the issue can come from ?

PS : result is a pure json string like this one :

{
    "error_code": 250,
    "user": {
        "id": "1",
        "login": "Talus",
        "password": "",
        "email": "",
        "display_name": ""
    },
    "session": {
        "id": "6",
        "user_owner": "1",
        "token": "3MJ9G53U3292DA4217ECCC201CE06D0B0AB0B5941580BA54",
        "last_activity": "2016-02-16 11:59:02",
        "in_game": "0"
    }
}

Upvotes: 1

Views: 35

Answers (1)

Latheesan
Latheesan

Reputation: 24116

Try this:

import subprocess

proc = subprocess.Popen(['php', '/path/to/sort.php', result],
    stdout=subprocess.PIPE,
    shell=True,
    stdout=subprocess.PIPE)

script_response = proc.stdout.read()

Update

Here's an alternate solution..

Try this with your python script:

subprocess.call(['php', '-f', '/path/to/sort.php', result])

Then update your PHP script like this:

// Only execute via php-cli
if (php_sapi_name() == 'cli')
{
    if ($argc > 1)
    {
        // We use the index 1, because 0 == sort.php
        $result = json_decode($argv[1]);
        
        print_r($result);
    }
    else
    {
        exit('Invalid request @ '. basename(__FILE__) .':'. __LINE__);
    }
}
else
{
    exit('Invalid request @ '. basename(__FILE__) .':'. __LINE__);
}

Upvotes: 1

Related Questions