home *** CD-ROM | disk | FTP | other *** search
/ linuxmafia.com 2016 / linuxmafia.com.tar / linuxmafia.com / pub / palmos / pippy-0.6beta-src.tar.gz / pippy-0.6beta-src.tar / pippy-0.6beta-src / src / Lib / SimpleHTTPServer.py < prev    next >
Text File  |  2000-12-21  |  5KB  |  154 lines

  1. """Simple HTTP Server.
  2.  
  3. This module builds on BaseHTTPServer by implementing the standard GET
  4. and HEAD requests in a fairly straightforward manner.
  5.  
  6. """
  7.  
  8.  
  9. __version__ = "0.3"
  10.  
  11.  
  12. import os
  13. import string
  14. import posixpath
  15. import BaseHTTPServer
  16. import urllib
  17.  
  18.  
  19. class SimpleHTTPRequestHandler(BaseHTTPServer.BaseHTTPRequestHandler):
  20.  
  21.     """Simple HTTP request handler with GET and HEAD commands.
  22.  
  23.     This serves files from the current directory and any of its
  24.     subdirectories.  It assumes that all files are plain text files
  25.     unless they have the extension ".html" in which case it assumes
  26.     they are HTML files.
  27.  
  28.     The GET and HEAD requests are identical except that the HEAD
  29.     request omits the actual contents of the file.
  30.  
  31.     """
  32.  
  33.     server_version = "SimpleHTTP/" + __version__
  34.  
  35.     def do_GET(self):
  36.         """Serve a GET request."""
  37.         f = self.send_head()
  38.         if f:
  39.             self.copyfile(f, self.wfile)
  40.             f.close()
  41.  
  42.     def do_HEAD(self):
  43.         """Serve a HEAD request."""
  44.         f = self.send_head()
  45.         if f:
  46.             f.close()
  47.  
  48.     def send_head(self):
  49.         """Common code for GET and HEAD commands.
  50.  
  51.         This sends the response code and MIME headers.
  52.  
  53.         Return value is either a file object (which has to be copied
  54.         to the outputfile by the caller unless the command was HEAD,
  55.         and must be closed by the caller under all circumstances), or
  56.         None, in which case the caller has nothing further to do.
  57.  
  58.         """
  59.         path = self.translate_path(self.path)
  60.         if os.path.isdir(path):
  61.             self.send_error(403, "Directory listing not supported")
  62.             return None
  63.         try:
  64.             f = open(path, 'rb')
  65.         except IOError:
  66.             self.send_error(404, "File not found")
  67.             return None
  68.         self.send_response(200)
  69.         self.send_header("Content-type", self.guess_type(path))
  70.         self.end_headers()
  71.         return f
  72.  
  73.     def translate_path(self, path):
  74.         """Translate a /-separated PATH to the local filename syntax.
  75.  
  76.         Components that mean special things to the local file system
  77.         (e.g. drive or directory names) are ignored.  (XXX They should
  78.         probably be diagnosed.)
  79.  
  80.         """
  81.         path = posixpath.normpath(urllib.unquote(path))
  82.         words = string.splitfields(path, '/')
  83.         words = filter(None, words)
  84.         path = os.getcwd()
  85.         for word in words:
  86.             drive, word = os.path.splitdrive(word)
  87.             head, word = os.path.split(word)
  88.             if word in (os.curdir, os.pardir): continue
  89.             path = os.path.join(path, word)
  90.         return path
  91.  
  92.     def copyfile(self, source, outputfile):
  93.         """Copy all data between two file objects.
  94.  
  95.         The SOURCE argument is a file object open for reading
  96.         (or anything with a read() method) and the DESTINATION
  97.         argument is a file object open for writing (or
  98.         anything with a write() method).
  99.  
  100.         The only reason for overriding this would be to change
  101.         the block size or perhaps to replace newlines by CRLF
  102.         -- note however that this the default server uses this
  103.         to copy binary data as well.
  104.  
  105.         """
  106.  
  107.         BLOCKSIZE = 8192
  108.         while 1:
  109.             data = source.read(BLOCKSIZE)
  110.             if not data: break
  111.             outputfile.write(data)
  112.  
  113.     def guess_type(self, path):
  114.         """Guess the type of a file.
  115.  
  116.         Argument is a PATH (a filename).
  117.  
  118.         Return value is a string of the form type/subtype,
  119.         usable for a MIME Content-type header.
  120.  
  121.         The default implementation looks the file's extension
  122.         up in the table self.extensions_map, using text/plain
  123.         as a default; however it would be permissible (if
  124.         slow) to look inside the data to make a better guess.
  125.  
  126.         """
  127.  
  128.         base, ext = posixpath.splitext(path)
  129.         if self.extensions_map.has_key(ext):
  130.             return self.extensions_map[ext]
  131.         ext = string.lower(ext)
  132.         if self.extensions_map.has_key(ext):
  133.             return self.extensions_map[ext]
  134.         else:
  135.             return self.extensions_map['']
  136.  
  137.     extensions_map = {
  138.             '': 'text/plain',   # Default, *must* be present
  139.             '.html': 'text/html',
  140.             '.htm': 'text/html',
  141.             '.gif': 'image/gif',
  142.             '.jpg': 'image/jpeg',
  143.             '.jpeg': 'image/jpeg',
  144.             }
  145.  
  146.  
  147. def test(HandlerClass = SimpleHTTPRequestHandler,
  148.          ServerClass = BaseHTTPServer.HTTPServer):
  149.     BaseHTTPServer.test(HandlerClass, ServerClass)
  150.  
  151.  
  152. if __name__ == '__main__':
  153.     test()
  154.