更改webpy中的静态目录路径

发布于 2024-11-28 07:00:31 字数 186 浏览 2 评论 0原文

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

这仅供本地使用,不适用于生产。有什么想法吗?谢谢

I'd love to be able to change the webpy static directory without the need to set up and run nginx locally. Right now, it seems webpy will only create a static directory if /static/ exists. In my case, I want to use /foo/bar/ as my static directory, but couldn't find any info related to configuring this (other than running apache or nginx locally).

This is for local use only, not production. Any ideas? Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

物价感观 2024-12-05 07:00:31

如果您需要为同一路径拥有不同的目录,那么您可以子类化 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()

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

If you need to have different directory for the same path then you may subclass web.httpserver.StaticMiddleware or write your own middleware like this (it tricks StaticApp by modifying PATH_INFO):

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

Or you can create symlink named "static" and point it to another directory.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文