Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

497
Views
python script return null when I execute it with php exec()

Today I have a question for php exec()

I have three files(2 of them are PHP files, the other one is Python). A PHP file(file 1 ) receives an input value and sends it to another PHP file(file2).

file 2 makes a parameter with values it got from file 1. after that It execute python file(file3) with exec() and it also got return data and send it back to file1.

so,

file1 :

  $.ajax({
                    url: "./section_result.php",
                    type: 'post',
                    dataType: 'json',
                    data: { 'search': search, 'target': target, 'keyword' : keyword.val() }
                }).done(function(data){
                    console.log('done...', data)
}

file2:

$search = $_POST['search'];
$target = $_POST['target'];
$keyword = str_replace("\n", ",", trim($_POST['keyword']))
$command = '/home/ubuntu/venv/bin/python /home/ubuntu/pypypython/';

$command = sprintf('%s "%s" "%s"', $command, $keyword, $target);

exec($command, $output);
http_response_code(200);
echo $output[0];

file 3 :

if __name__ == "__main__":
    start_time = time.time()   
    url = sys.argv[1]
    users = [] 
    users = do_something_sympathy(url)    
    driver = get_blog_comment(url)
    something = do_random_info(driver)
    users.extend(do_reply_scrap(driver))
    driver.close()
    driver.quit()
    users = list({user['url']: user for user in users}.values())

    data = { 'title' : blog['title'], 'posted' : blog['posted'], 'social' : users }
    json_data = json.dumps(data, ensure_ascii=False)        
    print(json_data)
    # print(url)
    print("--- Data count : %s" % len(users))
    print("--- elapsed time %s seconds ---" % (time.time() - start_time))

it used to work fine apparently(as far as I heard) but somehow it stops work (return null when I console.log it).

I thought that's due to problem of path or something at the first place. However when I execute python file with FTP environment it worked perfectly fine. (like $ python random_website_comment.py "https://blahblah.com/abadacadabra" "mo")

I was lost but I found a clue after all. my python file has some delay methods such as webdriverWait, sleep et Cetra. and sum of their delay time is over 10seconds. but when I submit input in file 1, it only took a 2 or 3 seconds to get return null.

but what I don't understand is doesn't exec() function wait until it got return data? also my max_execution_time on php.info is 30seconds which is enough to wait til exec() is accomplished.

If you know about how to deal with this issue, please share with me.

thx for reading, your help will be appreciated.

over 4 years ago · Santiago Trujillo
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!