Ikuti tutorial di Internet dan gunakan Python untuk menulis CGI untuk memuat naik fail, tetapi 500 terus muncul Apa yang berlaku?
fail html:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>文件上传</title>
</head>
<body>
<form enctype="multipart/form-data" action="save_file.py" method="post">
<p>
选中文件: <input type="file" name="filename" />
</p>
<p>
<input type="submit" value="上传"/>
</p>
</form>
</body>
</html>
.py fail
#!/usr/bin/python
# -*- coding:utf-8 -*-
import cgi, os
import cgitb; cgitb.enable()
# 获取文件名
f = cgi.FiledStorage().getvalue('filename')
# 检测文件是否上传
if f is not None:
# 设置文件路径
fn = os.path.basename(f.filename.replace("","/"))
open('/tmp/' + fn,'wb').write(f.file.read())
message = '文件 "' + fn + '" 上传成功'
else:
message = "文件没有上传"
print """
Content-Type:text/html
<html>
<head>
<meta charset="utf-8">
<title>文件上传</title>
</head>
<body>
<p> %s </p>
</body>
</html>
""" % (message,)
Mesej ralat:
[Thu May 11 10:24:46.697197 2017] [http:error] [pid 8254] [client 127.0.0.1:58072] AH02429: Response header name '<!--' contains invalid characters, aborting request, referer: http://studycgi/file/file.html
127.0.0.1 - - [11/May/2017:10:24:41 +0800] "POST /file/save_file.py HTTP/1.1" 500 4063
Tidakkah anda rasa URL dalam mesej ralat anda kelihatan pelik?
Sepatutnya studycgi.com
Bukankah mesej ralat lebih jelas: Nama pengepala respons '<!--' mengandungi aksara tidak sah