浏览器显示代码而不是运行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,)
我已尝试在论坛上发布了各种解决方案,但在我的案例中没有一个解决方案。请指导我需要纠正的问题。谢谢大家的快速帮助。
答案 0 :(得分:0)
我只是偶然发现了这个问题,我遇到了同样的问题。你现在有这个答案吗?
至于为什么你的代码不起作用是因为HTML没有将它解释为python文件。到目前为止,这是我所理解的,因为我还没有解决我的问题。你需要某种解释器来获取html才能理解python文件。