Explaining WSGI and Python Applications

Juan Benito Pacheco Rubio
3 min readMay 23, 2020

--

WSGI is one of the concepts you read when you start developing your python web project. However is a widely used concept, it might be quite difficult to understand when you read the first couple of times. this article will answer the next questions

  • What is WSGI?
  • Why was WSGI created?
  • How does WSGI help us when we deploy our python web projects?

Definition

WSGI means Web Server Gateway Interface, it is the standard which defined the rules followed by the Application Servers, such as Gunicorn, which handle the income requests and lead them to our python application, and the Frameworks which we use to develop our applications, such as Django.

History

When python started being popular as programming language, many developers wanted to use it for the prominent web world until then dominated by PHP. In order to use python code on the server, the community developed an Apache module called mod_python.

However mod_python was popular, many vulnerabilities and bugs came up. Finally, the community decided to create a formal specification.

This specification was defined in the proposals PEP 0333 and PEP 3333. The goals of these proposals was to create a standard solution safe and secure, but flexible enough to be used with different python technologies.

The found solution was WSGI.

Theory

This standard defined how an Application Server should load a Python Application and work together. It allowed developers to create different Application Servers which are able to work with any python application if they meet the WSGI specification.

Python Web Project communication diagram

The picture above illustrate how our application WSGI server and our python application communicate with each other. They are completely decoupled, they don’t need to know each other, they only need to satisfy the WSGI standard.

The WSGI server is in charge to handle the information received by the peripherals of our server (it might be using a network socket, file, stdin…). The incoming information will be adapted to the WSGI standard, the server will load our python application and it will send the information to it. It will become the Gateway of our application.

The Python Application needs to understand how the WSGI server will load it, how he will receive the information, and how it will need to be returned. Our application will bootstrap itself in the appropriate way to be loaded by the server, and being able to communicate with it.

Code

The theory is fine, but as developers we like to see the theory be translated into code lines. The examples below are an Application Server (Funicorn) and Python Application (Fango), neither of these programs are production ready, are not tested and might contains bugs. They are examples to illustrate how the standard works in a general way.

In order to simplify the code, the most important parts of the standard are implemented

Application Server

Funicorn Server

https://gist.github.com/juanbenitopr/cbfef0f29668fc95852684c41b759b1c

Python Application

Fango Application

https://gist.github.com/juanbenitopr/14a66aeeb2012bd79abb9be4bd53fed7

Conclusion

The WSGI standard improve the python community in the web applications fields, allowing the developers to decouple the server technology from the python application in a isolated way.

The framework developers who didn’t need to worry about how to handle the socket connection, the income requests or concurrent connections, boosted the creation of different python frameworks loved by the community as Django, Flask, Pyramid… and so on.

If you would like to dive deeper about how WSGI works I would recommend you go directly to the proposals PEP 0333 and PEP 3333, they contains the whole specification.

--

--

Juan Benito Pacheco Rubio

Tech Lead at SmartProtection. Entusiast programmer who like reading, writting and coding!