#!/usr/bin/python from xml.dom import minidom from xml.sax import parseString, SAXParseException from xml.sax.handler import ContentHandler import cgi, os, re, sys, traceback, urllib if os.environ['REQUEST_METHOD']=="GET": print "Status: 403 Forbidden" print "Content-type: text/html\r\n\r\n" def text(element, tag): nodes = element.getElementsByTagName(tag) if not nodes: return "" return "".join([child.data for child in nodes[0].childNodes]) class rdf(ContentHandler): """ xml -> dictionary of {dc:identifier => trackback:ping|rdf:about} Parse a given html page, and retrieve the rdf:about information associated with a given href. """ ids = {} def startElement(self, name, attrs): if name == 'rdf:Description': attrs=dict(attrs) if attrs.has_key('dc:identifier'): if attrs.has_key('trackback:ping'): self.ids[attrs['dc:identifier']] = attrs['trackback:ping'] elif attrs.has_key('about'): self.ids[attrs['dc:identifier']] = attrs['about'] elif attrs.has_key('rdf:about'): self.ids[attrs['dc:identifier']] = attrs['rdf:about'] tb_re=re.compile('()') def backref(href): """ href -> trackback Parse a given html page, and retrieve the rdf:about for that url. """ base = href.split("#")[0] file = urllib.urlopen(base) data = file.read().replace('\n',' ') file.close() trackback = None for x in tb_re.findall(data): try: parseString(x, rdf()) except SAXParseException: pass if rdf.ids.has_key(href): trackback = rdf.ids[href] return trackback fail = None title = "Server Failure" url = cgi.FieldStorage().getvalue("url") tb = None try: tb = backref(url.replace("&","&")) if not tb: fail = "Trackback RDF not found" if not fail: tb = backref(cgi.FieldStorage().getvalue("url")) if tb.find('?')>=0: tb = tb + "&__mode=rss" else: tb = tb + "?__mode=rss" stream=urllib.urlopen(tb) rss=minidom.parse(stream) stream.close() channel = rss.getElementsByTagName('channel')[0] title = text(channel, "title") link = text(channel, "link") except: fail = ''.join(apply(traceback.format_exception, sys.exc_info())) head=open("blosxom/head.html").read() head=re.sub("\$blog_title", "Backtrack: " + title, head) print head try: if not fail: print '
' print '

%s

' % title print text(channel, "description").encode('latin-1') print '' % link print '
' print '

Backtracks

' for item in rss.getElementsByTagName('item'): link = text(item, "link") back = re.sub("#", "%23", link) print '
' print '

%s

' % text(item, "title").encode('latin-1') print text(item, "description").encode('latin-1') print '
' print '[more]' % link print '[back]' % back print '
' print '
' except: fail=''.join(apply(traceback.format_exception, sys.exc_info())) if fail: print '

Exception Traceback

' if url: print 'url= %s
' % (url, url) if tb: print 'tb= %s
' % (tb, tb) print '' print fail print '' foot=open("blosxom/foot.html").read() list=re.split('#include "(\S+)"', foot) print list.pop(0) while list: print open("blosxom/"+list.pop(0)).read() print list.pop(0)