All pastes #165817 Raw Edit

Stuff

public python v1 · immutable
·published 2006-09-10 05:45 UTC
rendered paste body
import osimport posixpathimport BaseHTTPServerimport urllibimport cgiimport shutilimport mimetypesfrom StringIO import StringIOfrom SimpleHTTPServer import *class SimpleRDFRequestHandler(SimpleHTTPRequestHandler):    """Override things to generate rdf, skip index.html, etc"""    def send_head(self):        """Common code for GET and HEAD commands.        This sends the response code and MIME headers.        Return value is either a file object (which has to be copied        to the outputfile by the caller unless the command was HEAD,        and must be closed by the caller under all circumstances), or        None, in which case the caller has nothing further to do.        """        path = self.translate_path(self.path)        f = None        if os.path.isdir(path):                return self.list_directory(path)        ctype = self.guess_type(path)        try:            # Always read in binary mode. Opening files in text mode may cause            # newline translations, making the actual size of the content            # transmitted *less* than the content-length!            f = open(path, 'rb')        except IOError:            self.send_error(404, "File not found")            return None        self.send_response(200)        self.send_header("Content-type", ctype)        self.send_header("Content-Length", str(os.fstat(f.fileno())[6]))        self.end_headers()        return f    def list_directory(self, path):        """Helper to produce a directory listing (absent index.html).        Return value is either a file object, or None (indicating an        error).  In either case, the headers are sent, making the        interface the same as for send_head().        """        try:            list = os.listdir(path)        except os.error:            self.send_error(404, "No permission to list directory")            return None        list.sort(key=lambda a: a.lower())        f = StringIO()#------------------------------------------------------------------------#        f.write("<title>Directory listing for %s</title>\n" % self.path)#        f.write("<h2>Directory listing for %s</h2>\n" % self.path)#        f.write("<hr>\n<ul>\n")#        for name in list:#            fullname = os.path.join(path, name)#            displayname = linkname = name#            # Append / for directories or @ for symbolic links#            if os.path.isdir(fullname):#                displayname = name + "/"#                linkname = name + "/"#            if os.path.islink(fullname):#                displayname = name + "@"#                # Note: a link to a directory displays with @ and links with /#            f.write('<li><a href="%s">%s</a>\n'#                    % (urllib.quote(linkname), cgi.escape(displayname)))#        f.write("</ul>\n<hr>\n")#------------------------------------------------------------------------	f.write('<?xml version="1.0" encoding="utf-8"?>')	#f.write('<?xml-stylesheet type="text/css" href="/rdf.css"?>\n')	f.write('<?xml-stylesheet type="text/xsl" href="/rdf.xsl"?>\n')	f.write('<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns="http://purl.org/rss/1.0/">')        f.write('<channel rdf:about="%s">\n' % self.path)        f.write('<title>Directory listing for %s</title>\n' % self.path)        f.write('<link>%s</link>\n' % self.path)        f.write('<description>Directory listing for %s</description>\n' % self.path)        f.write('<items>\n<rdf:Seq>\n')        for name in list:            fullname = os.path.join(path, name)            displayname = linkname = name            # Append / for directories or @ for symbolic links            if os.path.isdir(fullname):                displayname = name + "/"                linkname = name + "/"            if os.path.islink(fullname):                displayname = name + "@"                # Note: a link to a directory displays with @ and links with /            f.write('<rdf:li resource="%s" />\n'                    % urllib.quote(linkname))        f.write('</rdf:Seq>\n</items>\n</channel>\n')        for name in list:            fullname = os.path.join(path, name)            displayname = linkname = name            # Append / for directories or @ for symbolic links            if os.path.isdir(fullname):                displayname = name + "/"                linkname = name + "/"            if os.path.islink(fullname):                displayname = name + "@"                # Note: a link to a directory displays with @ and links with /            f.write('<item rdf:about="%s">\n' % urllib.quote(linkname))            f.write('<title>%s</title>\n' % cgi.escape(displayname))            f.write('<link>%s</link>\n' % urllib.quote(linkname))            f.write('<description><a href="%s">%s</a></description>\n' % (urllib.quote(linkname), cgi.escape(displayname)))            f.write('</item>')        f.write("</rdf:RDF>\n")#------------------------------------------------------------------------        length = f.tell()        f.seek(0)        self.send_response(200)        self.send_header("Content-type", "text/xml")        self.send_header("Content-Length", str(length))        self.end_headers()        return fdef test(HandlerClass = SimpleRDFRequestHandler,         ServerClass = BaseHTTPServer.HTTPServer):    BaseHTTPServer.test(HandlerClass, ServerClass)if __name__ == '__main__':    test()