falcon

falcon

高性能REST API和微服务框架

Falcon是一款专注于构建关键REST API和微服务的ASGI/WSGI框架。该框架以可靠性、正确性和大规模性能为核心,提供简洁设计和原生HTTP支持。Falcon具有ASGI、WSGI和WebSocket兼容性,支持原生asyncio,无需依赖全局变量进行路由和状态管理。框架适用于CPython 3.8+和PyPy 3.8+环境,提供高度优化的可扩展代码库,简化API建模和请求处理流程。

FalconWeb框架PythonREST API性能优化Github开源项目

.. raw:: html

<a href="https://falconframework.org" target="_blank">
<img
    src="https://raw.githubusercontent.com/falconry/falcon/master/logo/banner.jpg"
    alt="Falcon web framework logo"
    style="width:100%"
>
</a>

|Build Status| |Docs| |codecov.io|

The Falcon Web Framework

Falcon <https://falconframework.org>__ is a minimalist ASGI/WSGI framework for building mission-critical REST APIs and microservices, with a focus on reliability, correctness, and performance at scale.

When it comes to building HTTP APIs, other frameworks weigh you down with tons of dependencies and unnecessary abstractions. Falcon cuts to the chase with a clean design that embraces HTTP and the REST architectural style.

Falcon apps work with any WSGI <https://www.python.org/dev/peps/pep-3333/>_ or ASGI <https://asgi.readthedocs.io/en/latest/>_ server, and run like a champ under CPython 3.8+ and PyPy 3.8+.

Quick Links

  • Read the docs <https://falcon.readthedocs.io/en/stable>_ (FAQ <https://falcon.readthedocs.io/en/stable/user/faq.html>_ - getting help <https://falcon.readthedocs.io/en/stable/community/help.html>_ - reference <https://falcon.readthedocs.io/en/stable/api/index.html>_)
  • Falcon add-ons and complementary packages <https://github.com/falconry/falcon/wiki>_
  • Falcon articles, talks and podcasts <https://github.com/falconry/falcon/wiki/Articles,-Talks-and-Podcasts>_
  • falconry/user for Falcon users <https://gitter.im/falconry/user>_ @ Gitter
  • falconry/dev for Falcon contributors <https://gitter.im/falconry/dev>_ @ Gitter

What People are Saying

"Falcon is rock solid and it's fast."

"We have been using Falcon as a replacement for [another framework] and we simply love the performance (three times faster) and code base size (easily half of our [original] code)."

"I'm loving #falconframework! Super clean and simple, I finally have the speed and flexibility I need!"

"Falcon looks great so far. I hacked together a quick test for a tiny server of mine and was ~40% faster with only 20 minutes of work."

"I feel like I'm just talking HTTP at last, with nothing in the middle. Falcon seems like the requests of backend."

"The source code for Falcon is so good, I almost prefer it to documentation. It basically can't be wrong."

"What other framework has integrated support for 786 TRY IT NOW ?"

Features

Falcon tries to do as little as possible while remaining highly effective.

  • ASGI, WSGI, and WebSocket support
  • Native asyncio support
  • No reliance on magic globals for routing and state management
  • Stable interfaces with an emphasis on backwards-compatibility
  • Simple API modeling through centralized RESTful routing
  • Highly-optimized, extensible code base
  • Easy access to headers and bodies through request and response classes
  • DRY request processing via middleware components and hooks
  • Strict adherence to RFCs
  • Idiomatic HTTP error responses
  • Straightforward exception handling
  • Snappy testing with WSGI/ASGI helpers and mocks
  • CPython 3.8+ and PyPy 3.8+ support

.. Patron list starts here. For Python package, we substitute this section with: Support Falcon Development

A Big Thank You to Our Patrons!

.. raw:: html

<p>
<a href="https://www.govcert.lu/" target="_blank"><img src="https://falconframework.org/assets/govcert.png" height="60" alt="CERT Gouvernemental Luxembourg" ></a>
 </p>

<p>
    <a href="https://www.kontrolnaya-rabota.ru/s/" target="_blank"><img src="https://falconframework.org/assets/rabota.jpg" height="30" alt="Examination RU" style="margin-right: 10px"></a>

    <a href="https://www.pnk.sh/python-falcon" target="_blank"><img src="https://falconframework.org/assets/paris.svg" height="30" alt="Paris Kejser" style="margin-right: 10px"></a>

    <a href="https://www.algolia.com" target="_blank" style="margin-right: 10px"><img src="https://falconframework.org/assets/algolia.svg" height="30" alt="Algolia"></a>

    <a href="https://www.salesforce.com" target="_blank"><img src="https://falconframework.org/assets/salesforce.svg" height="30" alt="Salesforce"></a>
</p>

<p>
    <a href="https://www.misaka.io" target="_blank" style="margin-right: 10px"><img src="https://falconframework.org/assets/misaka.svg" height="30" alt="Misaka Network"></a>
    <a href="https://github.com/LikaloLLC" target="_blank" style="margin-right: 10px"><img src="https://falconframework.org/assets/likalo.png" height="30" alt="Likalo"></a>
</p>

.. Patron list ends here (see the comment above this section).

Has Falcon helped you make an awesome app? Show your support today with a one-time donation or by becoming a patron. Supporters get cool gear, an opportunity to promote their brand to Python developers, and prioritized support.

  • Learn how to support Falcon development <https://falconframework.org/#sectionSupportFalconDevelopment>_

Thanks!

How is Falcon Different?

Perfection is finally attained not when there is no longer anything
to add, but when there is no longer anything to take away.

*- Antoine de Saint-Exupéry*

We designed Falcon to support the demanding needs of large-scale microservices and responsive app backends. Falcon complements more general Python web frameworks by providing bare-metal performance, reliability, and flexibility wherever you need it.

Reliable. We go to great lengths to avoid introducing breaking changes, and when we do they are fully documented and only introduced (in the spirit of SemVer <http://semver.org/>_) with a major version increment. The code is rigorously tested with numerous inputs and we require 100% coverage at all times. Falcon has no dependencies outside the standard library, helping minimize your app's attack surface while avoiding transitive bugs and breaking changes.

Debuggable. Falcon eschews magic. It's easy to tell which inputs lead to which outputs. Unhandled exceptions are never encapsulated or masked. Potentially surprising behaviors, such as automatic request body parsing, are well-documented and disabled by default. Finally, when it comes to the framework itself, we take care to keep logic paths simple and understandable. All this makes it easier to reason about the code and to debug edge cases in large-scale deployments.

Fast. Same hardware, more requests. Falcon turns around requests significantly faster than other popular Python frameworks like Django and Flask. For an extra speed boost, Falcon compiles itself with Cython when available, and also works well with PyPy <https://pypy.org>_. Considering a move to another programming language? Benchmark with Falcon+PyPy first!

Flexible. Falcon leaves a lot of decisions and implementation details to you, the API developer. This gives you a lot of freedom to customize and tune your implementation. It also helps you understand your apps at a deeper level, making them easier to tune, debug, and refactor over the long run. Falcon's minimalist design provides space for Python community members to independently innovate on Falcon add-ons and complementary packages <https://github.com/falconry/falcon/wiki>_.

Who's Using Falcon?

Falcon is used around the world by a growing number of organizations, including:

  • 7ideas
  • Cronitor
  • EMC
  • Hurricane Electric
  • Leadpages
  • OpenStack
  • Rackspace
  • Shiftgig
  • tempfil.es
  • Opera Software

If you are using the Falcon framework for a community or commercial project, please consider adding your information to our wiki under Who's Using Falcon? <https://github.com/falconry/falcon/wiki/Who's-using-Falcon%3F>_

Community

A number of Falcon add-ons, templates, and complementary packages are available for use in your projects. We've listed several of these on the Falcon wiki <https://github.com/falconry/falcon/wiki>_ as a starting point, but you may also wish to search PyPI for additional resources.

The Falconry community on Gitter is a great place to ask questions and share your ideas. You can find us in falconry/user <https://gitter.im/falconry/user>. We also have a falconry/dev <https://gitter.im/falconry/dev> room for discussing the design and development of the framework itself.

Per our Code of Conduct <https://github.com/falconry/falcon/blob/master/CODEOFCONDUCT.md>_, we expect everyone who participates in community discussions to act professionally, and lead by example in encouraging constructive discussions. Each individual in the community is responsible for creating a positive, constructive, and productive culture.

Installation

PyPy ^^^^

PyPy <http://pypy.org/>__ is the fastest way to run your Falcon app. PyPy3.8+ is supported as of PyPy v7.3.7+.

.. code:: bash

$ pip install falcon

Or, to install the latest beta or release candidate, if any:

.. code:: bash

$ pip install --pre falcon

CPython ^^^^^^^

Falcon also fully supports CPython <https://www.python.org/downloads/>__ 3.8+.

The latest stable version of Falcon can be installed directly from PyPI:

.. code:: bash

$ pip install falcon

Or, to install the latest beta or release candidate, if any:

.. code:: bash

$ pip install --pre falcon

In order to provide an extra speed boost, Falcon can compile itself with Cython. Wheels containing pre-compiled binaries are available from PyPI for several common platforms. However, if a wheel for your platform of choice is not available, you can install the source distribution. The installation process will automatically try to cythonize Falcon for your environment, falling back to a normal pure-Python install if any issues are encountered during the cythonization step:

.. code:: bash

$ pip install --no-binary :all: falcon

If you want to verify that Cython is being invoked, simply pass the verbose flag -v to pip in order to echo the compilation commands.

The cythonization step is only active when using the CPython Python implementation, so installing using PyPy will skip it. If you want to skip Cython compilation step and install the pure-Python version directly you can set the environment variable FALCON_DISABLE_CYTHON to a non empty value before install:

.. code:: bash

$ FALCON_DISABLE_CYTHON=Y pip install -v --no-binary :all: falcon

Please note that pip>=10 is required to be able to install Falcon from source.

Installing on OS X

Xcode Command Line Tools are required to compile Cython. Install them with this command:

.. code:: bash

$ xcode-select --install

The Clang compiler treats unrecognized command-line options as errors, for example:

.. code:: bash

clang: error: unknown argument: '-mno-fused-madd' [-Wunused-command-line-argument-hard-error-in-future]

You might also see warnings about unused functions. You can work around these issues by setting additional Clang C compiler flags as follows:

.. code:: bash

$ export CFLAGS="-Qunused-arguments -Wno-unused-function"

Dependencies ^^^^^^^^^^^^

Falcon does not require the installation of any other packages, although if Cython has been installed into the environment, it will be used to optimize the framework as explained above.

WSGI Server

Falcon speaks WSGI <https://www.python.org/dev/peps/pep-3333/>_ (or ASGI <https://asgi.readthedocs.io/en/latest/>_; see also below). In order to serve a Falcon app, you will need a WSGI server. Gunicorn and uWSGI are some of the more popular ones out there, but anything that can load a WSGI app will do.

.. code:: bash

$ pip install [gunicorn|uwsgi]

ASGI Server

In order to serve a Falcon ASGI app, you will need an ASGI server. Uvicorn is a popular choice:

.. code:: bash

$ pip install uvicorn

Source Code

Falcon lives on GitHub <https://github.com/falconry/falcon>_, making the code easy to browse, download, fork, etc. Pull requests are always welcome! Also, please remember to star the project if it makes you happy. :)

Once you have cloned the repo or downloaded a tarball from GitHub, you can install Falcon like this:

.. code:: bash

$ cd falcon
$ pip install .

Or, if you want to edit the code, first fork the main repo, clone the fork to your desktop, and then run the following to install it using symbolic linking, so that when you change your code, the changes will be automagically available to your app without having to reinstall the package:

.. code:: bash

$ cd falcon
$ pip install -e .

You can manually test changes to the Falcon framework by switching to the directory of the cloned repo and then running pytest:

.. code:: bash

$ cd falcon
$ pip install -r requirements/tests
$ pytest tests

Or, to run the default set of tests:

.. code:: bash

$ pip install tox && tox

See also the tox.ini <https://github.com/falconry/falcon/blob/master/tox.ini>_ file for a full list of available environments.

Read the Docs

The docstrings in the Falcon code base are quite extensive, and we recommend keeping a REPL running while learning the framework so that you can query the various modules and classes as you have questions.

Online docs are available at: https://falcon.readthedocs.io

You can build the same docs locally as follows:

.. code:: bash

$ pip install tox && tox -e docs

Once the docs have been built, you can view them by opening the following index page in your browser. On OS X it's as simple as::

$ open docs/_build/html/index.html

Or on Linux:

.. code:: bash

$ xdg-open docs/_build/html/index.html

Getting Started

Here is a simple, contrived example showing how to create a Falcon-based WSGI app (the ASGI version is included further down):

.. code:: python

# examples/things.py

# Let's get this party started!
from wsgiref.simple_server import make_server

import falcon


# Falcon follows the REST architectural style, meaning (among
# other things) that you think in terms of resources and state
# transitions, which map to HTTP verbs.
class ThingsResource:
    def on_get(self, req, resp):
        """Handles GET requests"""
        resp.status = falcon.HTTP_200  # This is the default status
        resp.content_type = falcon.MEDIA_TEXT  # Default is JSON, so override
        resp.text = ('\nTwo things awe me most, the starry sky '
                     'above me and the moral law within me.\n'
                     '\n'
                     '    ~ Immanuel Kant\n\n')


# falcon.App instances are callable WSGI apps...
# in larger applications the app is created in a separate file
app = falcon.App()

# Resources are represented by long-lived class instances
things = ThingsResource()

# things will handle all requests to the '/things' URL path
app.add_route('/things', things)

if __name__ == '__main__':
    with make_server('', 8000, app) as httpd:
        print('Serving on port 8000...')

        # Serve until process is killed
        httpd.serve_forever()

You can run the above example directly using the included wsgiref server:

.. code:: bash

$ pip install falcon
$ python things.py

Then, in another terminal:

.. code:: bash

$ curl localhost:8000/things

The ASGI version of the example is similar:

.. code:: python

# examples/things_asgi.py

import falcon
import falcon.asgi


# Falcon follows the REST architectural style, meaning (among
# other things) that you think in terms of resources and state
# transitions, which map to HTTP verbs.
class ThingsResource:
    async def on_get(self, req, resp):
        """Handles GET requests"""
        resp.status = falcon.HTTP_200  # This is the default status
        resp.content_type = falcon.MEDIA_TEXT  # Default is JSON, so override
        resp.text = ('\nTwo things awe me most, the starry sky '
                     'above me and the moral law within me.\n'
                     '\n'
                     '    ~ Immanuel Kant\n\n')


# falcon.asgi.App instances are callable ASGI apps...
# in larger applications the app is created in a separate file
app = falcon.asgi.App()

# Resources are represented by long-lived class instances
things = ThingsResource()

# things will handle all requests to the '/things' URL path
app.add_route('/things', things)

You can run the ASGI version with uvicorn or any other ASGI server:

.. code:: bash

$ pip install falcon uvicorn

编辑推荐精选

TRAE编程

TRAE编程

AI辅助编程,代码自动修复

Trae是一种自适应的集成开发环境(IDE),通过自动化和多元协作改变开发流程。利用Trae,团队能够更快速、精确地编写和部署代码,从而提高编程效率和项目交付速度。Trae具备上下文感知和代码自动完成功能,是提升开发效率的理想工具。

AI工具TraeAI IDE协作生产力转型热门
蛙蛙写作

蛙蛙写作

AI小说写作助手,一站式润色、改写、扩写

蛙蛙写作—国内先进的AI写作平台,涵盖小说、学术、社交媒体等多场景。提供续写、改写、润色等功能,助力创作者高效优化写作流程。界面简洁,功能全面,适合各类写作者提升内容品质和工作效率。

AI辅助写作AI工具蛙蛙写作AI写作工具学术助手办公助手营销助手AI助手
问小白

问小白

全能AI智能助手,随时解答生活与工作的多样问题

问小白,由元石科技研发的AI智能助手,快速准确地解答各种生活和工作问题,包括但不限于搜索、规划和社交互动,帮助用户在日常生活中提高效率,轻松管理个人事务。

热门AI助手AI对话AI工具聊天机器人
Transly

Transly

实时语音翻译/同声传译工具

Transly是一个多场景的AI大语言模型驱动的同声传译、专业翻译助手,它拥有超精准的音频识别翻译能力,几乎零延迟的使用体验和支持多国语言可以让你带它走遍全球,无论你是留学生、商务人士、韩剧美剧爱好者,还是出国游玩、多国会议、跨国追星等等,都可以满足你所有需要同传的场景需求,线上线下通用,扫除语言障碍,让全世界的语言交流不再有国界。

讯飞智文

讯飞智文

一键生成PPT和Word,让学习生活更轻松

讯飞智文是一个利用 AI 技术的项目,能够帮助用户生成 PPT 以及各类文档。无论是商业领域的市场分析报告、年度目标制定,还是学生群体的职业生涯规划、实习避坑指南,亦或是活动策划、旅游攻略等内容,它都能提供支持,帮助用户精准表达,轻松呈现各种信息。

AI办公办公工具AI工具讯飞智文AI在线生成PPTAI撰写助手多语种文档生成AI自动配图热门
讯飞星火

讯飞星火

深度推理能力全新升级,全面对标OpenAI o1

科大讯飞的星火大模型,支持语言理解、知识问答和文本创作等多功能,适用于多种文件和业务场景,提升办公和日常生活的效率。讯飞星火是一个提供丰富智能服务的平台,涵盖科技资讯、图像创作、写作辅助、编程解答、科研文献解读等功能,能为不同需求的用户提供便捷高效的帮助,助力用户轻松获取信息、解决问题,满足多样化使用场景。

热门AI开发模型训练AI工具讯飞星火大模型智能问答内容创作多语种支持智慧生活
Spark-TTS

Spark-TTS

一种基于大语言模型的高效单流解耦语音令牌文本到语音合成模型

Spark-TTS 是一个基于 PyTorch 的开源文本到语音合成项目,由多个知名机构联合参与。该项目提供了高效的 LLM(大语言模型)驱动的语音合成方案,支持语音克隆和语音创建功能,可通过命令行界面(CLI)和 Web UI 两种方式使用。用户可以根据需求调整语音的性别、音高、速度等参数,生成高质量的语音。该项目适用于多种场景,如有声读物制作、智能语音助手开发等。

咔片PPT

咔片PPT

AI助力,做PPT更简单!

咔片是一款轻量化在线演示设计工具,借助 AI 技术,实现从内容生成到智能设计的一站式 PPT 制作服务。支持多种文档格式导入生成 PPT,提供海量模板、智能美化、素材替换等功能,适用于销售、教师、学生等各类人群,能高效制作出高品质 PPT,满足不同场景演示需求。

讯飞绘文

讯飞绘文

选题、配图、成文,一站式创作,让内容运营更高效

讯飞绘文,一个AI集成平台,支持写作、选题、配图、排版和发布。高效生成适用于各类媒体的定制内容,加速品牌传播,提升内容营销效果。

热门AI辅助写作AI工具讯飞绘文内容运营AI创作个性化文章多平台分发AI助手
材料星

材料星

专业的AI公文写作平台,公文写作神器

AI 材料星,专业的 AI 公文写作辅助平台,为体制内工作人员提供高效的公文写作解决方案。拥有海量公文文库、9 大核心 AI 功能,支持 30 + 文稿类型生成,助力快速完成领导讲话、工作总结、述职报告等材料,提升办公效率,是体制打工人的得力写作神器。

下拉加载更多