php脚本执行python脚本来编写一些文件

时间:2021-11-17 00:00:35

I have a php script that calls a python script which in turn is in charge of writing some files to disk.

我有一个调用python脚本的php脚本,而python脚本又负责将一些文件写入磁盘。

If I execute the php script by entering its url in the web browser it can perform several filesystem related tasks:

如果我通过在Web浏览器中输入其URL来执行php脚本,它可以执行几个与文件系统相关的任务:

  • it's able to create a dir
  • 它能够创建一个目录

  • it's able to chmod the dir
  • 它能够调整目录

but it's not able to execute the python script which would create and write other files.

但它无法执行创建和编写其他文件的python脚本。

The strange thing is that if I run the python script manually as www-data:

奇怪的是,如果我手动运行python脚本作为www-data:

user@host $ sudo su www-data
passwd for sudo:
$ whoami
www-data
$ python my_script.py

It works like a charm.

它就像一个魅力。

I'm assuming the user when I run the script through the browser is www-data. So why is the result in the console any different?

我通过浏览器运行脚本时假设用户是www-data。那么为什么控制台的结果会有所不同呢?


SOLVED:

Python script starts off by importing some modules from my repository, which are appended to the path via .bashrc or .bash_profile on login consoles. These modules were not available from the browser to the user www-data. So adding this to the python script solved it:

Python脚本从我的存储库导入一些模块开始,这些模块通过登录控制台上的.bashrc或.bash_profile附加到路径中。这些模块无法从浏览器提供给用户www-data。所以将它添加到python脚本解决了它:

import sys
sys.path.insert(0, 'path_to_my_modules')

solved it.

I apologize for the question. It didn't bring all the necessary information for users to lead to a solution. I guess the problem was so broad that it was difficult for me to start thinking of where its root was.

我为这个问题道歉。它没有为用户带来所有必要的信息来导致解决方案。我想问题是如此广泛,以至于我很难开始考虑其根源所在。

I lacked a good debugging technique to see what the error was. I commented out all the python script and begun with a simple print 'here'. Uncommenting lines one by one showed me the place where it just didn't print anything anymore (the error was obvious then).

我缺乏一个好的调试技术来查看错误是什么。我注释掉了所有的python脚本,并以简单的'here'开头。逐行取消注释,向我展示了它不再打印任何东西的地方(错误很明显)。

1 个解决方案

#1


1  

First of all, don't assume that the user is www-data. You should get the output from whoami running from the PHP script to see if it is www-data. Also, you need to make sure your script has +x or execute permissions for the user.

首先,不要假设用户是www-data。您应该从PHP脚本运行whoami的输出,看看它是否是www-data。此外,您需要确保您的脚本具有+ x或用户的执行权限。

You should read about execute permissions.

您应该阅读有关执行权限的内容

You need to use chown on your PHP script to change the ownership to the www-data user:

您需要在PHP脚本上使用chown将所有权更改为www-data用户:

sudo chown www-data:www-data yourscript.php

Then you need to give the user execute permissions:

然后你需要赋予用户执行权限:

sudo chmod u+x yourscript.php

#1


1  

First of all, don't assume that the user is www-data. You should get the output from whoami running from the PHP script to see if it is www-data. Also, you need to make sure your script has +x or execute permissions for the user.

首先,不要假设用户是www-data。您应该从PHP脚本运行whoami的输出,看看它是否是www-data。此外,您需要确保您的脚本具有+ x或用户的执行权限。

You should read about execute permissions.

您应该阅读有关执行权限的内容

You need to use chown on your PHP script to change the ownership to the www-data user:

您需要在PHP脚本上使用chown将所有权更改为www-data用户:

sudo chown www-data:www-data yourscript.php

Then you need to give the user execute permissions:

然后你需要赋予用户执行权限:

sudo chmod u+x yourscript.php