簡體   English   中英

Bottle Server:刪除路由

[英]Bottle Server: removing routes

我想在我的Bottle服務器運行時添加和刪除路由。

我的一般問題:是否有適當的方法來刪除路線?

換句話說:我怎樣才能撤消我對app.route所做的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 和/remove s 后導致服務器崩潰。

此外,我注意到,我可以不更改實際路由的情況下更改app.routes ,因此次要問題:

我可以從 app.routes 中刪除“已棄用”的路由以防止堆棧溢出嗎?

第三個問題:

我是否以完全錯誤的方式做某事?

我檢查了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是對象 路由器,它具有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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM