【问题标题】:How to serve a wsgi app from within Pyramid?如何从 Pyramid 中提供 wsgi 应用程序?
【发布时间】:2013-11-07 22:18:49
【问题描述】:

我正在构建一个 Pyramid 应用程序,该应用程序需要将地图图块提供给 OpenLayers 网络地图。

TileStache 是一个 WMS 磁贴服务器,它为我需要的磁贴提供服务,我想在我的 Pyramid 应用程序中以视图的形式访问它。

单独访问 TileStache 网址 www.exampletilestacheurl.com/LAYERNAME/0/0/0.png 效果很好 - 它可以正确返回磁贴。

在 Pyramid 中,我想使用 pyramid.wsgi.wsgiapp 将 TileStache 应用程序包装为视图。我的目标是访问www.mypyramidapp.com/tilestache/LAYERNAME/0/0/0.png 就像上面的 TileStache url 示例一样工作。

我将 TileStache 应用程序包装成一个视图:

from pyramid.wsgi import wsgiapp

@wsgiapp
def tileserver(environ, start_response):
    # Enable TileStache tile server
    import TileStache
    tile_app = TileStache.WSGITileServer('tilestache/tilestache.cfg', autoreload=False)
    return [tile_app]

并为myapp.__init__.main中的视图分配了一条路线:

from tilestache import tileserver
config.add_view(tileserver, name='tilestache')
config.add_route('tilestache', '/tilestache')

但是当我访问任何以www.mypyramidapp.com/tilestache/ 开头的网址时,它只会返回IndexError: list index out of range. 有人熟悉wsgiapp 的工作原理吗?

【问题讨论】:

    标签: python pyramid wsgi tilestache


    【解决方案1】:

    如果 tile_app 是一个 wsgi 应用程序,你需要像这样返回调用它的结果...

    from pyramid.wsgi import wsgiapp
    
    # Enable TileStache tile server
    import TileStache
    tile_app = TileStache.WSGITileServer('tilestache/tilestache.cfg', autoreload=False)
    
    @wsgiapp
    def tileserver(environ, start_response):
    
        return tile_app(environ, start_response)
    

    注意:我将应用程序创建移至模块级别,以便在导入时创建它,而不是在每次处理请求时创建。这可能不是您正在寻找的行为,但大多数时候它是。

    【讨论】:

    • 谢谢,这正是我所需要的。完美运行。
    猜你喜欢
    • 1970-01-01
    • 2012-04-14
    • 2011-12-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-17
    相关资源
    最近更新 更多