Why choose to learn the Sanic framework

Get into the habit of writing together! This is the 13th day of my participation in the "Nuggets Daily New Plan · April Update Challenge", click to view the details of the event .

Web Server and Framework

Sanic describes itself as both a web framework and a web server. What does it mean? More importantly, why does this matter?

So what is Web Server?

Web Server

A web server is software designed to deliver documents and data over the HTTP protocol. Its function is to accept incoming HTTP requests, decode the message to understand what the request is trying to accomplish, and provide an appropriate response. The language of the web server is the HTTP protocol.

We can set up a simple Sanic server, then make requests from curl, and see messages.

  1. Create a server.pyfile and write the following code:

img

from sanic import Sanic, text, Request

app = Sanic(__name__)


@app.post("/")
async def handler(request: Request):
	message = (
		request.head + b'\n\n' + request.body
	).decode("utf-8")
	
	print(message)
	return text("Done")
	
app.run(port=8088, debug=True)
复制代码
  1. execute sanic server.app, run the server
  2. Open another terminal, run the curl localhost:8088 -d '{"foo": "bar"}'statement , and see the following output:

img

Then go back to another terminal, you can see the HTTP request message, as follows:

POST / HTTP/1.1
Host: localhost:8088
User-Agent: curl/7.68.0
Accept: */*
Content-Length: 14
Content-Type: application/x-www-form-urlencoded

{"foo": "bar"}
复制代码
  • The first line contains the HTTP method, path and HTTP protocol used
  • Next is a list of HTTP headers, one per line, in the formatkey:value
  • At the end is the HTTP body, preceded by a blank line. The HTTP response is very similar:
HTTP/1.1 200 OK
content-length: 4
connection: keep-alive
content-type: text/plain; charset=utf-8
Done
复制代码
  • The first line contains the HTTP protocol, followed by the HTTP status and status description
  • Next is a list of HTTP headers, one per line, in the format key:value
  • At the end is the HTTP body (if any), preceded by a blank line.

img

While this is the language of the web server, writing all of this is cumbersome. Therefore, tools such as web browsers and HTTP client libraries were created to construct and parse these messages for us.

web framework

Of course, we could write a program in Python that receives these raw HTTP messages, decodes them, and returns an appropriate HTTP response message. However, this would require a lot of files, be difficult to scale, and be prone to errors.

有一些工具可以帮我们做到这一点:Web框架。Web 框架的工作是构建 HTTP 消息并适当地处理请求。许多框架通过提供便利和实用程序来进一步简化流程。

Python 生态系统中有许多 Web 框架在不同程度上完成了这项工作。有些提供了大量的功能,有些则非常稀少。有些非常严格,有些则更加开放。Sanic 只在不妨碍开发人员的情况下,才尝试保持功能丰富的连续性。

Sanic提供的一个功能是,它既是一个 Web 框架,也是一个 Web 服务器。Web 框架做的就是有一个服务器调用一个输入函数,向其传递有关请求的信息,并得到响应。

async/await 风格的协程处理程序的项目,绝大多数都需要运行一个ASGI服务器。它遵循一个类似的模式:一个ASGI就绪的服务器调用一个ASGI就绪的框架。

这两个组件使用一个特定的协议相互操作。目前有三种流行的ASGI服务器:uvicornhypercorndaphne

为什么选择学习 Sanic 框架

正因为 Sanic 诞生于 ASGI 之前的时代,它需要自己的服务器。随着时间的推移,这已经成为它最大的资产之一,并且在很大程度上是它胜过其他大多数 Python 框架的原因。Sanic 服务器的开发非常注重性能和请求/响应周期的最小化。然而,近年来 Sanic 也采用了 ASGI 接口,使其能够由 ASGI 网络服务器运行。

Sanic 具备开箱即用的功能,它可以用于编写,部署和扩展生产级 Web 应用程序。

为什么选择 Sanic 框架学习呢?官方给出了 6 个原因:

特征(Features)

  • 内置极速 web server
  • 生产准备就绪
  • 极高的拓展性
  • 支持 ASGI
  • 简单直观的 API 设计
  • 社区保障

总结

Sanic would be seen as an attempt to bring async/await style programming to Flask applications. While this may have been a fair view of the original proof-of-concept, Sanic has developed on a very different path, with the goal and impact of being a powerful tool designed for performance applications.

As such, Sanic is often used by developers and teams looking to build a rich environment that addresses the unique, distinct design patterns required by their application needs. The intent of this project is to remove the difficult or tedious parts of building web servers and provide tools to create high-performance and scalable web applications.

Guess you like

Origin juejin.im/post/7086786497860337677