What is WSGI? A Readable Explanation for Python Developers
Understand the web server gateway interface completely
Introduction to the WSGI
When we enter a link on a browser, what will happen?
Simply put, there are only four steps:
- The browser sends an HTTP request.
- The web server receives the request and finds or generates an HTML document.
- The server sends the HTML document as the body of a HTTP response to the browser.
- The browser receives the HTTP response, renders and displays the HTML document in it.
Therefore, to develop a website in Python, we just need to implement these four steps. Sounds easy? 🙂
However, parsing HTTP requests, sending HTTP responses and handling all the relative issues are complex work. If we would like to write these programs by ourselves, we have to become an HTTP and network expert firstly. Not to mention there is much cyber security knowledge we have to know as well.
So, the best practice for web development is to use a server software, such as Apache or Nginx, to help us handle these common issues. And then we can focus on the business logic of our websites.
But as to websites written in Python, how can a server software communicate with Python programs?
If there are no common specifications or rules for this, Python web development will be messy. Problems like the following will emerge again and again:
- How can Django apps communicate with Apache?
- How can Flask apps communicate with Nginx?
- ……
- How can A (a Python web app/framework) communicate with B (a server software)?
Therefore, the Python community provides a standard interface by PEP 3333 to solve the problem.
This interface is called WSGI: Web Server Gateway Interface.
A software that implements this interface is called a WSGI server software.
By the way, just like there are a few different implementations of Python (Cpython, Jython, PyPy and so on). There also are different implementations of WSGI. Gunicorn and uWSGI are two of the most popular implementations.
Now, we got the whole picture. The backend architecture of a Python web app is like the following:

Try the Built-in WSGI Server of Python
Python has a built-in module called wsgiref.
As its name implies, it’s a reference implementation of the WSGI standard without considering any efficiency. But it’s very useful for development and testing processes.
Let’s write a simple program to see how to use it:





