Yes, let’s create yet another web framework, because why not? By the end of this post, we’ll be able to use our new framework as such:
First of all, we need to understand how to talk to a web server from our Python web framework. In the early days of Python web development, there were many frameworks out there, but they had limited compatibility across different web servers. To solve that, they came up with
wsgi which stands for Web Server Gateway Interface¹. Now web servers have a convenient and standard way to talk to each other, also known as an interface.
The Web Server Gateway Interface (WSGI) is a simple calling convention for web servers to forward requests to web applications or frameworks written in the Python¹.
A WSGI-compatible web server expects to receive a callable that takes two arguments. Our callable is
application . The first argument is a
dict , that holds the information about the incoming request. The second is another
Callable responsible for setting the response code and response headers.
I picked the
gunicorn server to serve our app. The entry-point expects the callable as below:
$ gunicorn <module>:<callable>
From the command-line, we can start the app.
$ gunicorn wsgi:application
[INFO] Starting gunicorn 20.0.4
[INFO] Listening at: http://127.0.0.1:8000 (35567)
[INFO] Using worker: sync
[INFO] Booting worker with pid: 35570
Now our web app is running on port
8000 so we can access it via browser or
$ curl http://127.0.0.1:8000/
You might have noticed the
async keyword in the snippet at the beginning. We are creating an
async — compatible framework, and…