WSGI (Web Server Gateway Interface) is a specification that describes the communication between web servers and Python web applications or frameworks. It explains how a web server communicates with Python web applications or frameworks and how web applications or frameworks can be chained for processing a request.
Web Server Gateway Interface (WSGI) is a mediator responsible for conveying communication between a web server and a Python web application. It explains how the web server communicates with the app and how the app can be chained for processing a request. It’s vital for deploying a Django or Flask app.
Python standard WSGI is detailed in PEP 3333. Learning more about PEP 3333 will help you become more familiar with WSGI.
How Does WSGI Work?
Now, let’s have a look at how WSGI works. Let’s assume a scenario in which you’ve developed a Django or Flask web application.
A web application is deployed in the web server and obtains requests from various users.
The web server can be Apache, NGINX or any other server that’s responsible for handling various static files and caching. You can also use the server as a load balancer, if you are willing to scale multiple applications.
How Does a Web Server Interact With the Python Application?
A problem occurs when a web server has to interact with a Python application.
This is where the mediator comes in. A mediator is required to carry out the interaction between the web servers and the Python application. WSGI is the standard mediator for carrying out communication between the web server and Python application.
Now, the web server is able to send requests or communicate with WSGI containers. Likewise, the Python application provides a ‘callable’ object containing certain functionalities that WSGI invokes and are defined according to the PEP 3333 standard. There are multiple WSGI containers available, such as Gunicorn and uWSGI.
When a user interacts with the web application, the server contacts the WSGI, which then communicates with the Python application and back.
There are multiple WSGI containers that are available today. A WSGI container must be installed in the project so that a web server can communicate to a WSGI container, which further communicates to the Python application and provides the response back accordingly. When the web server obtains the response, it’s sent back to the web browser/users.
Why Use a WSGI?
If you directly point your web server to your application, it reduces the flexibility of your application. Since your web server now directly points to your web application, you are unable to swap out web stack components.
For instance, let’s say you’ve decided to deploy your application using Gunicorn, but several years later, you decide to switch from Gunicorn to mod_wsgi. In this situation, you could easily switch to mod_wsgi without making any changes in the application or framework because you used WSGI. WSGI provides flexibility to your application.
Another reason for using WSGI is due to its scalability. Once your application is live, there can be thousands of requests in your application. WSGI is capable of serving thousands of requests at a time. The WSGI server is responsible for handling the requests from the web server and making decisions for carrying out the communication of those requests to an application framework’s process. Here, we can divide the responsibilities among the servers for scaling web traffic.
Popular WSGI Servers to Know
There are various servers that support WSGI. I’ll go over some of the most common WSGI servers that are used for the deployment of Flask/Django applications.
Gunicorn (Green Unicorn)
Gunicorn is a pre-fork worker model based server that’s compatible with various web frameworks. Additionally, it’s simple to implement.
uWSGI is known for being a highly-performant WSGI server implementation. uWSGI can be used for the development and deployment of a Python application. In uWSGI, application servers, proxies, process managers and monitors are all implemented through a common API and a common configuration style. This makes it a developer-friendly WSGI server.
mod_wsgi is a Python package that provides an Apache module that implements a WSGI compliant interface for hosting Python-based web applications on top of the Apache web server.
CherryPy is a Python web framework that allows developers to create web applications in the same way by which they develop other object-oriented Python programs. CherryPy is considered to be a reliant object-oriented HTTP framework that can efficiently run on multiple HTTP servers at once.
Understand the WSGI Fundamentals
You only need to dive deeper into WSGI if you’re developing a new web application framework. Otherwise, you only need to be familiar with its purpose and applicability. Today, most web application frameworks support WSGI, so you don’t need to worry about the configuration and deployment of your application.