Python: Flask alternatives


Useful frameworks for deploying APIs without Flask or Django.


If you’ve ever deployed an endpoint or created a website with Python, then it’s likely that you’ve heard of Flask and Django. Flask and Django are the industry standard in Python when it comes to doing nearly anything with the HTTP protocol in Python. Flask is typically used for more light-weight applications that don’t need a lot of features, whereas Django fills the void on the web-app spectrum. Both are great tools, but in some niche circumstances, it can be better to instead use a similar web-framework while not retaining the same features that Flask and Django come with.

Bottle

Bottle is an incredibly light micro-framework for WSGI in Python. It is published under the MIT license and is entirely free for modification and distribution. A big advantage to using Bottle is that it is stored inside of a single, small Python module with no dependencies. This makes things incredibly simple and seamless when you are ready to deploy your API.

One thing I really love about Bottle is how easy to use and light it is, with no added fluff like you might get with Flask and even more so Django. Sometimes minimalism is key, and Django and Flask can sometimes be a little overwhelming with all of the dynamic pieces of a small API coming into play. Another significant advantage of adding to the lightweight aspect is Bottle’s packed-in server. Bottle comes with its own web-server, meaning there is no need to configure Gunicorn3, but that isn’t to say you can’t use Gunicorn3. Bottle has support for Paste, fapws3, gae, or any other WSGI-compatible server.

In terms of syntax, Bottle blurs the lines of similarity between itself and Flask with very few syntactical differences. Hello world, for example:

from bottle import route, run, template

@route('/hello/<name>')
def index(name):
return template('<b>Hello {{name}}</b>!', name=name)

run(host='localhost', port=8080)

TurboGears


Another cool web framework I like to use is GNU TurboGears. To confess, I am fond of TurboGears partially because it is published under the GNU GPL license version 2, but that isn’t to say that TurboGears isn’t a great framework. Oddly enough, TurboGears is actually built on top of several other frameworks including Django and Rails. That being said, TurboGears is a marginal step away from something like Bottle and certainly has its place being used more like Django.

TurboGears was designed to be a full-stack framework, and should certainly be used that way. A great thing about TurboGears is its extensibility, the framework can be extended using all sorts of simple WSGI plugins. Additionally, it comes with support for horizontal data partitioning, or sharding. Syntactically, TurboGears is likely an entirely different beast than what any Python programmer is used to. The code tends to focus more on the object oriented paradigm, which is a completely different approach to most other frameworks. For example, here’s hello world:

from tg import expose, TGController

class RootController(TGController):
@expose()
def index(self):
return 'Hello World'
from tg import MinimalApplicationConfigurator

config = MinimalApplicationConfigurator()
config.update_blueprint({
'root_controller': RootController()
})
from wsgiref.simple_server import make_server

print("Serving on port 8080...")
httpd = make_server('', 8080, application)
httpd.serve_forever()

web2py


Web2py is the one framework on this list I imagine we are all familiar with. Web2py is another module that is primarily focused on the full-stack side of things. Similar to bottle, however, Web2py has no dependencies outside of the Python standard library. What’s impressive to me about Web2py is that it is able to accomplish similar goals to frameworks like Django and TurboGears while simultaneously being much more lightweight and easy to use. Web2py is published under the GNU GPL version 3 license, which means you can use it for just about anything without having to worry about licensing.

Web2py’s selling point is most likely its fully-featured IDE, which allows you to modify your website from any web-browser after it is deployed. With that in mind, it only makes sense that the primary advantage to using this software is how quickly you can create with it. Compared to Django and similar frameworks, Web2py is certainly a lot easier and quicker, but in my experience can be a bit lacking and old-fashioned when compared with more modern options.


CherryPy is another framework that I really enjoy and is likely my favorite on this list.

(It’s between CherryPy and Bottle)

CherryPy approaches web development in a way that is more similar to TurboGears, but approaches methodology in a way that is more similar to Bottle. CherryPy is incredibly light and is considered a minimalist web framework. Despite its light weight, however, CherryPy still manages to cram in a lot of incredibly useful features including authentication, sessions, and caching to name a few. CherryPy also features an interesting and unique plugin system with extremely manipulable… Well…

Everything!

CherryPy is licensed under the FreeBSD license, BSD licenses are a low restriction type of license for open source software that does not put requirements on redistribution, meaning similarly to most of the other frameworks it is free and open to use. In its similarity to TurboGears, CherryPy takes a very object-oriented approach to web-development. Here is hello world:

import cherrypy

class HelloWorld(object):
@cherrypy.expose
def index(self):
return "Hello World!"

cherrypy.quickstart(HelloWorld())

Quixote

Quixote is another framework that is well intertwined with the object-oriented paradigm. The framework focuses primarily on speed and flexibility, but can certainly be a difficult one to get into. Separation of presentation logic and “back-end” logic is not enforced by Quixote. This can be seen as both an advantage and a disadvantage. I would say that this framework is highly manipulable but definitely a lot harder to use compared to the other options available to Python programmers.

Another issue you’re going to run into with this framework is the severe, crippling,

lack of documentation.

Given these shortcomings, I would say that this framework is only recommended for an application that needs to be built from scratch. Though it might not be great for a lot of applications because of how long it will take to build, it certainly has its place and can get the job done in a way that other frameworks can’t.

Conclusion

Though Flask and Django are certainly great tools, I think it’s great to take a look at the competition because occasionally having the flexibility can certainly come in handy. The things I look for in a framework might not necessarily be the same things that other people look for, and could also depend entirely on what I want to create as well. My favorite on this list is without a doubt is CherryPy, because I often look for a very lightweight option because of how I typically use Pythonic web frameworks. Most of my web deployments are simple APIs that don’t need a lot of maintenance or pieces, but at the same time need to be secure and stable enough to deliver data as needed, and CherryPy is perfect for that. I’d be curious to know what other engineers look for in their frameworks, and which ones they would choose. Web frameworks might not be the most complex beasts on Earth, but having a great one can certainly make your day a lot simpler!

Comments

Popular posts from this blog

Frida: Hook methods

Ghindra: Reverse engineering tool

Mobexler: Os built for Pentesting