sanic README

The project's README from github.com/sanic-org/sanic. As one markdown file: README.md.

.. image:: https://raw.githubusercontent.com/sanic-org/sanic-assets/master/png/sanic-framework-logo-400x97.png :alt: Sanic | Build fast. Run fast.

Sanic | Build fast. Run fast.

.. start-badges

.. list-table:: :widths: 15 85 :stub-columns: 1

* - Build
  - | |Tests|
* - Docs
  - | |UserGuide| |Documentation|
* - Package
  - | |PyPI| |PyPI version| |Wheel| |Supported implementations| |Code style ruff|
* - Support
  - | |Forums| |Discord| |Awesome|
* - Stats
  - | |Monthly Downloads| |Weekly Downloads| |Conda downloads|

.. |UserGuide| image:: https://img.shields.io/badge/user%20guide-sanic-ff0068 :target: https://sanic.dev/ .. |Forums| image:: https://img.shields.io/badge/forums-community-ff0068.svg :target: https://community.sanicframework.org/ .. |Discord| image:: https://img.shields.io/discord/812221182594121728?logo=discord&label=Discord&color=5865F2 :target: https://discord.gg/FARQzAEMAA .. |Tests| image:: https://github.com/sanic-org/sanic/actions/workflows/tests.yml/badge.svg?branch=main :target: https://github.com/sanic-org/sanic/actions/workflows/tests.yml .. |Documentation| image:: https://readthedocs.org/projects/sanic/badge/?version=latest :target: http://sanic.readthedocs.io/en/latest/?badge=latest .. |PyPI| image:: https://img.shields.io/pypi/v/sanic.svg :target: https://pypi.python.org/pypi/sanic/ .. |PyPI version| image:: https://img.shields.io/pypi/pyversions/sanic.svg :target: https://pypi.python.org/pypi/sanic/ .. |Code style ruff| image:: https://img.shields.io/badge/code%20style-ruff-000000.svg :target: https://docs.astral.sh/ruff/ .. |Wheel| image:: https://img.shields.io/pypi/wheel/sanic.svg :alt: PyPI Wheel :target: https://pypi.python.org/pypi/sanic .. |Supported implementations| image:: https://img.shields.io/pypi/implementation/sanic.svg :alt: Supported implementations :target: https://pypi.python.org/pypi/sanic .. |Awesome| image:: https://cdn.rawgit.com/sindresorhus/awesome/d7305f38d29fed78fa85652e3a63e154dd8e8829/media/badge.svg :alt: Awesome Sanic List :target: https://github.com/mekicha/awesome-sanic .. |Monthly Downloads| image:: https://img.shields.io/pypi/dm/sanic.svg :alt: Downloads :target: https://pepy.tech/project/sanic .. |Weekly Downloads| image:: https://img.shields.io/pypi/dw/sanic.svg :alt: Downloads :target: https://pepy.tech/project/sanic .. |Conda downloads| image:: https://img.shields.io/conda/dn/conda-forge/sanic.svg :alt: Downloads :target: https://anaconda.org/conda-forge/sanic

.. end-badges

Sanic is a Python 3.10+ web server and web framework that's written to go fast. It allows the usage of the async/await syntax added in Python 3.5, which makes your code non-blocking and speedy.

Sanic is also ASGI compliant, so you can deploy it with an alternative ASGI webserver <https://sanicframework.org/en/guide/deployment/running.html#asgi>_.

Source code on GitHub <https://github.com/sanic-org/sanic/>_ | Help and discussion board <https://community.sanicframework.org/>_ | User Guide <https://sanicframework.org>_ | Chat on Discord <https://discord.gg/FARQzAEMAA>_

The project is maintained by the community, for the community. Contributions are welcome!

The goal of the project is to provide a simple way to get up and running a highly performant HTTP server that is easy to build, to expand, and ultimately to scale.

Sponsor

Check out open collective <https://opencollective.com/sanic-org>_ to learn more about helping to fund Sanic.

Installation

pip install sanic

Sanic makes use of ``uvloop`` and ``ujson`` to help with performance. If you do not want to use those packages, simply add an environmental variable ``SANIC_NO_UVLOOP=true`` or ``SANIC_NO_UJSON=true`` at install time.

.. code:: shell

   $ export SANIC_NO_UVLOOP=true
   $ export SANIC_NO_UJSON=true
   $ pip install --no-binary :all: sanic

.. note::

If you are running on a clean install of Fedora 28 or above, please make sure you have the redhat-rpm-config package installed in case if you want to use sanic with ujson dependency.

Hello World Example

.. code:: python

from sanic import Sanic
from sanic.response import json

app = Sanic("my-hello-world-app")

@app.route('/')
async def test(request):
    return json({'hello': 'world'})

Sanic can now be easily run from CLI using sanic hello.app.

.. code::

[2018-12-30 11:37:41 +0200] [13564] [INFO] Goin' Fast @ http://127.0.0.1:8000
[2018-12-30 11:37:41 +0200] [13564] [INFO] Starting worker [13564]

And, we can verify it is working: curl localhost:8000 -i

.. code::

HTTP/1.1 200 OK
Connection: keep-alive
Keep-Alive: 5
Content-Length: 17
Content-Type: application/json

{"hello":"world"}

Now, let's go build something fast!

Minimum Python version is 3.10.

Documentation

User Guide, Changelog, and API Documentation can be found at sanic.dev <https://sanic.dev>__.

Questions and Discussion

Ask a question or join the conversation <https://community.sanicframework.org/>__.

Contribution

We are always happy to have new contributions. We have marked issues good for anyone looking to get started <https://github.com/sanic-org/sanic/issues?q=is%3Aopen+is%3Aissue+label%3Abeginner>, and welcome questions on the forums <https://community.sanicframework.org/>. Please take a look at our Contribution guidelines <https://github.com/sanic-org/sanic/blob/master/CONTRIBUTING.md>_.

sanic (sanic-org/sanic)

Searching Database…

18.6K ·GitHub
这是什么?Sanic 是一个用 Python 3.10+ 编写的 Web 服务器兼 Web 框架,目标就是跑得快——你用它提供的框架来搭建应用,它再以高性能通过 HTTP 把应用跑起来。
那它到底是框架还是 Web 服务器?我装下来的究竟是什么?两者打包在一起:它既带有供你的应用使用的路由框架,也自带一个 Web 服务器;同时它兼容 ASGI,所以你也可以把它部署到别的 ASGI 服务器上运行。
怎么把第一个应用跑起来?先运行 pip install sanic,然后用路由装饰器定义一个应用,再在命令行里用 'sanic hello.app' 启动——它会跑起来并在 localhost:8000 提供服务,可以直接用 curl 测试。
我需要哪个 Python 版本?至少要 Python 3.10——这是官方写明的最低版本,更旧的解释器跑不了。
安装时有什么坑需要注意吗?为了提速它会附带安装 uvloop 和 ujson;如果在安装时设置 SANIC_NO_UVLOOP=true 或 SANIC_NO_UJSON=true,并运行 pip install --no-binary :all: sanic,就可以跳过它们。在干净的 Fedora 28+ 系统上,要装 ujson 还需要先安装 redhat-rpm-config 包。
用它的前提是得懂 Python 的 async/await 吗?是的——处理函数要用 async/await 写成异步函数,正是这一点让你的代码不阻塞、跑得快,所以熟悉 Python 的 asyncio 会很有帮助。
一个最小应用长什么样?只要几行代码:创建应用,给一个异步函数加上 @app.route('/') 装饰器,然后返回一个 JSON 响应——README 里的 hello world 就是在根路由返回 {'hello': 'world'}。
为什么一直强调'写得快,跑得快'?速度就是它的全部卖点:设计上就用不阻塞的异步处理函数,并且明确以打造一个高性能 HTTP 服务器为目标——容易构建、容易扩展,最终也容易横向扩容。
What are similar projects?uvicorn, starlette, aiohttp, hapi, beego, Rocket, gunicorn, Perfect
Creator
sanic-org
Size
10.7 MB
Stars
18.6K
Age
10y
Commits · 90 Days