Python 写入文件时 CGI 脚本权限被拒绝

Python CGI script permission denied when writing file

我在 /Library/WebServer/CGI-Executables 中有一个名为 test.cgi 的 python 脚本。我在 /Library/WebServer/Documents 中有一个 index.html 文件。我的 html 文件包含一个发送到 CGI 脚本的表单并且工作正常。当我的脚本尝试写入文件时出现以下错误:

无论我指定什么作为输出目录,我都会收到相同的错误消息。我试过更改 cgi-bin 文件夹和脚本的权限,但这也不起作用。有什么建议吗?

看起来您在通往 /Users/user/Documents/pictures/lol.jpg 的过程中的某个时刻没有写入权限 - 您应该相应地修改其中的权限(同时牢记安全隐患)

在 Linux,Web 服务器通常作为非特权用户和组运行。通常是 user=www-data 和 group=www-data,但这取​​决于您的设置。 CGI 继承了这个用户和组。

要将文件创建为 www-data,您需要确保该目录对该用户可写。

一种常见的方法是确保该目录在 www-data 组中并且是可写的。以下命令是示例:

$ chgrp www-data /Users/user/Documents/pictures
$ chmod g+rwx /Users/user/Documents/pictures

这只有在您自己在 www-data 组(或 root)中时才有效。

您可能希望使该目录中的现有文件可写:

$ chgrp www-data /Users/user/Documents/pictures/*
$ chmod g+rw /Users/user/Documents/pictures/*

您还需要检查 /Users/user/Documents/pictures 以上的所有目录是否都可以访问 www-data。所以 chgrp/chmod 如果他们不对任何人开放,他们也是如此。