0

my php code:

<?php
$data = array('as', 'df', 'gh');
$result=shell_exec("start test.py ".escapeshellarg(json_encode($data)));
?>

test.py code:

import sys
import json
try:
    print json.loads(sys.argv[1])
except:
    print "ERROR"
print
raw_input()

it's failing to try and showing ERROR. while if change my python code to this:

import sys
try:
    data=sys.argv[1]
except:
    print "ERROR"
print data,
raw_input()

i'm getting the output as: [ as , df , gh ]

as i'm passing a json encoded data, i should be able to decode it with python's json.loads() method. but why it's not working?

RatDon
  • 3,403
  • 8
  • 43
  • 85
  • Can you post the output from your PHP code? In other words, print out the exact string that's being passed to the shell. – Jud Nov 19 '13 at 20:42
  • with `echo escapeshellarg(json_encode($data));` i'm getting output `"[ as , df , gh ]"` – RatDon Nov 19 '13 at 20:50
  • That's not valid json. at bare minimum it should be `["af","df","gh"]` after json_encode, and `'["af","df","gh"]'` after escapeshellarg – Marc B Nov 19 '13 at 21:03
  • @MarcB what's the solution. i'm just calling library functions, nothing else. i want to send a associative array created by a html form and pass it to python. but with json encoding not working what can i do? – RatDon Nov 19 '13 at 21:12
  • check into why your json_encode is screwing up so badly. if that's really what yours is producing, you've got a majorly buggy install. – Marc B Nov 19 '13 at 21:13
  • @MarcB with deleting `escapeshellarg` and writing just `echo json_encode($data);` i'm getting a output `{"1":"as","2":"df","3":"gh"}` and in python `{1:as,2:df,3:gh}` for `$data = array("1"=>"as","2"=>"df","3"=>"gh");` – RatDon Nov 19 '13 at 21:20
  • ok. so it's creating a JS object (`{}`) instead of an array (`[]`). Probably because you're quoting your array keys in the original array definition, so that json_encode sees their internal type as strings, meaning it HAS to produce an object. – Marc B Nov 19 '13 at 21:23
  • @MarcB that's how i've to pass an associative array and in python, convert it to an dictionary. instead of keys like `1,2,3` i'll have `"name", "mail"` etc as keys. i guess, problem is with `escapeshellarg()` in windows. i've read some issues. – RatDon Nov 19 '13 at 21:27
  • gotcha. but the shellarg stuff won't matter to python, any escaping done by the shell will only be relevant to the shell, and the escapes/quotes will be gone when the data gets into python. – Marc B Nov 19 '13 at 21:30
  • @MarcB don't know what is the problem. with `shell_exec("start test.py ".json_encode(json_encode($data)));` i'm receiving in python a string `{"1":"as","2":"df","3":"gh"}` for `$data = array('1' => "as",'2' => "df",'3' => "gh");` but not being able to convert that string to a dictionary with ast.literal_eval(). it's producing errors. – RatDon Nov 19 '13 at 21:52

2 Answers2

1

don't know how it's working. but able to tune the program according to what i want.

php code:

<?php
$data = array('1'=>"as",'2'=>"df",'3'=>"gh");
$result=shell_exec("start test.py ".json_encode(json_encode($data)));
?>

test.py:

import sys
import json

try:
    data=sys.argv[1]
except:
    print "ERROR"

print data
dit=json.loads(data)
print dit
print dit['1']
raw_input()

and got the output as required:

{"1":"as","2":"df","3":"gh"}
{u'1': u'as', u'3': u'gh', u'2': u'df'}
as

someone having a good knowledge in these pls explain.

RatDon
  • 3,403
  • 8
  • 43
  • 85
1

The problem is the following:

On Windows, escapeshellarg() removes percent signs, replaces double quotes with spaces and adds double quotes around the string.

-> https://secure.php.net/manual/en/function.escapeshellarg.php

I'd recommend using addslashes(...)

exec("test.py " . addslashes(json_encode($json)) . " 2>&1", $output, $exit_code);
TeNNoX
  • 1,899
  • 3
  • 16
  • 27