本文介绍了如何在Heroku中使用Python webapp2处理静态文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

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



现在我卡住了处理静态文件。



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



预先感谢。



编辑

好吧,我现在正在为我的WSGI服务器使用 paste 。和 paste.StaticURLParser()应该是我需要实现静态文件处理程序。不过,我不知道如何将它与 webapp2.WSGIApplication()集成。任何人都可以帮助我?



也许我需要重载 webapp2.RequestHandler 类来加载 paste.StaticURLParser()正确;

  import os 
import webapp2
从粘贴导入httpserver
$ b $ class StaticFileHandler(webapp2.RequestHandler):
u静态文件处理程序

def __init __(self):
#我想我需要重写一些东西来正确加载
#`paste.StaticURLParser()`。
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()

请注意!

解决方案

下面是我如何得到这个工作。



我猜测依赖级联应用程序并不是最有效的选择,但它对我的需求足够好。


来自paste.urlparser的

 从paste.cascade导入StaticURLParser 
导入级联
从粘贴导入httpserver
导入webapp2
导入套接字


类HelloWorld(webapp2.RequestHandler):
def get(self):
self.response.write('Hello cruel world。')

#创建主应用程序
web_app = webapp2.WSGIApplication([
('/',HelloWorld),
])

#创建一个应用程序来为静态文件提供服务
#选择一个与你的源分离的目录(例如static /),这样它就不会变成可用的
static_app = StaticURLParser(static /)

#创建一个首先查找静态文件的级联,然后尝试webapp
app = Cascade([static_app,web_app])

def main():
httpserver.serve(app,host = socket.gethostname(),port ='8080')

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!

解决方案

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

这篇关于如何在Heroku中使用Python webapp2处理静态文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-08 16:49