推荐学习书目
› Learn Python the Hard Way
Python Sites
› PyPI - Python Package Index
› http://diveintopython.org/toc/index.html
› Pocoo
值得关注的项目
› PyPy
› Celery
› Jinja2
› Read the Docs
› gevent
› pyenv
› virtualenv
› Stackless Python
› Beautiful Soup
› 结巴中文分词
› Green Unicorn
› Sentry
› Shovel
› Pyflakes
› pytest
Python 编程
› pep8 Checker
Styles
› PEP 8
› Google Python Style Guide
› Code Style from The Hitchhiker's Guide
prasanta
V2EX  ›  Python

Xweb-Router, xweb 路由插件

  •  
  •   prasanta · Oct 6, 2018 · 1905 views
    This topic created in 2916 days ago, the information mentioned may be changed or developed.

    xweb-router

    Router middleware for xweb

    基本用法

    from xweb import App
    
    from xweb_router import Router
    
    app = App()
    router = Router()
    app.use(router)
    
    @router.get('/')
    async def home(ctx):
        ctx.body = "Home"
    
    if __name__ == '__main__':
        app.listen(8000)
    

    路由插件

    from xweb import App
    
    from xweb_router import Router
    
    app = App()
    router = Router()
    app.use(router)
    
    
    @router.use('/')
    async def middleware(ctx, fn):
        """Router Middleware"""
        print('middleware')
        await fn()
        
    @router.get('/')
    async def home(ctx):
        ctx.body = "Home"
        
    if __name__ == '__main__':
        app.listen(8000)
    

    动态参数

    from xweb import App
    
    from xweb_router import Router
    
    app = App()
    router = Router()
    
    @router.get('/{name}')
    async def hello(ctx):
        """URL parameters"""
        ctx.body = f"Hello {ctx.params.name}"
    
    if __name__ == '__main__':
        app.listen(8000)
    

    嵌套路由

    
    from xweb import App
    
    from xweb_router import Router
    
    app = App()
    router = Router()
    nested = Router()
    
    app.use(router)
    
    router.use('/post')(nested)
    
    
    @nested.get('/index')
    async def index(ctx):
        ctx.body = "Nested Index"
    
    
    if __name__ == '__main__':
        app.listen(8000)
    

    Github: xweb-router

    No Comments Yet
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   814 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 23ms · UTC 21:40 · PVG 05:40 · LAX 14:40 · JFK 17:40
    ♥ Do have faith in what you're doing.