Running a Python script from PHP

后端 未结 9 1978
鱼传尺愫
鱼传尺愫 2020-11-22 00:57

I\'m trying to run a Python script from PHP using the following command:

exec(\'/usr/bin/python2.7 /srv/http/assets/py/switch.py arg1 arg2\');

H

相关标签:
9条回答
  • 2020-11-22 01:06

    I recommend using passthru and handling the output buffer directly:

    ob_start();
    passthru('/usr/bin/python2.7 /srv/http/assets/py/switch.py arg1 arg2');
    $output = ob_get_clean(); 
    
    0 讨论(0)
  • 2020-11-22 01:16

    Tested on Ubuntu Server 10.04. I hope it helps you also on Arch Linux.

    In PHP use shell_exec function:

    Execute command via shell and return the complete output as a string.

    It returns the output from the executed command or NULL if an error occurred or the command produces no output.

    <?php 
    
    $command = escapeshellcmd('/usr/custom/test.py');
    $output = shell_exec($command);
    echo $output;
    
    ?>
    

    In Python file test.py, verify this text in first line: (see shebang explain):

    #!/usr/bin/env python
    

    Also Python file must have correct privileges (execution for user www-data / apache if PHP script runs in browser or curl) and/or must be "executable". Also all commands into .py file must have correct privileges:

    Taken from php manual:

    Just a quick reminder for those trying to use shell_exec on a unix-type platform and can't seem to get it to work. PHP executes as the web user on the system (generally www for Apache), so you need to make sure that the web user has rights to whatever files or directories that you are trying to use in the shell_exec command. Other wise, it won't appear to be doing anything.

    To make executable a file on unix-type platforms:

    chmod +x myscript.py
    
    0 讨论(0)
  • 2020-11-22 01:19

    Inspired by Alejandro Quiroz:

    <?php 
    
    $command = escapeshellcmd('python test.py');
    $output = shell_exec($command);
    echo $output;
    
    ?>
    

    Need to add Python, and don't need the path.

    0 讨论(0)
  • 2020-11-22 01:21

    In my case I needed to create a new folder in the www directory called scripts. Within scripts I added a new file called test.py.

    I then used sudo chown www-data:root scripts and sudo chown www-data:root test.py.

    Then I went to the new scripts directory and used sudo chmod +x test.py.

    My test.py file it looks like this. Note the different Python version:

    #!/usr/bin/env python3.5
    print("Hello World!")
    

    From php I now do this:

    $message = exec("/var/www/scripts/test.py 2>&1");
    print_r($message);
    

    And you should see: Hello World!

    0 讨论(0)
  • 2020-11-22 01:23

    Alejandro nailed it, adding clarification to the exception (Ubuntu or Debian) - I don't have the rep to add to the answer itself:

    sudoers file: sudo visudo

    exception added: www-data ALL=(ALL) NOPASSWD: ALL

    0 讨论(0)
  • 2020-11-22 01:26

    This is so trivial, but just wanted to help anyone who already followed along Alejandro's suggestion but encountered this error:

    sh: blabla.py: command not found

    If anyone encountered that error, then a little change needs to be made to the php file by Alejandro:

    $command = escapeshellcmd('python blabla.py');
    
    0 讨论(0)
提交回复
热议问题