推荐学习书目
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 · 1654 views
    This topic created in 2776 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   ·   Solana   ·   3229 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 144ms · UTC 12:51 · PVG 20:51 · LAX 05:51 · JFK 08:51
    ♥ Do have faith in what you're doing.