簡體   English   中英

python cgi PermissionError: [Errno 13] 權限被拒絕:

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

在我的 macOS 上,我嘗試使用 apache 和 python cgi 構建一個非常簡單的網站,我可以在其中上傳圖像,然后在成功或失敗時輸出。 這是 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>

這是我編寫 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,))

其實我從http://cgi.tutorial.codepoint.net/file-upload復制了大部分代碼,我也運行了chmod 755 hello.py來處理權限。 但是,當我上傳名為“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'

我不知道為什么。 事實上,我什至不明白“open('files/'+fn,'wb').write(fileitem.file.read())”是用來做什么的。 但是如果我刪除這一行,它會給我一個空白頁。 請大家給點建議,謝謝!

您提到的行獲取上傳文件的內容,並將其寫入服務器目錄,並使用 Web 用戶提供的原始文件名。

該錯誤與 hello.py 權限無關,為了修復它,您需要確保files目錄存在並具有寫入權限。

但是,您很可能需要為上傳的文件區域指定更准確的文件/目錄名稱(例如原始 Web 用戶文件名不是最好的主意)並添加適當的寫入權限。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM