Python Falcon 简明教程
Python Falcon - Resource Class
Falcon 的设计借鉴了 REST 架构样式中的几个关键概念。REST 的含义是 Relational State Transfer 。REST 定义了 Web 应用程序架构的行为方式。
Falcon’s design borrows several key concepts from the REST architectural style. REST stands for Relational State Transfer. REST defines how the architecture of web applications should behave.
REST 是一种基于资源的架构。在此,REST 服务器承载的所有内容(无论是文件、图像还是数据库中表格中的行)都被视为资源,可能有多种表现形式。REST API 提供对这些资源的受控访问,以便客户端可以检索和修改它们。
REST is a resource-based architecture. Here, everything that the REST server hosts, be it a file, an image or row in a table of a database, is treated as a resource, which may have many representations. The REST API provides a controlled access to these resources so that the client can retrieve and modify them.
服务器上的资源应仅有一个统一资源标识符 (URI)。它只标识资源;它不指定对该资源执行的操作。相反,用户从一组标准方法中进行选择。用于对资源执行操作的 HTTP 动词或方法。POST、GET、PUT 和 DELETE 方法分别执行 CREATE、READ、UPDATE 和 DELETE 操作。
A resource with the server should have only one uniform resource identifier (URI). It only identifies the resource; it does not specify what action to take on that resource. Instead, users choose from a set of standard methods. HTTP verb or method to be used for the operation on the resources. The POST, GET, PUT and DELETE methods perform CREATE, READ, UPDATE and DELETE operations respectively.
Falcon 使用普通的 Python 类来表示资源。此类在应用程序中充当控制器。它将传入请求转换为一个或多个内部操作,然后根据这些操作的结果将响应返回给客户端。
Falcon uses normal Python classes to represent resources. Such a class acts as a controller in your application. It converts an incoming request into one or more internal actions, and then compose a response back to the client based on the results of those actions.

每个资源类定义各种 "responder" 方法,每个方法对应于资源允许的一种 HTTP 方法。响应程序名称以 "on_" 开头,并且根据它们处理的 HTTP 方法命名,例如 on_get(), on_post(), on_put(), 等。
Each resource class defines various "responder" methods, one for each HTTP method the resource allows. Responder names start with "on_" and are named according to which HTTP method they handle, as in on_get(), on_post(), on_put(), etc.
在 hellofalcon.py 以上使用的示例代码中, HelloResource (资源类)具有 on_get() 响应程序方法。响应程序必须始终定义至少两个参数来接收请求和响应对象。
In the hellofalcon.py example code used above, HelloResource (the resource class) has an on_get() responder method. Responders must always define at least two arguments to receive Request and Response objects.
import falcon
class HelloResource:
def on_get(self, req, resp):
"""Handles GET requests"""
resp.status = falcon.HTTP_200
resp.content_type = falcon.MEDIA_TEXT
resp.text = (
'Hello World'
)
对于 ASGI 应用程序,响应程序必须是一个协程函数,即必须使用 async 关键字进行定义。
For ASGI apps, the responder must be a coroutine function, i.e. must be defined with async keyword.
class HelloResource:
async def on_get(self, req, resp):
"""Handles GET requests"""
resp.status = falcon.HTTP_200
resp.content_type = falcon.MEDIA_TEXT
resp.text = (
'Hello World'
)
Request object 表示传入的 HTTP 请求。可以通过此对象访问请求相关的请求头、查询字符串参数和其他元数据。
The Request object represents the incoming HTTP request. Request headers, query string parameters, and other metadata associated with the request can be accessed through this object.
Response 对象表示应用程序对请求的 HTTP 响应。此对象的属性和方法设置状态、标头和正文数据。它还公开了一个类似 dict 的上下文属性,用于将任意数据传递给挂钩和其他中间件方法。
The Response object represents the application’s HTTP response to the request. Properties and methods of this object set status, header and body data. It also exposes a dict-like context property for passing arbitrary data to hooks and other middleware methods.
请注意 HelloResource 在上面的示例中只是一个普通的 Python 类。它可以具有任何名称;但是,惯例是将其命名为 xxxResource 。
Note that HelloResource in the above example is just a normal Python class. It can have any name; however, the convention is to name it as xxxResource.