简体   繁体   English

被困在os.rename()

[英]Stuck at os.rename()

i am ubuntu user.. i have a command in php that exec a python file.. the python file is set to executable.. so, my php command is:- 我是ubuntu用户..我在php中有一个命令执行python文件.. python文件设置为可执行..所以,我的PHP命令是: -

 shell_exec("try.py");

the python file is located at desktop.. but the php file is located in www folder.. python文件位于桌面..但PHP文件位于www文件夹..

 /var/www/try.php

in the try.py, i have a code to rename a file on the desktop as follow:- 在try.py中,我有一个代码重命名桌面上的文件如下: -

 print "enter"
 os.rename("a.txt", "b.txt")
 print "exit"

so, the try.py and a.txt are in desktop.. 所以,try.py和a.txt都在桌面上..

my problem is, when i execute the php file, it shows the "enter" only but not with the "exit".. so i guess it cannot execute the os.rename maybe because of the root privilege or anything that i dont know.. i have tried some solutions to disable password for sudo but still i didnt show the "exit". 我的问题是,当我执行php文件时,它只显示“输入”但不显示“退出”..所以我猜它不能执行os.rename可能是因为root权限或我不知道的任何事情。我已经尝试了一些解决方案来禁用sudo的密码,但我仍然没有显示“退出”。

but, if i execute the try.py directly by double click it on the desktop and execute it, the command can be done and the output shows:- 但是,如果我通过在桌面上双击它直接执行try.py并执行它,命令可以完成,输出显示: -

enter
exit

so, anyone knows how to execute it using php? 所以,谁知道如何使用PHP执行它?

您可以使用rename功能直接在php重命名文件。

rename($oldname, $newname);

The problem is when you are doing os.rename("a.txt", "b.txt") it is looking for a.txt in the directory from where the application is running (so it is looking for a.txt in /var/www/ . 问题是当你在做os.rename("a.txt", "b.txt")它正在运行应用程序的目录中查找a.txt (因此它正在/var/www/寻找a.txt/var/www/

You should give both a.txt and b.txt the full path: 你应该给a.txtb.txt两个完整的路径:

os.rename('/home/user/Desktop/a.txt', '/home/user/Desktop/b.txt')

You will also have to make sure that www-data (or whatever user is running Apache) can write to the Desktop directory; 您还必须确保www-data (或任何正在运行Apache的用户)可以写入Desktop目录; however from a security perspective this is a very bad idea - any script running on the server can read the contents of your desktop (and even delete the files). 但从安全角度来看,这是一个非常糟糕的主意 - 服务器上运行的任何脚本都可以读取桌面内容(甚至删除文件)。

finally, i got the solution.. 最后,我得到了解决方案..

to be able to rename the file in desktop using os.rename, we need to edit some of the configurations in sudoers.. 为了能够使用os.rename在桌面上重命名文件,我们需要编辑sudoers中的一些配置..

it involved some steps in visudo and also the php script.. 它涉及visudo的一些步骤以及php脚本..

in visudo (using command "sudo visudo" in terminal:)- 在visudo(在终端使用命令“sudo visudo”) -

#user privilege specification
root All=(ALL:ALL) ALL
www-data ALL=(ALL:ALL) ALL

#includedir /etc/sudoers.d

ubuntu ALL=(ALL) NOPASSWD: ALL //use your username.. mine is ubuntu
www-data ALL=(ALL) NOPASSWD: ALL //the place where i put the php file

in php file:- 在php文件中: -

<?php
$output = exec("sudo python /home/ubuntu/Desktop/[filename.py]");
?>

for python file is just the same.. 对于python文件是一样的..

os.rename("a.txt", "b.txt")

ok.. that is the solution.. now i can run the python script which it will change the name of a file in desktop from php.. 好的..这是解决方案..现在我可以运行python脚本,它将从PHP更改桌面文件的名称..

Thank you for all the comments and suggestion for the solutions.. =)) 感谢您对解决方案的所有意见和建议.. =))

You need something like: 你需要这样的东西:

my_dir = os.path.dirname(os.path.realpath(sys.argv[0]))

That will equate to the directory try.py is actually in. 这将等于try.py实际所在的目录。

Then you can execute: 然后你可以执行:

os.rename(os.path.join(my_dir, 'a.txt'), os.path.join(my_dir, 'b.txt'))

As mentioned above, this isn't necessarily a great idea from a security PoV though, since you have to grant write permissions to the php script. 如上所述,这不一定是安全PoV的好主意,因为你必须授予php脚本的写权限。

I also usually put the my_dir code in an if sys.argv[0]: so that I can still debug from python REPL: 我通常也将my_dir代码放在if sys.argv[0]:这样我仍然可以从python REPL调试:

if sys.argv[0]:
    # running as executable
    my_dir = os.path.dirname(os.path.realpath(sys.argv[0]))
else:
    # imported into interpreter
    my_dir = '.'

That way I can still import the module into REPL to debug it, as long as I'm in the same directory as the file in question. 这样我仍然可以将模块导入REPL进行调试,只要我和相关文件位于同一个目录中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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