Developers shoulder the responsibility of building reliable, scalable, and maintainable web applications, web resources, and dynamic websites through sophisticated and state-of-the-art web frameworks.
These web frameworks come with a set of coding components and a set of basic structuring tools that help simplify web development. These frameworks can solve problems and tasks through programming languages that fit their needs.
Python is one such programming language. The main reasons for its success are its dynamic behavior that contributes to a multi-purpose programming language, versatility in scripting, and its powerful nature. Most of the available frameworks in the language are non-interoperable.
Python works on WSGI servers or a Web Server Gateway Interface, which dictates the way in which the web server communicates with applications. The performance parameter of the Python stack is influenced greatly by the WSGI server.
What Is the WSGI Framework and Why Is It Necessary?
WSGI, authored by Philip J. Eby and published on December 7, 2003, is a Web Server Gateway Interface, a specification that explains how a web server communicates with web applications, and how the applications can be chained together to generate requests.
There was one major flaw in Python applications, and that was negative interoperability. It was designed for only one FastCGI, mod_Python, CGI, or other APIs of a specific web server, and they didn’t allow for interoperability. Unfortunately, mod_Python, an Apache module, was not an official specification, and there was little security. Developers needed a new solution.
This is where WSGI came in. It had a standard interface for routing web apps and frameworks to web servers. The framework traces its origins to CGI, or Common Gateway Interface, and was in use in the early days of the internet. The success of CGI was because it could work with many languages, but the downside was that it was slow and limited.
As per the details in PEP 333, the interface allows for greater interoperability between Python frameworks. WSGI provides the common ground for Python web application development because it provides a universal, standardized, low-level interface between web servers and web frameworks.
There are two major sides to the WSGI framework:
WSGI Application/Framework made from the fPython script.
WSGI Server/Gateway like Apache or Nginx.
Being a callable object, WSGI paces two arguments through a __call__ method, according to PEP 333. They are:
WSGI environment as the first argument.
A function that starts the response.
Each time it receives a request from the HTTP clients directed towards the application, an application callable is invoked by the server/gateway. Eventually, WSGI became the accepted approach for running Python applications.
It is the server that triggers the web app, and transmits related information, with a callback function to the app. The processing of the request takes place on the app side, while the server receives the response, by making use of the callback function. Python frameworks that support WSGI are Django, web2py, Flask, TurboGears, and CherryPy.
There could be more than one WSGI middleware between the web app and the server. Their function would be to direct requests to various app objects, content preprocessing, load balancing, and so on.
Advantages of Using WSGI
One of the biggest advantages that WSGI gives us is flexibility. You can actually change the web stack components without changing the code at all, and without even changing the application that runs the WSGI servers.
Frameworks cannot handle too many requests. But WSGI servers can, and they can handle thousands of requests concurrently and route them from the web server through the best possible means.
WSGI helps speed up Python web application development because you don’t need to know any complicated things, just a basic knowledge of how the interface works. Through CherryPy, Django, and TurboGears, you don’t have to be worried about how your framework of choice uses the standard WSGI.
Many of the Python web frameworks come with a WSGI adapter, and server technologies, like Apache, mod_python, FastCGI, CGI, etc., can run your WSGI application, making web application portability an easy reality. Additionally, you can combine multiple platforms like Node.js and WSGI servers to become a part of your microservice architecture so you can take advantage of the strength of each platform.
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 wholeheartedly embrace it.
5. Reusable Middleware:
You can enhance the functionality of WSGI through existing middleware components like authentication/authorization, caching, filtering, etc. The reusability feature saves time.
WSGI Frameworks in Python
A web framework comes with a set of libraries and the main handler for building custom code that would build the web application, especially a dynamic and interactive one. Web frameworks in general consist of patterns and utilities that will help with URL Routing, the Template Engine, the Development Web Server, and Request and Response Objects. Have a look at some of the main frameworks of WSGI.
There are mainly two types of WSGI Frameworks in Python:
1. Full Stack Frameworks
2. Non- Full Stack Frameworks
1. Full Stack Frameworks
In full-stack programming, you do everything in one area, but you must be able to handle everything to build a fully functional, complete web application. So you need to be aware of the whole stack, like front-end/UI language and framework, backend language and framework, API layer, data access layer, databases, etc.
Written in Python, the Django framework is just perfect for developers chasing tight deadlines. Django is also known as a battery included framework, where everything comes bundled within the framework, instead of as separate libraries. For example, URL routing, a template engine, authentication, database schema migrations, and an ORM, or Object Relational Mapper, all come with the Django package. This is the reason why Django is considered a full-stack Python framework.
Django is also an open-source, MVC style, and server-side web framework, with the motto, “Don’t repeat yourself.” Features like scalability and maturity with a huge developer community and an incredible set of built-in components make Django the most recommended framework.
And it comes with complex, database-backed web application with out-of-the-box utilities and patterns. There are pre-built modules that can be integrated as it is or customized as per your requirements.
Wheezy.web is a lightweight, high concurrency WSGI web framework that comes with incredible features that help you build modern, efficient web applications. It requires Python 2.4-2.7 or 3.2+. Just like other frameworks, it uses the MVC pattern to separate the data model from the user interface. This helps with modularizing code and promoting code reuse.
Wheezy.web frameworks use the push-based architecture where handlers (callables that accept HTTP Requests and returns HTTP Responses) do the processing and “push” the data to the template layer as a result. The framework comes with a host of functionalities including routing, model update/validation, authentication, and content caching with dependency. The Wheezy.web framework is optimized for performance, well-tested, and documented.
2. Non-Full Stack Frameworks
While a full-stack framework is one that helps with the full development stack, from user-interface to data store, a non-full stack framework is just the opposite. It would be somewhat like a use-at-will framework, and more like a class library, and if the library is less than 5K in size, then it can be tagged as a microframework. These are usually good at doing one thing, and the one thing will be done perfectly. Discussed below are two of the many Python frameworks that are popular this year.
To create a full-featured website from your framework, you may need to use a lot of code and add some extra bits yourself. This could take up a little time.
Bottle is a simple WSGI micro web framework in the Python programming language. It is fast and lightweight and distributed as a single file module with no dependencies except for the Python Standard Library.
It comes with the following attributes: routing; fast and Pythonic built-in template engine; utilities for convenient access to HTTP related metadata like form data, cookies, file uploads, and headers; built-in HTTP development server and support.
Bottle is a great resource for prototyping ideas, and for building and running simple personal web applications. Prototyping becomes easier with Bottle because it is less opinionated than Django and the latter contains a significant amount of boilerplate code.
Bottle has a single large source file named bottle.py aiding in great reading when learning how WSGI web frameworks work. All you need to know about how the web application’s code connects with the Bottle framework is within that single source code. Personal projects are easily deployed with Bottle because you can easily package your app’s source code with bottle.py. This would help you skip some of the steps and, in the meantime, get your application up and running easily.
The simple profile of Bottle makes it easier to embed Bottle within a larger application, and not be bothered about system-wide dependencies. Bottle is thus a reliable solution for building small applications.
Flask is a micro web framework written in Python, based on the Werkzeug toolkit and Jinja2 template engine, and is BSD licensed. The latest stable version of Flask is 0.12.2 and was released in May 2017. Pinterest, LinkedIn, and the community web page for Flask, all use the Flask framework.
Flask is one of the favored choices for getting simple apps up and running because of the small amount of boilerplate code. An advantage that Flask has over Django is that the framework was written several years after it, so it is the result of the observations garnered from the Python community’s reactions.
Flask is a microframework because it doesn’t require any libraries for tools, nor does it have a database abstraction layer, form validation, or components where the third-party libraries provide common functions.
An important thing to note is that Flask supports extensions that would help improve and enhance application functionality. These extensions, namely for object-relational mappers, form validation, upload handling, various open authentication technologies, and several common framework-related tools look and feel as if they were implemented in Flask itself.
Flask lets you create applications that provide support for secure cookies for client-side sessions. Flask also offers RESTful request dispatching, wherein the developers can bring forth an implementation that can define an object that can dispatch requests to any resources like HTML, Image, JSP, Servlet, etc. Other noted features of Flask include a built-in development server and debugger, integrated unit testing support, Google App Engine compatibility, and extensive documentation.
Comparison Between Python’s Framework Benchmarks
To understand the performance overhead, it is important to perform a comparison between Python’s frameworks and set benchmarks. The main purpose of the benchmark is not for testing deployment capability, but rather to test the framework as it is. The aim of doing these comparisons is to have a clear vision of the different frameworks and to provide help in matching the servers against the requirements of each application and customize them.
There are three types of tests to aid in the benchmarking of the frameworks. They are:
JSON test - Serialize an object to JSON and return an `application/json` response.
Remote test - Load an HTTP response from a remote server and return it as a response.
Complete test - Insert a new object, sort, and render the template after loading some data from the database with an ORM.
Results obtained after running the tests are:
Wheezy.web fared much better in the Complete test scenario and returned a response that was fast when compared to other frameworks in the JSON test.
Bottle is much faster here because it has recorded a faster response when compared to the other frameworks. This is especially true in the Remote test scenario.
Wheezy.web fared much better in the Complete test scenario as it returned the response faster than other frameworks.
Image Credits: GitHub
WSGI is simple, and an official Python standard. It allows you to mix and match multiple HTTP-related components easily. Since WSGI components adhere to the same interface, you can easily create a stack of components to improve or enhance the app’s functionality. These components can easily be added or removed as per your requirements.
Therefore, WSGI quite easily simplifies writing web applications at the low level, thanks to the application callable interface. And with the help of callable middleware, it makes modularizing functionality to abstract away common requirements and boilerplate code another simple task.
WSGI comes with lots of reusable middleware for authentication/authorization, caching, filtering, etc. And developers can make deployment easy on non-Apache web servers, either through native WSGI support or flup. Many of the Python frameworks are already implementing WSGI; its flexibility coupled with its functionality really makes it a popular choice.