【问题标题】:Bottle Server: removing routes瓶服务器:删除路线
【发布时间】:2020-08-25 22:25:46
【问题描述】:

我想在我的 bottle 服务器运行时添加和删除路由。

我的一般问题:是否有适当的方法来删除路线?

换句话说:如何撤消我对app.route 所做的操作?

下面我将更详细地描述我的问题。如果您知道我的一般问题的答案,请不要浪费时间阅读它。

这是一个小演示脚本,描述了我如何解决我的问题:

如果调用GET /add:添加一个'Hello World'路由

如果调用 GET /remove:所有具有相同前缀的路由,如 'Hello World' 路由都会被更改以触发 404 错误

import bottle
from bottle import redirect, abort

def addRoute():
    app.route('/route/hello')(lambda :'Hello World')
    print('Routes after calling /add:\n' + '\n'.join([str(route) for route in app.routes]))
    redirect('route/hello')

def removeRoute():
    prefix = '/route/hello'

    #making a list of all routes having the prefix
    #because this is a testfile there is only one rule that startswith prefix
    routes = [route for route in app.routes if route.rule.startswith(prefix)]

    #because there could be multiple routes with the same rule and method,
    #making a list without duplicates
    ruleMethodTuples = list(dict.fromkeys([(route.rule, route.method) for route in routes]))

    for ruleMethod in ruleMethodTuples :

        #Workaround: Overwriting the existing route with a 404
        #Here I'd prefer to make a statement that removes the existing route instead,
        #so the default 404 would be called
        app.route(ruleMethod[0], method = ruleMethod[1])(lambda **kwords: abort(404, 'Route deleted'))

    print('Routes after calling /remove:\n' + '\n'.join([str(route) for route in app.routes]))
    redirect('/route/hello')

if __name__ == '__main__':
    app = bottle.app()
    app.route('/add')(addRoute)
    app.route('/remove')(removeRoute)
    print('Initial routes:\n' + '\n'.join([str(route) for route in app.routes]))
    bottle.run(app, host = 'localhost', port = 8080)

所以这里是启动和调用后的输出:

/添加

/删除

/添加

在每次通话后显示 app.routes 中有哪些路由

Initial routes:
<GET '/add' <function addRoute at 0x02A876A8>>
<GET '/remove' <function removeRoute at 0x00F5F420>>
Bottle v0.12.17 server starting up (using WSGIRefServer())...
Listening on http://localhost:8080/
Hit Ctrl-C to quit.

Routes after calling /add:
<GET '/add' <function addRoute at 0x02A876A8>>
<GET '/remove' <function removeRoute at 0x00F5F420>>
<GET '/route/hello' <function addRoute.<locals>.<lambda> at 0x030C3E88>>
127.0.0.1 - - [25/Aug/2020 23:19:09] "GET /add HTTP/1.1" 303 0
127.0.0.1 - - [25/Aug/2020 23:19:09] "GET /route/hello HTTP/1.1" 200 11
Routes after calling /remove:
<GET '/add' <function addRoute at 0x02A876A8>>
<GET '/remove' <function removeRoute at 0x00F5F420>>
<GET '/route/hello' <function addRoute.<locals>.<lambda> at 0x030C3E88>>
<GET '/route/hello' <function removeRoute.<locals>.<lambda> at 0x030C3FA8>>
127.0.0.1 - - [25/Aug/2020 23:19:14] "GET /remove HTTP/1.1" 303 0
127.0.0.1 - - [25/Aug/2020 23:19:14] "GET /route/hello HTTP/1.1" 404 720
Routes after calling /add:
<GET '/add' <function addRoute at 0x02A876A8>>
<GET '/remove' <function removeRoute at 0x00F5F420>>
<GET '/route/hello' <function addRoute.<locals>.<lambda> at 0x030C3E88>>
<GET '/route/hello' <function removeRoute.<locals>.<lambda> at 0x030C3FA8>>
<GET '/route/hello' <function addRoute.<locals>.<lambda> at 0x030E0270>>
127.0.0.1 - - [25/Aug/2020 23:19:17] "GET /add HTTP/1.1" 303 0
127.0.0.1 - - [25/Aug/2020 23:19:18] "GET /route/hello HTTP/1.1" 200 11

所以不是替换 GET '/route/hello',而是调用:app.route 使用相同的方法添加更多路由,并且规则在列表的末尾。

但是到目前为止这很有效,因为首先选择了最新的匹配路由,但我很确定这会导致(或早或晚)性能问题或在调用某些 /add 后导致服务器崩溃s 和 /removes。

此外,我注意到,我可以在不更改实际路由的情况下更改 app.routes,所以 次要问题:

我可以从 app.routes 中删除“已弃用”的路由以防止堆栈溢出吗?

还有第三个问题:

是不是我做错了什么?

【问题讨论】:

  • Bottle 对象(通常称为app)将路由作为routes 属性保存在列表中。你可以随意操作它。
  • 我试图操纵路线。它对路由机制没有任何作用。
  • 很好的问题!您是否会考虑在为什么您首先要删除路线上添加一两个词? Bottle 没有提供这样做的方法这一事实应该是您的第一个迹象,表明您可能正在尝试解决错误的问题。见meta.stackexchange.com/questions/66377/what-is-the-xy-problem

标签: python url-routing bottle


【解决方案1】:

我检查了add_route的源代码

它将route 添加到两个对象:self.routesself.routerapp.routesapp.router),这会产生问题。

def add_route(self, route):
    """ Add a route object, but do not change the :data:`Route.app`
        attribute."""
    self.routes.append(route)
    self.router.add(route.rule, route.method, route, name=route.name)
    if DEBUG: route.prepare()

self.router 是对象Router,具有rulesbuilderstatic dyna_routesdyna_regexes

如果您在添加新路线之前和之后检查它们,那么您会看到 builderstatic 的变化。

def addRoute():
    print('--- before ---')
    print(app.router.rules)
    print(app.router.builder)
    print(app.router.static)
    print(app.router.dyna_routes)
    
    app.route('/route/hello')(lambda :'Hello World')

    print('--- after ---')
    print(app.router.rules)
    print(app.router.builder)
    print(app.router.static)
    print(app.router.dyna_routes)

    print('Routes after calling /add:\n' + '\n'.join([str(route) for route in app.routes]))
    redirect('route/hello')

如果我从builderstatic 中删除'/route/hello',则'/route/hello' 停止工作,但app.routes 仍然显示它们,因此您必须从app.routesapp.router 中删除'/route/hello' - 但是他们对此没有特殊功能:)

def removeRoute():
    prefix = '/route/hello'

    del app.router.builder[prefix]
    del app.router.static['GET'][prefix]
    
    print('Routes after calling /remove:\n' + '\n'.join([str(route) for route in app.routes]))
    redirect('/route/hello')

我的完整代码:

import bottle
from bottle import redirect, abort

def addRoute():
    print('--- before /add ---')
    print(app.router.rules)
    print(app.router.builder)
    print(app.router.static)
    print(app.router.dyna_routes)
    print(app.router.dyna_regexes)
    print('Routes before calling /add:\n' + '\n'.join([str(route) for route in app.routes]))
    
    app.route('/route/hello')(lambda :'Hello World')
    
    print('--- after /add ---')
    print(app.router.rules)
    print(app.router.builder)
    print(app.router.static)
    print(app.router.dyna_routes)
    print(app.router.dyna_regexes)
    print('Routes after calling /add:\n' + '\n'.join([str(route) for route in app.routes]))
    
    redirect('route/hello')

def removeRoute():
    prefix = '/route/hello'

    print('--- before /remove ---')
    print(app.router.rules)
    print(app.router.builder)
    print(app.router.static)
    print(app.router.dyna_routes)
    print(app.router.dyna_regexes)
    print('Routes before calling /remove:\n' + '\n'.join([str(route) for route in app.routes]))

    del app.router.builder[prefix]
    del app.router.static['GET'][prefix]
    
    print('--- after /remove ---')
    print(app.router.rules)
    print(app.router.builder)
    print(app.router.static)
    print(app.router.dyna_routes)
    print(app.router.dyna_regexes)
    print('Routes before calling /remove:\n' + '\n'.join([str(route) for route in app.routes]))

    redirect('/route/hello')

if __name__ == '__main__':
    app = bottle.app()
    app.route('/add')(addRoute)
    app.route('/remove')(removeRoute)
    print('Initial routes:\n' + '\n'.join([str(route) for route in app.routes]))
    bottle.run(app, host = 'localhost', port = 8080)

【讨论】:

  • 非常感谢。顺便说一句:我在del app.router.static['GET'][prefix] 之后做了app.routes = [route for route in app.routes if not(route.rule == prefix)] 以从 app.routes 中删除路线
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-06-19
  • 2023-02-14
  • 1970-01-01
  • 2012-06-21
  • 1970-01-01
相关资源
最近更新 更多