Running a WSGI Application inside Twisted

There are a few ways to run a WSGI application, and today I will comment on the Twisted approach. The magic happens from the twisted.web2.wsgi module, and is fortunately simple.

The main advantages I can think of when running with Twisted/Twistd is that:

  1. All Python solution (if that really is an advantage)
  2. Ability to run other twisted services. Did you want RPC, Email, FTP, remote shell etc for free?
  3. Twisted authorization framework
Since the application in question has a very simple web part, and is predominately a remote GUI application, this seems the perfect solution.

First make a tac file, which you will be running with the old favourite:

twistd -ny mytac.tac

On to some code:

# This is my process for creating my WSGI application. You may have other methods.
from graelsite.www.application import make_app
from graelsite.www import config
wsgi_app = make_app(config)

# Now on to the real stuff
from twisted.application import service, strports
from twisted.web2 import server, channel, wsgi

application = service.Application('web2-wsgi') # call this anything you like
site = server.Site(wsgi.WSGIResource(wsgi_app))
s = strports.service('tcp:9876', channel.HTTPFactory(site))
s.setServiceParent(application)


And I honestly think that is the easiest way to deploy a WSGI application (outside the safety of Cherry, Paster, Werkzeug, etc.).

Note: Werkzeug is excellent, use it!

Popular posts from this blog

PyGTK, Py2exe, and Inno setup for single-file Windows installers

ESP-IDF for Arduino Users, Tutorials Part 2: Delay

How to install IronPython2 with Mono on Ubuntu