简体   繁体   中英

Running Python script in PHP: capture all outputs

I have a Python program that has multiple print statements in it. When I execute the program from PHP , the output displayed is just the value printed by the last print statement. Is there a way to capture values printed by all the print statements in the Python script?

PHP code:

<?php
ini_set('display_errors', 1);
$output = exec("python script.py");
echo $output;
?>

Try with shell_exec - Execute command via shell and return the complete output as a string

escapeshellcmd() escapes any characters in a string that might be used to trick a shell command into executing arbitrary commands.

Name your file like- python_script.py and follow the given script-

$command = escapeshellcmd('python_script.py');
$output = shell_exec($command);
echo $output;

Ref# running-a-python-script-from-php

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM