Search code examples
phppythonshell-execsys

sys.argv variable passed from shell_exec got shortened


i am having quite a trouble when i want to execute my python script from php shell_exec() as the title says

This is my code
exec.php

<?php
$my_url="http://reviews.femaledaily.com/moisturizer-36/lotions/nature-republic/aloe-vera-92-soothing-gel-06?tab=reviews&cat=&cat_id=0&age=&order=nrd&page=1";
echo shell_exec("python hello.py '".$my_url."'");
?>

hello.py

import sys
x = sys.argv[1]
print(x)

output

'http://reviews.femaledaily.com/moisturizer-36/lotions/nature-republic/aloe-vera-92-soothing-gel-06?tab=reviews

what i want expect from the output

'http://reviews.femaledaily.com/moisturizer-36/lotions/nature-republic/aloe-vera-92-soothing-gel-06?tab=reviews&cat=&cat_id=0&age=&order=nrd&page=1'

Is there any solution to this ? or i should split it into 2 argument ?


Solution

  • Use escapeshellarg() to properly escape the argument to a command.

    echo shell_exec("python hello.py " . escapeshellarg($my_url));