【发布时间】:2014-04-21 13:04:05
【问题描述】:
浏览器显示代码而不是运行 python 脚本。
HTML代码如下:
<html><body>
<form enctype="multipart/form-data" action="/static/savefiles.py" method="post">
<p>File: <input type="file" name="file"></p>
<p><input type="submit" value="Upload"></p>
</form>
</body></html>
Python代码如下:
#!c:\Python27\python.exe
#!/usr/bin/python
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
import cgi, os
import cgitb; cgitb.enable()
try: # Windows needs stdio set for binary mode.
import msvcrt
msvcrt.setmode (0, os.O_BINARY) # stdin = 0
msvcrt.setmode (1, os.O_BINARY) # stdout = 1
except ImportError:
pass
form = cgi.FieldStorage()
# A nested FieldStorage instance holds the file
fileitem = form['file']
# Test if the file was uploaded
if fileitem.filename:
# strip leading path from file name to avoid directory traversal attacks
fn = os.path.basename(fileitem.filename)
open(os.path.join('static/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-bin
-
我正在谷歌应用引擎上部署这段代码。我对 Python 非常陌生,并且正在帮助一位正在紧急休假的同事。你能解释一下我到底该怎么做。