基于asgi的web框架
nardis的Python项目详细描述
NARDIS
一个基于asgi的web框架。这是受到node.js的express框架的启发。
当前状态
仍然没有准备好生产。
此api目前处于试验阶段,随时可能更改。
因此,请不要将此用于生产应用程序。
不过,请你好好玩玩。随着api变得更加稳定,现阶段的任何反馈都是受欢迎的。
要求
Python3.6+
安装
通过PIP
运行以下命令:
$ pip install nardis
来源
要从源代码构建,请克隆此repo,然后:
$ python setup.py install
示例
这里有一个简单的例子。创建一个application.py
,然后复制并粘贴这个:
fromnardis.asgiimportmainfromnardis.routingimportGetasget,Postaspostimportasynciotemplate_start="""<!doctype html><head><title>example</title></head><body>"""template_end="""</body>"""asyncdefindex(req,res):""" This just demonstrates that you can write async code. Don't actually write this in production. """awaitres.send(template_start,more=True)forxinrange(10,0,-1):awaitres.send(f"<p>{x}!</p>",more=True)awaitasyncio.sleep(1)awaitres.send("<p>liftoff!</p>",more=True)awaitres.send(template_end)asyncdefhello(req,res):""" Try going to http://127.0.0.1:8000/hello/your_name/ You'll see "Hello, your_name!" """name=req.params.get('name','world')awaitres.send(f"<h1>Hello, {name}!</h1>")routes=[get(r"^/?$",index),get(r"^/hello/(?P<name>\w+)/?$",hello),]config={'routes':routes,}app=main(config)# this is the ASGI applicationif__name__=='__main__':fromuvicorn.runimportrunrun(app,'127.0.0.1',8000)
然后:
$ python application.py
或者,您也可以执行以下操作:
$ uvicorn application:app
这将在http://127.0.0.1:8000上启动服务器
使用其他Web服务器
目前,乌维康是纳迪斯的属地。
代码库实际上并不使用uvicorn,但是这种依赖性允许您快速运行应用程序(参见上面的示例)。将来可能会删除此依赖项。
nardis还应该与其他基于asgi的web服务器一起工作,比如daphne。
要让daphne使用上面的示例代码,您可以执行以下操作:
$ daphne application:app