简体   繁体   English

python cgi PermissionError: [Errno 13] 权限被拒绝:

[英]python cgi PermissionError: [Errno 13] Permission denied:

on my macOS, I try to use apache and python cgi to build a very simple web site, where I can upload an image and then output if I succeed or fail.在我的 macOS 上,我尝试使用 apache 和 python cgi 构建一个非常简单的网站,我可以在其中上传图像,然后在成功或失败时输出。 Here is the index.html:这是 index.html:

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>upload</title>
    </head>
<body>
<form enctype="multipart/form-data" action="/CGI-Executables/hello.py" method="post">
<p>upload:<input type="file" name="filename" /></p>
<p><input type="submit" value="upload" /> </p>
</form>
</body>
</html>

Here is the hello.py where I write the python cgi part:这是我编写 python cgi 部分的 hello.py:

#!/anaconda3/bin/python
# -*- coding: UTF-8 -*-
print ('Content-Type: text/html')
print ('')
import cgi,os
import cgitb;cgitb.enable()
form=cgi.FieldStorage()
fileitem=form['filename']

if fileitem.filename:
    fn=os.path.basename(fileitem.filename)
    open('files/'+fn,'wb').write(fileitem.file.read())
    message = "successful"
else:
    message='fail'
    print ("""\
<html>
<head>
<meta charset="utf-8">
<title>test</title>
</head>
<body>
   <p>%s</p>
</body>
</html>
""" % (message,))

In fact, I copy most of the code from http://cgi.tutorial.codepoint.net/file-upload , I have also run chmod 755 hello.py to deal with the permission.其实我从http://cgi.tutorial.codepoint.net/file-upload复制了大部分代码,我也运行了chmod 755 hello.py来处理权限。 However, after I upload a image called "123.png" and click upload button, a error occurs:但是,当我上传名为“123.png”的图像并单击上传按钮后,出现错误:

Traceback (most recent call last):
  File "/Users/chuci/apa/CGI-Executables/hello.py", line 12, in <module>
    open('files/'+fn,'wb').write(fileitem.file.read())
PermissionError: [Errno 13] Permission denied: 'files/123.png'

And I don't know why.我不知道为什么。 In fact, I don't even understand what is "open('files/'+fn,'wb').write(fileitem.file.read())" used for.事实上,我什至不明白“open('files/'+fn,'wb').write(fileitem.file.read())”是用来做什么的。 But if I delete this line, it will give me a blank page.但是如果我删除这一行,它会给我一个空白页。 Please give some advices, thanks!请大家给点建议,谢谢!

The line you mention takes the content of uploaded file and writes it to the server directory with the original filename given by web user.您提到的行获取上传文件的内容,并将其写入服务器目录,并使用 Web 用户提供的原始文件名。

The error has nothing to do with hello.py permissions, in order to fix it you need to make sure files directory exists and has write permission.该错误与 hello.py 权限无关,为了修复它,您需要确保files目录存在并具有写入权限。

However, most likely you need to specify more accurate file / directory names for uploaded file area (for example original web user file name is not the best idea) and add the appropriate write permissions.但是,您很可能需要为上传的文件区域指定更准确的文件/目录名称(例如原始 Web 用户文件名不是最好的主意)并添加适当的写入权限。

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

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