Unveiling the Black-Box Between an HTTP Request and Your Python Code: WSGI

ankita gulati
Aug 13, 2020 · 5 min read

We cannot deny the fact that we live in the world of internet. Every day we come across multiple websites. If we type http://medium.com in our browser, it will return us some webpage.

Today, if you want to communicate with some other person, you will use some language like English, Hindi or French which has some standard protocols. Similarly, if two computers want to communicate with each other over the internet, there should be some standard protocol, and that standard protocol is called “HyperText Transfer Protocol (HTTP)”.

So, the HTTP mentioned in the sample URL above in this blog is a communication protocol used to sent and receive webpages and other data files on the internet. When a user enters a web URL, an HTTP request that is sent by the browser and an HTTP response received from our application server have a definite format.

Today, most of the websites are dynamic in nature i.e. they are not based on files in the file system, but rather on programs which are run by the web server whenever a request comes in. These programs generate the content (through some calculation, logic, database etc) that is returned to the user. So, every application needs a web server that can interact with your code as well as with the outside world of HTTP. And, if your application is written in python, then all you need is a WSGI server for this purpose.

A Web Server Gateway Interface (WSGI) is a simple calling convention for web servers to forward request to web application or framework written in Python programming language.

A WSGI server implements the web server side of WSGI interface for running python web application. There are many WSGI servers in the market today like uWSGI, mod-WSGI, Green Unicorn etc.

Image for post
Image for post
Interaction between your Browser and Python Web Application

But why exactly do we need WSGI?

To understand the real need of WSGI, let’s dive deep into how WSGI came into existence.

Before WSGI, the traditional web server did not understand or had any way to run applications. In the late 1990s, a developer named Grisha Trubetskoy came up with an Apache module called mod_python to execute arbitrary Python code. However, mod_python was not an official specification. It was simply created so they could run Python code on a server. For more than a decade, people kept using mod_python to execute their python code.

But as the mod_python’s development stalled and security vulnerabilities were discovered, developers began to look for a new solution. The python community recognised the need of a consistent way to execute Python code for web applications and they came up with WSGI as standard interface that modules and containers could implement.

In nutshell, WSGI is basically a contract through which a web server can communicate with your python application.

The benefits of choosing WSGI as standard are :

Flexibility: WSGI helps you to decouple your application code from the web server code. That means, if a developer wants to switch from uWSGI to mod-WSGI server, this task can be done without modifying the application or framework that implements WSGI. It helps the application developer and the server developer to focus on their preferred area of specialisation.

Scaling: The python application is a single threaded application (due to GIL). WSGI helps you to scale the application, by controlling number of processes and threads that should be run for utilising the underlying resources and managing the incoming traffic.

Speed: WSGI helps speed up Python web application development because you don’t need to know any complicated things, just a basic knowledge about how the interface works. Through CherryPy, Django, and Flask, you don’t have to be worried about how your framework of choice uses the WSGI standard.

Simple: The learning curve of WSGI is simple, making it easier to pick up, and with no configuration or installation hassles. This is one of the biggest benefits of WSGI, and developers embrace it completely.

As stated earlier in the blog, there are multiple WSGI servers available in the market. In the below example, I will be creating a simple Hello World application using uWSGI server (You can choose any server of your choice).

Set up an App Directory and a Virtualenv

We will start by creating a folder for our app. For our purposes, this directory will simply hold our virtual environment and our WSGI entry point:

mkdir myapp

Next, move into the directory so that we can set up the environment for our application:

cd myapp

Create a virtual environment with the virtualenv command. We will call this myappenv for simplicity:

virtualenv myappenv

A new Python environment will be set up under a directory called myappenv. We can activate this environment by typing:

source myappenv/bin/activate

With this environment active, any Python packages installed will be contained within this directory hierarchy. They will not interfere with the system’s Python environment. With this in mind, we can now install the uWSGI server into our environment using pip.

pip install uwsgi

Create a WSGI Application

We will write our application in a file called app.py in myapp directory.

vi app.py

Please paste the following code inside this file.

def application(environ, start_response):
start_response('200 OK', [('Content-Type', 'text/html')])
return ["<h1 style='color:red'>Hello World!</h1>"]

The above code constitutes a complete WSGI application. By default, uWSGI will look for a callable called application, which is why we called our function application. As you can see, it takes two parameters.

The first we called environ because it will be an environmental variable-like key-value dictionary. The second is called start_response and is the name the app will use internally to refer to the web server (uWSGI) callable that is sent in.

Now it’s time to test out the code, we can start up the uWSGI. In the parameters, we will tell it to use HTTP protocol and to listen on port 8000

uwsgi --socket 0.0.0.0:8000 --protocol=http -w app

Now, open your browser and type http://127.0.0.1:8000/. You should see the following text in your browser window.

Image for post
Image for post

Voila!! We have created a simple WSGI application without thinking much details about the server part.

To summarise, WSGI server takes care of everything which happens in-between the web server and the web application, thus making development for us, a lot more easier. This was the basic overview of what WSGI is, why do we need them and how can we create a web application with it.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store