January 2004 archive

Python Trackback Library

In addition to the pingback library that I built earlier today, I decided to add Trackback support to my weblog software as well. To that end, I’ve created a new trackback.py library which handles the grunt work of being a Trackback client. It’s not exactly very difficult, anyways. The method of greatest concern for a client:


def ping(trackbackURI, sourceURI, title = None, excerpt = None, siteName = None):
    """Implements a trackback ping.  This method throws exceptions based upon
    the error returned from the trackback server, unless it is the successful
    error code '0'.

    The excerpt parameter should contain plaintext for maximum readability on
    trackback servers.  Providing HTML is not recommended.  This library
    provides a function ``detagHTML`` which will shred HTML text for you."""
    ...

This library does not offer support for being a Trackback server, nor autodiscovery in any way. I do not believe that the autodiscovery method suggested by the Trackback specification is appropriate. Embedding RDF data in an HTML comment? This is the kind of thing a perl author would suggest, since they believe they could could regex their way out of the hole they dug for themselves. I don’t like it.

An alternative implementation of trackback in Python is available called tblib. I chose to write my own library because:

  1. I was bored at work,
  2. I didn’t want to use a GPL-licensed library in my modified-BSD licensed weblog,
  3. I didn’t like the way tblib used regular expressions to grab data from ping responses rather than using an XML parser,
  4. I didn’t want to implement autodiscovery in a flappy regex perl way. (No disrespect meant towards the author of tblib, of course. I realize that there is no simpler or stronger way to do Trackback autodiscovery.)

Python Pingback Library

I’ve created a simple and easy-to-use Python library to handle the client implementation of the Pingback 1.0 protocol. The pingback.py script handles all aspects of a client, including automatically parsing HTML and reStructredText for hyperlinks which should be checked.

Pingback can now be implemented by other Python projects by using one of the following functions:


def autoPingback(sourceURI, reST = None, HTML = None):
    """Scans the input text, which can be in either reStructuredText or HTML
    format, pings every linked website for auto-discovery-capable pingback
    servers, and does an appropriate pingback."""
    ...

def pingback(sourceURI, targetURI):
    """Attempts to notify the server of targetURI that sourceURI refers to
    it."""
    ...

The documentation at the top of the module describes how one might implement a Pingback server by using the SimpleXMLRPCServer module, as well.