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

发布于 2024-12-20 20:46:00 字数 1010 浏览 2 评论 0原文

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

现在我一直在处理静态文件。

有什么好的做法吗?如果是这样,请带我去那里。

提前致谢。

编辑

嗯,我现在正在为我的 WSGI 服务器使用pastepaste.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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

Smile简单爱 2024-12-27 20:46:00

下面是我如何让它工作的。

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

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

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.

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()
-残月青衣踏尘吟 2024-12-27 20:46:00

这使得您的代码更易于部署,因为您可以使用 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')

this makes your code more friendly to deploy since you can use nginx to serve your static media in production.

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')
弃爱 2024-12-27 20:46:00

考虑到我进入游戏较晚,但实际上我更喜欢 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')

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

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')
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文