python轻量级框架

Responder框架

responder是@kennethreitz新开发的一个项目, 是一个基于 Python 的 HTTP 服务框架. 底层用了 Starlette 的框架, Starlette 是一款轻量级的 ASGI 框架/工具包, 可以用 Starlette 构建高性能的异步 IO 服务.

示例代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#! /usr/bin/env python
# -*- coding: utf-8 -*-

'''
@version: v0.1
@Author: shu_ke163@163.com
@Description: file content
@Date: 2020-05-05 01:04:37
@LastEditors: shuke
@LastEditTime: 2020-05-05 02:07:37
'''
import time
import graphene
import responder

api = responder.API()

@api.route("/{greeting}")
async def greet_world(req, resp, *, greeting):
resp.text = f"{greeting}, world"

@api.route("/")
def hello(req, resp):

# 作为后台任务执行
@api.background.task
def sleep(s=10):
time.sleep(s)
print("slept!")

sleep()
resp.content = "processing"

class Query(graphene.ObjectType):
hello = graphene.String(name=graphene.String(default_value="stranger"))

def resolve_hello(self, info, name):
return f"Hello {name}"

schema = graphene.Schema(query=Query)
view = responder.ext.GraphQLView(api=api, schema=schema)

api.add_route("/graph", view)

if __name__ == "__main__":
api.run()

官方示例文档

Starlette

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#! /usr/bin/env python
# -*- coding: utf-8 -*-

'''
@version: v0.1
@Author: shu_ke163@163.com
@Description: file content
@Date: 2020-05-05 02:16:32
@LastEditors: shuke
@LastEditTime: 2020-05-05 02:22:39
'''

from starlette.applications import Starlette
from starlette.responses import JSONResponse
from starlette.routing import Route

async def homepage(request):
return JSONResponse({"hello": "shuke's world"})

routes = [
Route("/", endpoint=homepage)
]

app = Starlette(debug=True, routes=routes)

启动

1
(base) ➜  uvicorn example:app --reload

请求

1
2
3
4
5
6
7
8
9
10
(base) ➜ http http://127.0.0.1:8000
HTTP/1.1 200 OK
content-length: 25
content-type: application/json
date: Mon, 04 May 2020 18:27:23 GMT
server: uvicorn

{
"hello": "shuke's world"
}

官方文档

FastAPI

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#! /usr/bin/env python
# -*- coding: utf-8 -*-

'''
@version: v0.1
@Author: shu_ke163@163.com
@Description: file content
@Date: 2020-05-05 01:04:37
@LastEditors: shuke
@LastEditTime: 2020-05-05 02:48:43
'''

from fastapi import FastAPI

app = FastAPI()

@app.get("/")
async def read_root():
return {"hello": "world!"}

@app.get("/items/{item_id}")
async def read_item(item_id: int, q: str = None) -> dict:
return {"item_id": item_id, "q": q}

# 启动
(base) ➜ learn uvicorn main:app --reload

请求示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
(base) ➜  http http://127.0.0.1:8000
HTTP/1.1 200 OK
content-length: 18
content-type: application/json
date: Mon, 04 May 2020 18:52:08 GMT
server: uvicorn

{
"hello": "world!"
}

(base) ➜ http http://127.0.0.1:8000/items/5\?q\=shuke
HTTP/1.1 200 OK
content-length: 25
content-type: application/json
date: Mon, 04 May 2020 18:55:40 GMT
server: uvicorn

{
"item_id": 5,
"q": "shuke"
}

api docs

1
http://127.0.0.1:8000/docs

refs

1
2
https://learnku.com/python/t/38942
https://fastapi.tiangolo.com/

本文标题:python轻量级框架

文章作者:shuke

发布时间:2020年05月05日 - 02:05

最后更新:2020年05月05日 - 03:05

原始链接:https://shuke163.github.io/2020/05/05/python%E8%BD%BB%E9%87%8F%E7%BA%A7%E6%A1%86%E6%9E%B6/

许可协议: 署名-非商业性使用-禁止演绎 4.0 国际 转载请保留原文链接及作者。

-------------本文结束感谢您的阅读-------------

本文标题:python轻量级框架

文章作者:shuke

发布时间:2020年05月05日 - 02:05

最后更新:2020年05月05日 - 03:05

原始链接:https://shuke163.github.io/2020/05/05/python%E8%BD%BB%E9%87%8F%E7%BA%A7%E6%A1%86%E6%9E%B6/

许可协议: 署名-非商业性使用-禁止演绎 4.0 国际 转载请保留原文链接及作者。

0%