更改webpy中的静态目录路径

Sha*_*tle 6 python web.py

我希望能够更改webpy静态目录,而无需在本地设置和运行nginx.现在,似乎webpy只会在/ static/exists时创建一个静态目录.在我的情况下,我想使用/ foo/bar /作为我的静态目录,但找不到与配置相关的任何信息(除了在本地运行apache或nginx).

这仅供本地使用,不适用于生产.有任何想法吗?谢谢

And*_*min 5

如果您需要为同一路径创建不同的目录,那么您可以继承web.httpserver.StaticMiddleware,或者像这样编写自己的中间件(通过修改PATH_INFO来欺骗StaticApp):

import web
import os
import urllib
import posixpath

urls = ("/.*", "hello")
app = web.application(urls, globals())

class hello:
    def GET(self):
        return 'Hello, world!'


class StaticMiddleware:
    """WSGI middleware for serving static files."""
    def __init__(self, app, prefix='/static/', root_path='/foo/bar/'):
        self.app = app
        self.prefix = prefix
        self.root_path = root_path

    def __call__(self, environ, start_response):
        path = environ.get('PATH_INFO', '')
        path = self.normpath(path)

        if path.startswith(self.prefix):
            environ["PATH_INFO"] = os.path.join(self.root_path, web.lstrips(path, self.prefix))
            return web.httpserver.StaticApp(environ, start_response)
        else:
            return self.app(environ, start_response)

    def normpath(self, path):
        path2 = posixpath.normpath(urllib.unquote(path))
        if path.endswith("/"):
            path2 += "/"
        return path2


if __name__ == "__main__":
    wsgifunc = app.wsgifunc()
    wsgifunc = StaticMiddleware(wsgifunc)
    wsgifunc = web.httpserver.LogMiddleware(wsgifunc)
    server = web.httpserver.WSGIServer(("0.0.0.0", 8080), wsgifunc)
    print "http://%s:%d/" % ("0.0.0.0", 8080)
    try:
        server.start()
    except KeyboardInterrupt:
        server.stop()
Run Code Online (Sandbox Code Playgroud)

或者,您可以创建名为"static"的符号链接并将其指向另一个目录.