Make writing a habit together! This is the 13th day of my participation in the “Gold Digging Day New Plan · April More Text Challenge”. Click here for more details.

Web Server and frameworks

Sanic describes itself as both a web framework and a web server. What does that mean? And more importantly, why does it 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 an incoming HTTP request, decode the message to understand what the request is trying to accomplish, and provide an appropriate response. The language of the Web server is HTTP protocol.

We can set up a simple Sanic server and make requests from Curl and view messages.

  1. To create aserver.pyFile, write the following code:

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)
Copy the code
  1. performsanic server.appTo run the server
  2. Open another terminal and runcurl localhost:8088 -d '{"foo": "bar"}'Statement, you can see the following output:

Then go back to the other terminal and see the HTTP request message, which reads 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"}Copy the code
  • The first line contains the HTTP method, path, and HTTP protocol used
  • What follows is a list of HTTP headers, one per line, in the formatkey:value
  • Finally, the HTTP body is 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; HTTP/1.1 200 OK Content-Length: 4 connection: keep-alive Content-type: text/plain; charset=utf-8 DoneCopy the code
  • The first line contains the HTTP protocol, followed by the HTTP state and state description
  • What follows is a list of HTTP headers, one per line, in the form key:value
  • Finally, the HTTP body (if any) is preceded by a blank line.

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

Web framework

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

There are tools that can help us do this: a Web framework. The job of a Web framework is to build HTTP messages and handle requests appropriately. Many frameworks further simplify the process by providing convenience and utilities.

There are many Web frameworks in the Python ecosystem that do this to varying degrees. Some offer a lot of functionality, some are very sparse. Some are very strict, some are more open. Sanic tries to maintain feature-rich continuity only as long as it doesn’t hinder developers.

One of the capabilities THAT Sanic provides is that it is both a Web framework and a Web server. What the Web framework does is have a server call an input function, pass it information about the request, and get a response.

Most projects with async/await style coroutine handlers require running an ASGI server. It follows a similar pattern: an ASGi-ready server calls an ASGi-ready framework.

The two components interact with each other using a specific protocol. There are three popular ASGI servers: Uvicorn, HyperCorn, and Daphne.

Why did you choose to study the Sanic framework

Because Sanic was born before ASGI, it needed its own servers. Over time, this has become one of its greatest assets, and is largely what makes it better than most other Python frameworks. Sanic server development is very focused on minimizing performance and request/response cycles. In recent years, however, Sanic has also adopted ASGI interfaces, enabling it to be run by ASGI network servers.

Sanic comes out of the box and can be used to write, deploy, and extend production-grade Web applications.

Why choose Sanic framework for learning? Officials give six reasons:

Characteristics (Features)

  • Built-in ultra-fast Web Server
  • Ready for production
  • Extremely scalable
  • Support ASGI
  • Simple and intuitive API design
  • Community security

conclusion

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

Therefore, Sanic is often used by developers and teams who want to build a rich environment that addresses the unique, obvious design patterns required by their application requirements. The intent of the project is to eliminate the difficult or cumbersome parts of building web servers and provide tools to create high-performance and scalable web applications.