zl程序教程

您现在的位置是:首页 >  其它

当前栏目

Uvicorn

2023-09-14 09:00:00 时间

Uvicorn——快速的ASGI服务器,基于uvloop和httptools构建

安装

pip install uvicorn

使用

uvicorn [OPTIONS] APP

eg:

  test.py

async def app(scope, receive, send):
    assert scope['type'] == 'http'

    await send({
        'type': 'http.response.start',
        'status': 200,
        'headers': [
            [b'content-type', b'text/plain'],
        ],
    })
    await send({
        'type': 'http.response.body',
        'body': b'Hello, world!',
    })

运行

uvicorn test:app

修改启动端口

uvicorn test:app --port 5000

支持局域网访问

 uvicorn main:app --host 0.0.0.0

官方文档 https://www.uvicorn.org/