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 can fit their needs.
Python is one such programming language that is very successfully used in the current scenario. The main reasons point towards its dynamic behavior that contributes to a multi-purpose programming language, versatility in scripting, and eventually, its powerful nature. Most of the available frameworks in the language are non-interoperable.
Python works on WSGI servers or 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 WSGI Framework & 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 application, and that was negative interoperability. It was designed for only one FastCGI, mod_Python, CGI, or other API 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 to combat the execution of Python web application.
This was the area where WSGI figured 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 detailing 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, standardised low-level interface between web servers and web frameworks. This provides great relief to programmers who were not very familiar with the working of Python programming and had to take trouble choosing a framework because of the limitations they had with usable web servers.
There are two major sides to the WSGI framework:
a. WSGI Application/Framework made from Python script
b. 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:
1. WSGI environment as first argument
2. 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 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 WSGIs
It is evident that WSGI comes with a series of advantages of its own, especially since they come in various sizes and flavors. Go through the different benefits of WSGI, the server options they generate, and choose the one that would suit your requirements.
1. Flexibility:
One of the biggest advantage that WSGI gives us flexibility. You can actually change the web stack components without changing the codes at all, and without even changing the application that runs the WSGI servers.
2. Scalability:
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.
3. 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 TurboGears, you don�t have to be worried about how your framework of choice uses WSGI standard.
Many of the Python web frameworks come with WSGI adapter, and the 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 server to become a part of micro-service architecture so as to take advantage of the strength of each platform.
4. 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 wholeheartedly embrace it.
5. Reusable Middleware:
You can enhance the functionality of WSGI through existing middleware components like authentication/authorization, session stuff, caching, filtering, etc. The reusability feature saves time.
WSGI Frameworks in Python
A web framework comes with a set of libraries and a main handler for building custom codes that would build the web application, especially a dynamic and interactive one. The web frameworks in general consist of patterns & utilities that will help with URL Routing, Template Engine, Development Web Server and Request & 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, back-end language and framework, API layer, data access layer, databases, etc.
Django
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, template engine, authentication, database schema migrations, ORM or Object Relational Mapper all come with the Django package. This is the reason why Django is considered a full-stack Python.
Django is also open-source, MVC style and a 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 makes Django the most recommended one.
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 requirement.
Thousands of websites currently use Django, and this includes newspaper sites, major foundations, non-profit organisations, social media & sharing sites. Washington Post and The Guardian are just two of them.
Wheezy.web
Wheezy Web is a lightweight, high concurrency WSGI web framework that comes with incredible features helping you build modern, efficient web applications. It requires Python 2.4-2.7 or 3.2+. It takes most of the advantages of the standard python library and acts like with other libraries. Just like other frameworks, it uses the MVC pattern to separate the data model from the user interface. This helps with modularising code and promoting code reuse.
Wheezy Web frameworks use the push-based architecture where handlers (callable that accepts HTTPRequest and returns HTTPResponse) do the processing and �push� the data to the template layer as results. The framework comes with a host of functionalities including routing, model update/validation, authentication and content caching with dependency. 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, starting 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 Micro framework. 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
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 & 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
Flask is a micro web framework written in Python, based on 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 the most favored choice for getting simple apps up and running because of the little 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 micro framework 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 & 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 built-in development server and debugger, integrated unit testing support, Google App Engine compatibility and extensive documentation.
Comparison between the Python�s Framework Benchmarks
To understand the performance overhead, it is important to perform a comparison between the 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:
1. JSON test - Serialize an object to JSON and return `application/json` response
2. Remote test - Load http response from a remote server and return as response
3. Complete test - Insert a new object, sort and render the template after loading some data from Database with 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
Conclusion
WSGI is simple, and an official Python standard. It allows you to easily 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 the requirement.
Therefore, WSGI quite easily simplifies writing web applications at the low level, thanks to the application callable interface. And with the help of middleware callable, 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, session stuff, 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; it�s flexibility coupled with its functionality really makes it a popular choice.
Want to adopt WSGI framework for your next web application development? We can help you...