我真的搜索了大約50個相關頁面,但從未見過類似於我的問題的問題。當我按提交按鈕時,它會調用腳本,但腳本返回一個空白頁面,並且我看不到任何文件已上傳。在我的代碼中沒有輸入錯誤,我檢查了好幾次,我真的需要爲我的項目運行此代碼。可能是什麼問題?我在ubuntu下運行的Apache和我的代碼是:Python/CGI - 上傳文件嘗試返回空白頁面
的html代碼:
<html><body>
<form enctype="multipart/form-data" action="save_file.py" method="post">
<p>File: <input type="file" name="file"></p>
<p><input type="submit" value="Upload"></p>
</form>
</body></html>
Python代碼:
#!/usr/bin/env python
import cgi, os
import cgitb; cgitb.enable()
try: #windows needs stdio set for binary mode
import msvcrt
msvcrt.setmode (0, os.O_BINARY)
msvcrt.setmode (1, os.O_BINARY)
except ImportError:
pass
form = cgi.FieldStorage()
#nested FieldStorage instance holds the file
fileitem = form['file']
#if file is uploaded
if fileitem.filename:
#strip leading path from filename to avoid directory based attacks
fn = os.path.basename(fileitem.filename)
open('/files' + fn, 'wb').write(fileitem.file.read())
message = 'The file "' + fn + '" was uploaded successfully'
else:
message = 'No file was uploaded'
print """\
Content-Type: text/html\n
<html><body>
<p>%s</p>
</body></html>
""" % (message,)
錯誤日誌說...? –
我不知道在python cgi中尋找錯誤日誌的位置,你能幫我嗎? – user1433743
無論你的Web服務器放在哪裏。 –