【问题标题】:Python webapp2 serve static contentPython webapp2 提供静态内容
【发布时间】:2018-01-29 14:43:43
【问题描述】:

我们的团队将一个项目从 GAE 迁移到 AWS。一个组件是构建在 webapp2 之上的 Web 应用程序,这是一个易于与 GAE 集成的框架。我们也将 webapp2 框架保留在 AWS 中,并进行了一些小的更改以使其正常工作。

Web 应用程序在云中运行良好,但我也在尝试找到一种在本地开发机器上运行它的方法。当我们使用 GAE 环境时,这很容易,因为 Google 提供了 App Engine Launcher,这个工具可以很好地模拟云环境。

在 AWS 中,我们继续进行一些修改以使用 App Engine Launcher 启动 Web 应用程序,但现在我们想放弃它。因此,我修改了 python 脚本并成功启动,但我不知道如何提供静态内容。静态文件(CSS、JS)被添加到 HTML 模板中,例如 link rel="stylesheet" type="text/css" href="{{statics_bucket}}/statics/css/shared.css"/,其中 {{statics_bucket}} 是一个环境变量,它指向每个环境的特定 Amazon S3 存储桶。当然,这在 localhost 上不起作用,因为没有人在 http://localhost:8080/statics/css/shared.css 上提供静态内容。 Google App Engine 启动器具有此功能,它完成了所有艰巨的工作。

有人能指出实现我目标的方法吗?

【问题讨论】:

    标签: python webapp2 static-content


    【解决方案1】:

    我设法通过以下脚本实现了我的目标:

    import os.path
    import application
    
    from paste import httpserver
    from paste.cascade import Cascade
    from paste.urlparser import StaticURLParser
    
    def main():
        web_client = application.application
        here = os.path.dirname(os.path.abspath(__file__))
        static_app = StaticURLParser(here)
    
        app = Cascade([web_client, static_app])
        httpserver.serve(app, host='localhost', port='8080')
    
    if __name__ == '__main__':
        main()
    

    该脚本启动云应用程序以及提供静态文件的组件,两者都位于同一服务器内的同一端口上。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-18
      • 1970-01-01
      • 2021-11-15
      相关资源
      最近更新 更多