(从this question改编)
看起来webapp2的没有一个静态文件处理程序;你将不得不推出自己的。这里有一个简单的一个:
import mimetypes
class StaticFileHandler(webapp2.RequestHandler):
def get(self, path):
# edit the next line to change the static files directory
abs_path = os.path.join(os.path.dirname(__file__), path)
try:
f = open(abs_path, 'r')
self.response.headers.add_header('Content-Type', mimetypes.guess_type(abs_path)[0])
self.response.out.write(f.read())
f.close()
except IOError: # file doesn't exist
self.response.set_status(404)
而在你app
对象,添加一个路线StaticFileHandler
:
app = webapp2.WSGIApplication([('/', MainHandler), # or whatever it's called
(r'/static/(.+)', StaticFileHandler), # add this
# other routes
])
现在http://localhost:8080/static/mydata.json
(比方说)将加载mydata.json
。
请记住,此代码是潜在的安全风险:它允许您的网站的任何访问者读取静态目录中的所有内容。因此,您应该将所有静态文件保存到不包含任何您想要限制访问权限的目录(例如源代码)。
[This](http://webapp-improved.appspot.com/tutorials/gettingstarted/staticfiles.html)可能是你要找的。 – 2013-05-12 09:08:42
是的,欢呼声。我也在研究这个问题,但到目前为止它并不工作。我将编辑该问题。 – devboell 2013-05-12 09:16:40
您链接到的文档是用于不使用GAE的webapp2 - 您使用它还是不使用?如果没有,那么app.yaml不适用... – Greg 2013-05-12 11:04:47