如何在 Heroku 中使用 Python webapp2 处理静态文件?
我现在正在将我的小型 Google App Engine 应用程序迁移到 Heroku 平台。我实际上并不使用 Bigtable,而 webapp2
大大降低了我的迁移成本。
现在我一直在处理静态文件。
有什么好的做法吗?如果是这样,请带我去那里。
提前致谢。
编辑
嗯,我现在正在为我的 WSGI 服务器使用paste
。 paste.StaticURLParser()
应该是我实现静态文件处理程序所需的。但是我不知道如何将它与 webapp2.WSGIApplication() 集成。有人可以帮助我吗?
也许我需要重写 webapp2.RequestHandler
类才能正确加载 paste.StaticURLParser()
;
import os
import webapp2
from paste import httpserver
class StaticFileHandler(webapp2.RequestHandler):
u"""Static file handler"""
def __init__(self):
# I guess I need to override something here to load
# `paste.StaticURLParser()` properly.
pass
app = webapp2.WSGIApplication([(r'/static', StaticFileHandler)], debug=True)
def main():
port = int(os.environ.get('PORT', 5000))
httpserver.serve(app, host='0.0.0.0', port=port)
if __name__ == '__main__':
main()
任何帮助将不胜感激!
I am now migrating my small Google App Engine app to Heroku platform. I don't actually use Bigtable, and webapp2
reduces my migration costs a lot.
Now I'm stuck on handling the static files.
Is there any good practices? If so, lead me there please.
Thanks in advance.
EDIT
Well, I'm now using paste
for my WSGI server. And paste.StaticURLParser()
should be what I need to implement static file handler. However I have no idea how to integrate it with webapp2.WSGIApplication()
. Could anyone help me?
Maybe I need to override webapp2.RequestHandler
class to load paste.StaticURLParser()
properly;
import os
import webapp2
from paste import httpserver
class StaticFileHandler(webapp2.RequestHandler):
u"""Static file handler"""
def __init__(self):
# I guess I need to override something here to load
# `paste.StaticURLParser()` properly.
pass
app = webapp2.WSGIApplication([(r'/static', StaticFileHandler)], debug=True)
def main():
port = int(os.environ.get('PORT', 5000))
httpserver.serve(app, host='0.0.0.0', port=port)
if __name__ == '__main__':
main()
Any helps would be appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
下面是我如何让它工作的。
我猜想依赖级联应用程序并不是最有效的选择,但它足以满足我的需求。
Below is how I got this working.
I'm guessing that relying on a cascade app isn't the most efficient option, but it works well enough for my needs.
这使得您的代码更易于部署,因为您可以使用 nginx 在生产中为静态媒体提供服务。
this makes your code more friendly to deploy since you can use nginx to serve your static media in production.
考虑到我进入游戏较晚,但实际上我更喜欢 DirectoryApp 一点。它处理事情的方式更像AppEngine。我可以在 src 目录中创建一个“静态”目录,然后我可以在 HTML(或其他内容)中引用这些目录,如下所示:http:..localhost:8080/static/js/jquery.js
Consider me late to the game, but I actually like DirectoryApp a little better. It handles things a bit more like AppEngine. I can create a "static" directory in my src directory, and then I can reference those in my HTML (or whatever) like this: http:..localhost:8080/static/js/jquery.js