第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機(jī)立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Bottle Server:刪除路由

Bottle Server:刪除路由

胡說叔叔 2023-06-20 16:14:30
我想在我的Bottle服務(wù)器運(yùn)行時添加和刪除路由。我的一般問題:是否有刪除路線的正確方法?換句話說:我怎樣才能撤消我所做的事情app.route?在下文中,我將以更詳細(xì)的方式描述我的問題。如果您知道我的一般問題的答案,請不要浪費(fèi)時間閱讀它。這是一個小演示腳本,描述了我如何解決我的問題:如果調(diào)用GET /add:添加“Hello World”路由如果調(diào)用GET /remove:所有具有相同前綴的路由(如“Hello World”路由)都會更改為觸發(fā) 404 錯誤import bottlefrom bottle import redirect, abortdef 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)所以這是啟動和調(diào)用后的輸出:/添加/消除/添加在每次通話后顯示app.routes中有哪些路線。因此,調(diào)用app.route不是替換GET '/route/hello',而是在列表末尾添加更多具有相同方法和規(guī)則的路由。然而,到目前為止這仍然有效,因?yàn)槭紫冗x擇了最新的匹配路由,但我很確定這會(或早或晚)導(dǎo)致性能問題或在調(diào)用一些 /add s 和 /remove s 后導(dǎo)致服務(wù)器崩潰。此外我注意到,我可以在不更改實(shí)際路由的情況下更改app.routes ,所以次要問題:我可以從 app.routes 中刪除“已棄用”的路由以防止計(jì)算器溢出嗎?第三個問題:我做的事情完全錯誤嗎?
查看完整描述

1 回答

?
慕絲7291255

TA貢獻(xiàn)1859條經(jīng)驗(yàn) 獲得超6個贊

我檢查了add_route的源代碼

它添加route到兩個對象:self.routesand?self.router(?app.routesand?app.router) 這會產(chǎn)生問題。

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是具有rules,?builder,?static?dyna_routes,?_dyna_regexes

如果您在添加新路線之前和之后檢查它們,那么您會看到builder和中的變化static

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

如果我'/route/hello'從中刪除builder然后停止工作但仍然顯示它們,那么你將不得不從兩者中刪除static-但它們沒有為此的特殊功能:)'/route/hello'app.routes'/route/hello'app.routesapp.router


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)


查看完整回答
反對 回復(fù) 2023-06-20
  • 1 回答
  • 0 關(guān)注
  • 138 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號