3 Dec 2006 Nafai77   » (Journeyer)

How to host Pyblosxom as a WSGI application with Twisted

I recently switched to PyBlosxom for my blog software. I have been using Twisted to host my website since I had my own server and wished to continue to do so if possible.

Dependencies

  • Twisted Web 2: Twisted Web2 has built-in support for WSGI, which is used to connect to PyBlosxom. The 0.2.0 release should work fine, though I'm running off of Twisted svn trunk.

    Note: The stated requirement to download twisted_wsgi.py found in wsgi_app.py in the Pyblosxom distribution only applies to Twisted Web 1, not Twisted Web 2, as Twisted Web 2 has WSGI support built in.

  • Twisted: If you chose to download the release tarball of Twisted Web2, you'll also have to download the 2.4.0 release. If, however, you have checked it out from subversion, you'll have both Twisted Web2 and Twisted. Refer to the Twisted site for more details.

  • PyBlosxom: The latest release should be fine, but note that I'm also running off of PyBlosxom SVN Trunk.

General Setup

  1. Get all of the above modules set up. The easiest way is to refer to the INSTALL file in each of the downloaded or checked out distributions.
  2. Create a directory for all of your configuration and run-time files. For example, I use $HOME/bin.
  3. Copy web/wsgi_app.py from the PyBlosxom distribution in the directory.
  4. Copy the config.py PyBlosxom configuration file into the directory as well, making any customizations that are needed.

Twisted Configuration

I use a .tac file for my Twisted server configuration. It is simply a set of Python code required to set up the server and its parameters. Here is a minimum working configuration, with no Twisted logging enabled:

   from twisted.application import service, strports
 from twisted.web2 import server, channel, wsgi
 from twisted.web2.channel import http
 from wsgi_app import application as pyblosxom_application

 pyBlosxomResource = wsgi.WSGIResource(pyblosxom_application)

 site = server.Site(pyBlosxomResource)
 application = service.Application('web')
 s = strports.service('tcp:80', channel.HTTPFactory(site))
 s.setServiceParent(application)

I could probably make it a bit cleaner, but this is a pared down version of the more complex working version that hosts my sites. You can download and save blog.tac in the directory created above. Be sure to change the port number from tcp:80 to tcp:<custom port number> if you want the server to listen on a different port.

Startup Script

I also wrote a quick script to make it easy to start the blog server. You'll have to modify the pathnames appropriately to your particular setup. Here is the script:

   #!/bin/sh

 cd ~/bin/
 export PYTHONPATH=/home/nafai/code/Twisted:/home/nafai/code/pyblosxom-new/pyblosxom:$PYTHONPATH
 python2.4 /home/nafai/code/Twisted/bin/twistd -y /home/nafai/bin/blog.tac --pidfile=blog.pid --logfile=blog.log

Download and save startblog.sh in the directory created above. Make it executable by doing chmod +x startblog.sh.

Run it!

Now try running the blog service (for example, if the configuration files were saved in ~/bin:

   ~/bin/startblog.sh

You should be able to see your blog if you browse to http://localhost/ with your web browser. If you get a 503 or other error, check blog.log in the ~/bin directory for any tracebacks or errors. Also check the pyblosxom specific log if you have one configured in your config.py.

Caveat

You may run into a bug that I've seen, but haven't taken the time to track down where it is coming from. I am getting this traceback fairly often in my blog.log file, though everything else seems to run normal:

   2006/08/24 22:22 MDT [HTTPChannel,39,127.0.0.1] Traceback (most recent call last):
 2006/08/24 22:22 MDT [HTTPChannel,39,127.0.0.1]   File "logging/__init__.py", line 737, in emit
 2006/08/24 22:22 MDT [HTTPChannel,39,127.0.0.1] ValueError: I/O operation on closed file

I should track this down sometime soon.

Congratulations!

Happy TwistedPyBlogging!

Syndicated 2006-08-25 05:43:00 from Travis B. Hartwell / Software Craftsman

Latest blog entries     Older blog entries

New Advogato Features

New HTML Parser: The long-awaited libxml2 based HTML parser code is live. It needs further work but already handles most markup better than the original parser.

Keep up with the latest Advogato features by reading the Advogato status blog.

If you're a C programmer with some spare time, take a look at the mod_virgule project page and help us with one of the tasks on the ToDo list!