(從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