Web Servers and Frameworks
Sanic calls itself both a web framework and a web server. What does it mean? More importantly, why does this matter?
So what is a Web Server?
Web Server
A web server is software designed to deliver documents and data via 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 a request from curl and see the messages.
Create a server.py file and 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)
Execute sanic server.app to run the server
Open another terminal, run curl localhost:8088 -d '{"foo": "bar"}' statement, you can see the following output:
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 format key:value
Finally comes 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
This is followed by a list of HTTP headers, one per line, in the format key:value
Finally the HTTP body (if any), preceded by a blank line.
While this is the language of the web server, writing all of this is cumbersome. So tools like web browsers and HTTP client libraries were created to build and parse these messages for us.
web framework
Of course, we 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 scale, and be error prone.
There are some tools that can help us do this: web frameworks. It's the web framework's job to construct 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 ton of features, others are very sparse. Some are very strict, others are more open. Sanic only tries to maintain feature-rich continuity if it doesn't get in the way of developers.
One of the features provided by Sanic 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 that implement async/await style coroutine handlers require an ASGI server to run. It follows a similar pattern: an ASGI-ready server calls an ASGI-ready framework.
These two components interoperate using a specific protocol. There are currently three popular ASGI servers: uvicorn, hypercorn, and daphne.
Sanic
Precisely because Sanic was born in a pre-ASGI era, it needed its own servers. Over time, this has become one of its greatest assets, and is largely the reason it outperforms most other Python frameworks. Sanic servers are developed with great focus on performance and minimization of request/response cycles. However, in recent years Sanic has also adopted the ASGI interface, enabling it to be run by ASGI web servers.
Sanic comes with out-of-the-box functionality for writing, deploying, and scaling production-grade web applications.
Why choose the Sanic framework to learn? The official gave 6 reasons:
Features
- Built-in super fast web server
- production ready
- Very high expandability
- Support for ASGI
- Simple and intuitive API design
- community security
Summarize
Sanic will be seen as an attempt to bring async/await style programming to Flask applications. While this may be a fair point for 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.
As such, Sanic is often used by developers and teams looking to build a rich environment that addresses the unique, distinct design patterns their application needs require. The intent of this project is to remove the difficult or tedious part of building web servers and provide tools to create performant and scalable web applications.
You must be logged in to post a comment.