【问题标题】:Can web.py route static content with its builtin server?web.py 可以使用其内置服务器路由静态内容吗?
【发布时间】:2013-09-18 03:22:23
【问题描述】:

web.py 带有一个内置的 http 服务器,足以满足我的测试需求。我在 server.py 中定义了以下 2 个端点。

/login
/info

我的文件夹结构是这样的。

app
|__api
|__|__login.py
|__|__info.py
|__|__server.py
|__www
|__|__index.html
|__|__app.js

在终端中,我只是运行以下命令。

$ python app/api/server.py

上述设置有效,但我要添加的是一个根 (/) 以转到 www/ 并提供 index.html。我可以用 web.py 内置服务器做到这一点吗?

【问题讨论】:

    标签: python http web.py


    【解决方案1】:

    自己写的:)

    urls = (
        '/(.*)', 'General'
    )
    
    class General:
        app_dir = 'www'
    
        def GET(self, path):
            root = os.path.abspath(os.path.join('.', os.pardir))
            dest = '%s/%s/index.html' % (root, self.app_dir)
            if path:
                dest = '%s/%s/%s' % (root, self.app_dir, path)
            with open(dest, 'r') as f:
                return f.read()
            return ''
    

    【讨论】:

    • 非常好的解决方案!
    猜你喜欢
    • 2018-11-26
    • 2017-08-14
    • 2011-11-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多