0

I've got a Django app, which calls a program using subprocess.call(). This program creates a couple of files, which I then use back in my app. The problem is the program doesn't seem to have permission to create files. I also tried calls like subprocess.call(['mkdir','/tmp/myapp']) but the directory was not created. What do I need to do?

This is just with my dev server at the moment, which I invoke with

sudo python manage.py runserver 0.0.0.0:8080

I can run the command from the terminal manually fine.

I can also use subprocess to touch the files, which it does fine, but when it runs the program that accesses the files itself, that program throws an error because it cannot.

4

1 回答 1

1

需要先创建 /tmp/myapp 目录。如果您在 Django 中的视图函数没有向您显示错误,则意味着您没有正确捕获它。尝试这样的事情:

response = HttpResponse()
calltxt = subprocess.call(['mkdir','/tmp/myapp'])
response.content = calltxt
return response

将该函数附加到您的 urls.py 并将其加载到浏览器中。0 表示命令执行成功,1 表示错误(这类似于 Bash 中的 $! 变量)。

我认为情况并非如此,但如果您的 Django 应用程序部署在 Web 服务器中,那么在 Python 环境中创建文件和执行命令的用户就是拥有 Web 服务器守护程序的用户。例如 Apache 中的 www-data。因此,还需要更改此用户的权限,以便在其正常范围之外编写和执行命令。

于 2013-07-19T01:00:30.600 回答