将HTML文件上传到Google App Engine时出现405错误
我正在用Python做一个简单的回显应用。我想通过一个POST表单提交一个文件,然后把它回显回来(一个HTML文件)。
这是我使用的YAML文件中的handlers
部分:
handlers:
- url: /statics
static_dir: statics
- url: .*
script: main.py
这基本上是main.py
中的“你好,世界”示例,我还添加了一个目录来存放我的静态HTML表单文件。这里是statics/test.html
中的HTML内容:
<form action="/" enctype="multipart/form-data" method="post">
<input type="file" name="bookmarks_file">
<input type="submit" value="Upload">
</form>
处理程序看起来是这样的:
#!/usr/bin/env python
from google.appengine.ext import webapp
from google.appengine.ext.webapp import util
class MainHandler(webapp.RequestHandler):
def get(self):
self.response.headers['Content-Type'] = 'text/plain'
self.response.out.write(self.request.get('bookmarks_file'))
def main():
application = webapp.WSGIApplication([('/', MainHandler)],
debug=True)
util.run_wsgi_app(application)
if __name__ == '__main__':
main()
不过,当我尝试上传文件时,出现了405错误。这是为什么呢?
1 个回答
8
你在用POST方法提交表单,但你却写了一个get()
的处理函数,而不是post()
的处理函数。把def get(self):
改成def post(self):
就能解决HTTP 405错误了。