【问题标题】:How can I handle static files with Python webapp2 in Heroku?如何在 Heroku 中使用 Python webapp2 处理静态文件?
【发布时间】:2012-01-18 05:39:06
【问题描述】:

我现在正在将我的小型 Google App Engine 应用程序迁移到 Heroku 平台。我实际上并没有使用 Bigtable,webapp2 大大降低了我的迁移成本。

现在我被困在处理静态文件上。

有什么好的做法吗?如果有,请带我去。

提前致谢。

编辑

好吧,我现在将paste 用于我的 WSGI 服务器。 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()

任何帮助将不胜感激!

【问题讨论】:

    标签: python heroku static-files webapp2


    【解决方案1】:

    以下是我如何使其工作的。

    我猜依赖级联应用并不是最有效的选择,但它足以满足我的需求。

    from paste.urlparser import StaticURLParser
    from paste.cascade import Cascade
    from paste import httpserver
    import webapp2
    import socket
    
    
    class HelloWorld(webapp2.RequestHandler):
        def get(self):
            self.response.write('Hello cruel world.')
    
    # Create the main app
    web_app = webapp2.WSGIApplication([
        ('/', HelloWorld),
    ])
    
    # Create an app to serve static files
    # Choose a directory separate from your source (e.g., "static/") so it isn't dl'able
    static_app = StaticURLParser("static/")
    
    # Create a cascade that looks for static files first, then tries the webapp
    app = Cascade([static_app, web_app])
    
    def main():
        httpserver.serve(app, host=socket.gethostname(), port='8080')
    
    if __name__ == '__main__':
        main()
    

    【讨论】:

    • 感谢您的回复。我以后要试试这个!我不知道Cascade
    • 您可以在开发过程中通过使用变量来提供静态文件,例如:if DEBUG: 等,在生产环境中使用 nginx。
    • 谢谢!正在寻找这个问题的答案。
    【解决方案2】:

    这使您的代码更易于部署,因为您可以使用 nginx 在生产中提供静态媒体。

    def main():
      application = webapp2.WSGIApplication(routes, config=_config, debug=DEBUG)
      if DEBUG:
        # serve static files on development
        static_media_server = StaticURLParser(MEDIA_ROOT)
        app = Cascade([static_media_server, application])
        httpserver.serve(app, host='127.0.0.1', port='8000')
    else:
        httpserver.serve(application, host='127.0.0.1', port='8000')
    

    【讨论】:

      【解决方案3】:

      认为我迟到了,但实际上我更喜欢 DirectoryApp。它处理的事情有点像 AppEngine。我可以在我的 src 目录中创建一个“静态”目录,然后我可以像这样在我的 HTML(或其他)中引用这些目录:http:..localhost:8080/static/js/jquery.js

      static_app = DirectoryApp("static")
      
      # Create a cascade that looks for static files first, then tries the webapp
      app = Cascade([static_app,wsgi_app])
      
      httpserver.serve(app, host='0.0.0.0',
                       port='8080')
      

      【讨论】:

        猜你喜欢
        • 2012-02-21
        • 2018-01-22
        • 2012-11-15
        • 1970-01-01
        • 1970-01-01
        • 2016-02-17
        • 2019-07-18
        • 2021-07-10
        • 2018-01-29
        相关资源
        最近更新 更多