基于内函数的Cherrypy路由选择

2024-03-29 09:22:58 发布

您现在位置:Python中文网/ 问答频道 /正文

我想打电话给http://localhost:8080/match/123/subtitle

def match(self, dayid):
    day = dayid
    day_folder = os.path.join(root_folder,day)                      
    #Some logics here

    @cherrypy.expose
    def subtitle(self, mid):
        return "Requesting subtitle for ", mid

这不是正确的方法吗?如果没有,重写Url的正确方法是什么?在

我试过下面的方法,但也不管用。在

^{pr2}$

Tags: path方法selflocalhosthttposdefmatch
1条回答
网友
1楼 · 发布于 2024-03-29 09:22:58

你可以用一个定制的调度器。这是一个简单的例子给你一个想法。你走对了。在

import cherrypy

class my_index_controller:
        @cherrypy.expose
        def index(self):
                return """<h1>WORKS</h1>"""


class other_controller:
        @cherrypy.expose
        def subtitle(self, mid):
                return """<h1>WORKS2 %s</h1>""" % str(mid)

controller1 = my_index_controller()
controller2 = other_controller()
d = cherrypy.dispatch.RoutesDispatcher()
d.connect('index', '/', controller=controller1, action='index')
d.connect('subtitle', '/match/:mid/subtitle', controller=controller2, action='subtitle')
conf = {'/': {'request.dispatch': d}}

if __name__=='__main__':
        cherrypy.quickstart(controller1, '/', config=conf)

相关问题 更多 >