V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
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 · 2018-10-06 16:29:11 +08:00 · 1301 次点击
    这是一个创建于 2020 天前的主题,其中的信息可能已经有所发展或是发生改变。

    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

    目前尚无回复
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5961 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 02:04 · PVG 10:04 · LAX 19:04 · JFK 22:04
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.