带有静态文件的 Cherrypy 自定义调度程序

发布于 2024-12-23 21:26:41 字数 1607 浏览 0 评论 0原文

我已经编写了自己的自定义调度程序,它使用正则表达式来映射路由,但是,我无法再在 /static 中托管静态文件。这是调度程序和配置:

class Dispatcher(object):
def __init__(self):
    self.urls = {}

def __call__(self, path_info):
    print('Dispatcher called: ' + path_info)

    func = self.find_handler(path_info)
    cherrypy.serving.request.handler = func

def find_handler(self, path_info):
    request = cherrypy.serving.request
    request.config = cherrypy.config.copy()

    for url in self.urls:
        args = re.findall(url, path_info)

        if len(args) > 0:
            # in the case that the route is just a URL, we don't want
            # an extra argument in the method function
            try:
                args.remove(path_info)
            except ValueError:
                pass

            controller = self.urls[url]
            method = request.method.lower()

            return cherrypy._cpdispatch.LateParamPageHandler(getattr(controller, method), *args)

    return cherrypy.NotFound()

def connect(self, url, controller):
    if not url.endswith("$"):
        url += "$"

    self.urls[url] = controller

和配置:

config = {
        'global': {
            'server.socket_host': '0.0.0.0',
            'server.socket_port': port,
        },

        '/static': {
            'tools.staticdir.on': True,
            'tools.staticdir.dir': os.path.join(os.getcwd(), 'static'),
        },

        '/': {
            'request.dispatch': self.dispatcher,
        }
    }

如果我使用标准调度程序,静态文件将按其应有的方式工作,但是如果我使用自己的调度程序,它们将不再工作。在调度程序中完成调试后,静态文件会通过调度程序,即使我指定只有在“/”中才会使用调度程序。

I have written my own custom dispatcher that uses regular expressions to map routes, however, I can no longer host static files in /static. Here is the dispatcher and the config:

class Dispatcher(object):
def __init__(self):
    self.urls = {}

def __call__(self, path_info):
    print('Dispatcher called: ' + path_info)

    func = self.find_handler(path_info)
    cherrypy.serving.request.handler = func

def find_handler(self, path_info):
    request = cherrypy.serving.request
    request.config = cherrypy.config.copy()

    for url in self.urls:
        args = re.findall(url, path_info)

        if len(args) > 0:
            # in the case that the route is just a URL, we don't want
            # an extra argument in the method function
            try:
                args.remove(path_info)
            except ValueError:
                pass

            controller = self.urls[url]
            method = request.method.lower()

            return cherrypy._cpdispatch.LateParamPageHandler(getattr(controller, method), *args)

    return cherrypy.NotFound()

def connect(self, url, controller):
    if not url.endswith("$"):
        url += "$"

    self.urls[url] = controller

And the config:

config = {
        'global': {
            'server.socket_host': '0.0.0.0',
            'server.socket_port': port,
        },

        '/static': {
            'tools.staticdir.on': True,
            'tools.staticdir.dir': os.path.join(os.getcwd(), 'static'),
        },

        '/': {
            'request.dispatch': self.dispatcher,
        }
    }

If I use the standard dispatcher, static files work as they should, however if I use my own, they no longer work. Having done debugging in the dispatcher, static files go through the dispatcher, even though I have specific that only in '/' does the dispatcher get used.

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

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

发布评论

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

评论(1

独行侠 2024-12-30 21:26:41

我不熟悉cherrypy,但似乎很明显:/static 中的所有内容也在/ 中,因此任何人都猜测它将使用哪个配置条目。我希望“更具体优先”,但根据你的描述,情况并非如此。查看文档也没有帮助,没有提到不明确的路径处理。

您可能认为更改顺序可能会有所帮助,但由于这是一本字典,因此不会保留顺序。

看来cherrypy是做不到这一点的。如果它有一个默认调度程序,而该调度程序因其他调度程序而过载,则可以解决问题。另一种选择是,您的自定义调度程序可以在检测到路径时调用静态调度程序。

最后,文档讨论了“将应用程序安装到路径”。如果您这样做,您可能需要更改顺序。如果您不这样做,它可能会自动完成,并且手动执行可能会解决您的问题。

并非所有这些都有意义,因为正如我所写的,我对cherrypy并不熟悉,但无论如何我希望它对您有所帮助。

I'm not familiar with cherrypy, but it seems obvious: everything in /static is also in /, so it is anyone's guess which config entry it will use. I would hope that "more specific has priority", but according to your description, this is not the case. Looking at the documentation also doesn't help, there's no mention of ambiguous path handling.

You would think that changing the order might help, but since this is a dictionary, the order isn't preserved.

It seems that cherrypy is unable to do this. If it has a default dispatcher which is overloaded with others, that could solve the problem. Another option is that your custom dispatcher can call the static one if it detects the path.

Finally, the documentation talks about "mounting an application to a path". If you do this, you may want to change the order. If you don't do this, it might be done automatically, and doing it manually may solve your problem.

Not all of this may make sense, since as I wrote I'm not familiar with cherrypy, but I hope it helps you a bit anyway.

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