- Python Falcon 教程
- Python Falcon - 首页
- Python Falcon - 简介
- Python Falcon - 环境设置
- Python Falcon - WSGI vs ASGI
- Python Falcon - Hello World(WSGI)
- Python Falcon - Waitress
- Python Falcon - ASGI
- Python Falcon - Uvicorn
- Python Falcon - API 测试工具
- 请求 & 响应
- Python Falcon - 资源类
- Python Falcon - 应用类
- Python Falcon - 路由
- Falcon - 后缀响应器
- Python Falcon - Inspect 模块
- Python Falcon - Jinja2 模板
- Python Falcon - Cookies
- Python Falcon - 状态码
- Python Falcon - 错误处理
- Python Falcon - 钩子
- Python Falcon - 中间件
- Python Falcon - CORS
- Python Falcon - WebSocket
- Python Falcon - SQLAlchemy 模型
- Python Falcon - 测试
- Python Falcon - 部署
- Python Falcon 有用资源
- Python Falcon - 快速指南
- Python Falcon - 有用资源
- Python Falcon - 讨论
Python Falcon - 钩子
钩子是在响应客户端请求时,当资源类中特定响应器方法被调用时,自动执行的用户定义函数。Falcon 支持 **before** 和 **after** 钩子。
用作钩子的函数,除了任何可能需要的可选参数外,还以请求、响应和资源类作为参数进行定义。
def hookfunction(req, resp, resource): . . . . . . . . . .
通过应用以下装饰器之一,将此类函数附加到单个响应器或整个资源类:-
@falcon.before(hookfunction)
@falcon.after(hookfunction)
将 before 钩子应用于 **on_post()** 响应器 -
@falcon.before(hookfunction) def on_post(self, req, resp): . . . . . .
应用 after 钩子 -
@falcon.after(hookfunction) def on_get(self, req, resp): . . . . . .
要装饰整个资源类,请在类声明的上方使用装饰器 -
@falcon.after(hookfunction) class SomeResource: def on_get(self, req, resp): . . . . . . def on_post(self, req, resp): . . . . . .
在以下示例中,我们有 **StudentResource** 类,其中定义了 **on_get()** 和 **on_post()** 响应器。当 POST 请求发送一些数据时,会调用 **on_post()** 响应器,并使用它创建一个新的 **dict** 对象,并将其添加到 **Students** 列表中。
在处理之前需要验证接收到的数据。为此,定义了以下函数。它检查 percent 参数的值是否在 0 到 100 之间。只有当数据通过此条件时,才会将其传递给响应器。
def checkinput(req, resp, resource,params):
student = json.load(req.bounded_stream)
if "name" not in student:
raise falcon.HTTPBadRequest(
title="Bad request", description="Bad input, name must be provided."
)
per=int(student['percent'])
if per<0 or per>100:
raise falcon.HTTPBadRequest(
title="Bad request", description="Bad input, invalid percentage"
)
req.context.data = student
此函数作为钩子应用于 **StudentResource** 类的 **on_post()** 响应器。
import falcon
import json
from waitress import serve
students = [
{"id": 1, "name": "Ravi", "percent": 75.50},
{"id": 2, "name": "Mona", "percent": 80.00},
{"id": 3, "name": "Mathews", "percent": 65.25},
]
class StudentResource:
def on_get(self, req, resp):
resp.text = json.dumps(students)
resp.status = falcon.HTTP_OK
resp.content_type = falcon.MEDIA_JSON
@falcon.before(checkinput)
def on_post(self, req, resp):
student = json.load(req.context.data)
students.append(student)
resp.text = "Student added successfully."
resp.status = falcon.HTTP_OK
resp.content_type = falcon.MEDIA_TEXT
def on_get_student(self, req, resp, id):
resp.text = json.dumps(students[id-1])
resp.status = falcon.HTTP_OK
resp.content_type = falcon.MEDIA_JSON
app = falcon.App()
app.add_route("/students", StudentResource())
if __name__ == '__main__':
serve(app, host='0.0.0.0', port=8000)
让我们运行 **Waitress** 服务器并启动 POST 请求。
http POST localhost:8000/students id=4 percent=50
HTTP/1.1 400 Bad Request
Content-Length: 76
Content-Type: application/json
Date: Tue, 26 Apr 2022 14:49:07 GMT
Server: waitress
Vary: Accept {
"description": "Bad input, name must be provided.",
"title": "Bad request"
}
由于数据不包含 name 参数的值,因此引发了异常。
在另一个 POST 请求中,如下所示,percent 参数的值未能满足所需条件,因此引发了异常。
http POST localhost:8000/students id=4 name="aaa" percent=500
HTTP/1.1 400 Bad Request
Content-Length: 72
Content-Type: application/json
Date: Tue, 26 Apr 2022 15:01:20 GMT
Server: waitress
Vary: Accept {
"description": "Bad input, invalid percentage",
"title": "Bad request"
}
广告