如何在Heroku中使用Python webapp2处理静态文件?

Jap*_*boy 5 python heroku static-files webapp2

我现在将我的小型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()
Run Code Online (Sandbox Code Playgroud)

任何帮助将不胜感激!

小智 7

以下是我的工作方式.

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

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()
Run Code Online (Sandbox Code Playgroud)