A micro web-framework using asyncio coroutines and chained middleware.
Growler is a web framework utilizing the new asynchronous library (asyncio) described in PEP 3156 and implemented in python 3.4. It takes a cue from nodejs’s express library, using a series of middleware to process HTTP requests. The custom chain of middleware provides an easy way to implement complex applications.
When available, growler will be installable via pip:
$ pip install growler
There are optionals to the install command that will ensure that additional functionality is working. For example if you want to use the (quite pythonic) jade html template engine, you can install with growler by adding it to the list of optionals:
$ pip install growler[jade]
When multiple optionals are available, they will be listed here.
The core of the framework is the growler.App class, which acts as both server and handler. The App object creates a request and a response object when a client connects and passes the pair to a series of middleware specified when setting up the server. Note: The middleware are processed in the same order they are specified. Headers are parsed and each middleware added to the app (using app.use()), then routes are matched and functions called. The middleware manipulate the request and response objects and either respond to the client or pass to the next middleware in the chain. This stream/filter model makes it very easy to modularize and extend web applications with any features, backed by the power of python.
import asyncio from growler import App from growler.middleware import (Logger, Static, Renderer) loop = asyncio.get_event_loop() # Construct our application with name GrowlerServer app = App('GrowlerServer', loop=loop) # Add some growler middleware to the application app.use(Logger()) app.use(Static(path='public')) app.use(Renderer("views/", "jade")) # Add some routes to the application @app.get('/') def index(req, res): res.render("home") @app.get('/hello') def hello_world(req, res): res.send_text("Hello World!!") # Create the server - this automatically adds it to the asyncio event loop Server = app.create_server(host='127.0.0.1', port=8000) # Tell the event loop to run forever - this will listen to the server's # socket and wake up the growler application upon each connection loop.run_forever()
This code creates an application which is identified by ‘GrowlerServer’ (this name does nothing at this point) and has some listening options, host and port. Requests are passed to some middleware provided by the Grower package: Logger and Renderer. Logger simply prints the ip address of the connecting client, and Renderer adds the render function to the response object (used in index(req, res)).
Decorators are used to add endpoints to the application, so requests matching ‘/’ will call index(req, res) and requests matching ‘/hello’ will call hello_world(req, res).
Calling app.run() starts the asyncio event loop and calls asyncio.run_forever. This does not HAVE to be called; you can create any task which calls the coroutine app._server_listen() and pass to the event loop if you prefer.
Growler introduces the virtual namespace growler_ext to which other projects may add their own growler-specific code. Of course, these packages may be imported in the standard way, but Growler provides an autoloading feature via the growler.ext module (note the ‘.’ in place of ‘_’) which will automatically import any packages found in the growler_ext namespace. This not only provides a standard interface for extensions, but allows for different implementations of an interface to be chosen by the environment, rather than hard-coded in. It also can reduce number of import statements at the beginning of the file. This specialized importer may be imported as a standalone module:
or a module from which to import from:
This works by replacing the ‘real’ ext module with an object that will import submodules in the growler_ext namespace automatically. Perhaps unfortunately, because of this there is no way I know of to allow the import growler.ext.my_extension syntax, as this skips the importer object and raises an import error. Users must use the from growler.ext import ... syntax instead.
The best practice for developers to add their middleware to growler is now to put their code in the python module growler_ext/my_extesion. This will allow your code to be imported by others via from growler.ext import my_extension or the combination of from growler import ext and ext.my_extesion.
An example of an extension is the indexer which hosts an automatically generated index of a filesystem directory. It should implement the best practices of how to write extensions.
Currently Growler is single threaded, and not tested very well. Any submissions or comments would be appreciated.
The name Growler comes from the beer bottle due to the apparent convention of giving python micro-web-frameworks fluid container names.
Growler is licensed under Apache 2.0.
Release history Release notifications | RSS feed
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
|Filename, size||File type||Python version||Upload date||Hashes|
|Filename, size growler-0.6.5-py3-none-any.whl (39.9 kB)||File type Wheel||Python version 3.4||Upload date||Hashes View|
|Filename, size growler-0.6.5.tar.gz (31.3 kB)||File type Source||Python version None||Upload date||Hashes View|