简体   繁体   中英

permissions/user problem when I call a python script from php

I would like to open a python script from php. My php user is "user23" which is the main user account on my Ubuntu 18.04 desktop. start.php :

    <?php 
    echo 'php user: ' . get_current_user();
    $output=shell_exec("python ./test.py");
    echo'<br>python script user: ';
    echo($output);
    echo '<br>Current PHP version: ' . phpversion();
    ?>

test.py:

    import getpass
    import os
    print(getpass.getuser())
    os.mkdir(getpass.getuser())

PHP output if i open http://localhost/start.php with chrome:

php user: user23
python script user: root
Current PHP version: 7.1.32

So the script works, but the 'root' folder is not created. PHP error log:

  File "./test.py", line 4, in <module>
    os.mkdir(getpass.getuser())
OSError: [Errno 13] Permission denied: 'root'

I do not understand why the php and the python users are different ( user23,root ) and how it is possible that the root user has no permission to create the folder ( Errno 13] Permission denied: 'root' ).

if I open the start.php the python user will be user23 and the folder created perfectly.

$ php start.php
php user: user23<br>python script user: user23
<br>Current PHP version: 7.2.19-0ubuntu0.18.04.2

I use xampp if it's matter. I hope you can help me, I'm totally lost right now. Sorry for my english.

也许它可以帮助您使用sudo编写PHP代码


shell_exec('echo "PASS" | sudo -u root -S python .test.py');

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