import os
import posixpath
import BaseHTTPServer
import urllib
import cgi
import shutil
import mimetypes
from StringIO import StringIO
from 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("
Directory listing for %s\n" % self.path)
# f.write("
Directory listing for %s
\n" % self.path)
# f.write("\n
\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('
\n\n")
#------------------------------------------------------------------------
f.write('')
#f.write('\n')
f.write('\n')
f.write('')
f.write('\n' % self.path)
f.write('Directory listing for %s\n' % self.path)
f.write('%s\n' % self.path)
f.write('Directory listing for %s\n' % self.path)
f.write('\n\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('\n'
% urllib.quote(linkname))
f.write('\n\n\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('\n' % urllib.quote(linkname))
f.write('%s\n' % cgi.escape(displayname))
f.write('%s\n' % urllib.quote(linkname))
f.write('%s\n' % (urllib.quote(linkname), cgi.escape(displayname)))
f.write('')
f.write("\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 f
def test(HandlerClass = SimpleRDFRequestHandler,
ServerClass = BaseHTTPServer.HTTPServer):
BaseHTTPServer.test(HandlerClass, ServerClass)
if __name__ == '__main__':
test()