Python Falcon 简明教程
Python Falcon - Introduction
Falcon 是一个 Python 库,用于开发任务关键型 REST API 和微服务。它同时支持 WSGI 和 ASGI 规范。Falcon 框架由 Kurt Griffiths 于 2013 年 1 月开发。Falcon 的最新版本是 3.1.0,于 2022 年 3 月发布。
Falcon is a Python library for developing mission-critical REST APIs and microservices. It supports both WSGI and ASGI specifications. Falcon framework has been developed by Kurt Griffiths in Jan. 2013. The latest version of Falcon is 3.1.0, released in March 2022.
Falcon 是一款轻量级的 Web 开发框架。其极简主义设计允许开发人员根据需要选择最佳策略和第三方包。
Falcon is a lightweight web development framework. Its minimalist design allows the developer to select the best strategies and 3rd-party packages as required.
Falcon - Important Features
Falcon 根据 Apache 2.0 License 条款发布。
Falcon is released under the terms of the Apache 2.0 License.
Falcon 的一些重要功能包括 −
Some of the important features of Falcon include −
-
Latest version of Falcon supports ASGI, WSGI, as well as WebSocket.
-
Falcon provides native support for asyncio.
-
Its stable interfaces ensure backwards-compatibility
-
Falcon follows REST architectural style for building APIs.
-
Class based construction of HTTP resources.
-
Highly-optimized, extensible code base.
-
Falcon provides easy access to headers and bodies through request and response classes
-
Middleware components and hooks available for DRY request processing.
-
Idiomatic HTTP error responses and exception handling.
Falcon - Design Philosophy
Falcon 最大限度减少对象实例化数量,以免创建对象时花费太多开销,并减少内存使用。同一个实例将用于处理该路由上发来的所有请求。
Falcon minimizes the instantiation of number of objects so as to avoid the expense of creating the object, and to reduce memory usage. The same instance will be used to serve all requests coming in on that route.
-
Exceptions are properly handled by the resource responders (methods such as on_get(), on_post(), etc.). Falcon doesn’t try very hard to protect responder code from itself. A high-quality Falcon API should fulfil following requirements −
-
The Falcon framework is thread-safe. Separate new Request and Response objects are created for each incoming HTTP request. However, a single instance of each resource class attached to a route is shared among all requests. Middleware objects, hooks, and custom error handlers, are also shared. Therefore, your WSGI app as a whole will be thread-safe.
-
Starting with version 3.0, Falcon supports asyncio. Use the falcon.asgi.App class to create an async application, and serve it via an ASGI application server such as Uvicorn.
-
The async version of Falcon supports the ASGI WebSocket protocol.
Falcon - Comparison with Other Frameworks
Python Web 框架有两大类 − full-stack 和 micro 框架。
There are two major categories of Python web frameworks − full-stack and micro frameworks.
-
Full-stack frameworks come with built-in features and libraries. Django, Turbogears, and Web2Py are full-stack frameworks.
-
In contrast, micro-frameworks are minimalistic, only providing the bare minimum; thus gives developers the freedom to choose official or third-party extensions and only include plugins which they need. Flask, Falcon, Pyramid belong to micro framework category.
我们根据以下参数将 Falcon 框架与不同框架进行比较 −
We compare Falcon framework against different frameworks on the basis of the following parameters −
Performance
Falcon 应用非常快,与 Flask 和 Pyramid 等微框架相比。而全栈框架通常很慢。
Falcon application is very fast, in comparison with micro frameworks such as Flask and pyramid. The full stack frameworks are generally slow.
REST Support
Falcon 是一个旨在用于开发 REST API 和微服务的框架。FastAPI 也支持 REST 开发。Flask 和 Django 没有内置的 REST 支持。但是,可以使用扩展名启用它。
Falcon is intended to be a framework of choice for development of REST APIs and microservices. FastAPI also encourages REST development. Flask and Django don’t have built-in REST support. However, it can be enabled using extensions.
Templating
Falcon 应用程序不应提供模板网页。它未与任何模板库捆绑在一起。不过,可以使用 jinja2 或 Macho 库。另一方面,Flask 对 jinja2 有内置支持。Django 有自己的模板库。FastAPI 还可以处理任何选择的模板库。
Falcon app is not supposed to serve template web pages. It is not bundled with any templating library. However, one can use jinja2 or Macho libraries. On the other hand, Flask has a built-in support for jinja2. Django has its own templating library. FastAPI also can handle any template library of choice.
Database Support
在 Falcon 中,没有内置的数据库支持。可以使用 SQLAlchemy 模型与关系数据库(如 MyQL、PostgreSQL、SQLite 等)进行交互。另一方面,Django 具有自己的 ORM 框架,可以开箱即用。
In Falcon database support is not built-in. It is possible to use SQLAlchemy models to interact with relational databases like MyQL, PostgreSQL, SQLite etc. Django on the other hand has its own ORM framework for use out of the box.
Flask 应用程序还可以通过 Flask 扩展与数据库进行交互。TurboGears 的早期版本与 SQLObject ORM 库兼容。较新版本与 SQLAlchemy 兼容。
A Flask application also can interact with databases through Flask extensions. Earlier versions of TurboGears had compatibility with SQLObject ORM library. The newer version is compatible with SQLAlchemy.
Flexibility
Falcon 应用程序非常灵活。它是要求高度定制和性能调优的应用程序的理想选择。FastAPI 和 Flask 也能灵活地进行编码,并且不会限制用户使用特定的项目或代码布局。
Falcon applications are very flexible. It is ideal for applications that require a high degree of customization and performance tuning. FastAPI and Flask too are flexible to code and doesn’t restrict users to a particular project or code layout.
Security
Falcon 没有内置的安全保障支持。Django 和 FastAPI 等其他框架可确保高度的安全性。Flask 也提供了出色的防护功能,可以抵御 CSRF 和 XSS 攻击等安全威胁。
Falcon has no built-in support to ensure security. Other frameworks like Django and FastAPI ensure high degree of security. Flask also provides excellent protection against security threats such as CSRF and XSS attacks.