John Davidson

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

0 comments
Message:


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.



My Answer:
'file2.php', method: 'POST', data: {input: inputValue}, success: function(response) { console.log(response); } });_file2 :_ $input = $_POST['input']; $param = "python file3.py " . $input; $output = exec($param); echo $output;_file3 :_ import sys input_value = sys.argv[1] # do some processing with input_value print("Processed data")_When I run file1.php and input a value, I expect to see "Processed data" printed in the console. However, I am getting a null value instead. What could be the issue here?

Rate this post

5 of 5 based on 5876 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us