BlackSheep 入门#
BlackSheep 属于 ASGI Web 框架,因此它 需要 ASGI HTTP 服务器才能运行,例如 uvicorn 或 hypercorn。在本教程中,请安装 Uvicorn 和 Blacksheep:
pip install blacksheep uvicorn
from pathlib import Path
temp_dir = Path(".temp") # 创建临时目录
if not temp_dir.exists():
temp_dir.mkdir(exist_ok=True)
创建文件 server.py
:
%%file {temp_dir}/server.py
from datetime import datetime
from blacksheep import Application, get
app = Application()
@get("/")
def home():
return f"Hello, World! {datetime.now().isoformat()}"
Writing .temp/server.py
使用以下命令通过端口 44777
启动应用程序,并在文件更改时自动重新加载:
cd {temp_dir}&&uvicorn server:app --port 44777 --reload
打开 Web 浏览器并导航到 http://127.0.0.1:44777
。Web 浏览器将显示来自 Web 应用程序的文本。
参考:MVC:
# using the provided dev.py file (useful to debug)
python dev.py
# or using the uvicorn CLI
uvicorn app.main:app --port 44777 --lifespan on --reload