我真的搜索了大约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服务器放在哪里。 –