home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2010 November / maximum-cd-2010-11.iso / DiscContents / calibre-0.7.13.msi / file_700 (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2010-08-06  |  4.9 KB  |  159 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. __version__ = '0.6'
  5. __all__ = [
  6.     'SimpleHTTPRequestHandler']
  7. import os
  8. import posixpath
  9. import BaseHTTPServer
  10. import urllib
  11. import cgi
  12. import shutil
  13. import mimetypes
  14.  
  15. try:
  16.     from cStringIO import StringIO
  17. except ImportError:
  18.     from StringIO import StringIO
  19.  
  20.  
  21. class SimpleHTTPRequestHandler(BaseHTTPServer.BaseHTTPRequestHandler):
  22.     server_version = 'SimpleHTTP/' + __version__
  23.     
  24.     def do_GET(self):
  25.         f = self.send_head()
  26.         if f:
  27.             self.copyfile(f, self.wfile)
  28.             f.close()
  29.         
  30.  
  31.     
  32.     def do_HEAD(self):
  33.         f = self.send_head()
  34.         if f:
  35.             f.close()
  36.         
  37.  
  38.     
  39.     def send_head(self):
  40.         path = self.translate_path(self.path)
  41.         f = None
  42.         if os.path.isdir(path):
  43.             if not self.path.endswith('/'):
  44.                 self.send_response(301)
  45.                 self.send_header('Location', self.path + '/')
  46.                 self.end_headers()
  47.                 return None
  48.             for index in ('index.html', 'index.htm'):
  49.                 index = os.path.join(path, index)
  50.                 if os.path.exists(index):
  51.                     path = index
  52.                     break
  53.                     continue
  54.                 self.path.endswith('/')
  55.             else:
  56.                 return self.list_directory(path)
  57.         ctype = self.guess_type(path)
  58.         
  59.         try:
  60.             f = open(path, 'rb')
  61.         except IOError:
  62.             self.send_error(404, 'File not found')
  63.             return None
  64.  
  65.         self.send_response(200)
  66.         self.send_header('Content-type', ctype)
  67.         fs = os.fstat(f.fileno())
  68.         self.send_header('Content-Length', str(fs[6]))
  69.         self.send_header('Last-Modified', self.date_time_string(fs.st_mtime))
  70.         self.end_headers()
  71.         return f
  72.  
  73.     
  74.     def list_directory(self, path):
  75.         
  76.         try:
  77.             list = os.listdir(path)
  78.         except os.error:
  79.             self.send_error(404, 'No permission to list directory')
  80.             return None
  81.  
  82.         list.sort(key = (lambda a: a.lower()))
  83.         f = StringIO()
  84.         displaypath = cgi.escape(urllib.unquote(self.path))
  85.         f.write('<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">')
  86.         f.write('<html>\n<title>Directory listing for %s</title>\n' % displaypath)
  87.         f.write('<body>\n<h2>Directory listing for %s</h2>\n' % displaypath)
  88.         f.write('<hr>\n<ul>\n')
  89.         for name in list:
  90.             fullname = os.path.join(path, name)
  91.             displayname = linkname = name
  92.             if os.path.isdir(fullname):
  93.                 displayname = name + '/'
  94.                 linkname = name + '/'
  95.             
  96.             if os.path.islink(fullname):
  97.                 displayname = name + '@'
  98.             
  99.             f.write('<li><a href="%s">%s</a>\n' % (urllib.quote(linkname), cgi.escape(displayname)))
  100.         
  101.         f.write('</ul>\n<hr>\n</body>\n</html>\n')
  102.         length = f.tell()
  103.         f.seek(0)
  104.         self.send_response(200)
  105.         self.send_header('Content-type', 'text/html')
  106.         self.send_header('Content-Length', str(length))
  107.         self.end_headers()
  108.         return f
  109.  
  110.     
  111.     def translate_path(self, path):
  112.         path = path.split('?', 1)[0]
  113.         path = path.split('#', 1)[0]
  114.         path = posixpath.normpath(urllib.unquote(path))
  115.         words = path.split('/')
  116.         words = filter(None, words)
  117.         path = os.getcwd()
  118.         for word in words:
  119.             (drive, word) = os.path.splitdrive(word)
  120.             (head, word) = os.path.split(word)
  121.             if word in (os.curdir, os.pardir):
  122.                 continue
  123.             
  124.             path = os.path.join(path, word)
  125.         
  126.         return path
  127.  
  128.     
  129.     def copyfile(self, source, outputfile):
  130.         shutil.copyfileobj(source, outputfile)
  131.  
  132.     
  133.     def guess_type(self, path):
  134.         (base, ext) = posixpath.splitext(path)
  135.         if ext in self.extensions_map:
  136.             return self.extensions_map[ext]
  137.         ext = ext.lower()
  138.         if ext in self.extensions_map:
  139.             return self.extensions_map[ext]
  140.         return self.extensions_map['']
  141.  
  142.     if not mimetypes.inited:
  143.         mimetypes.init()
  144.     
  145.     extensions_map = mimetypes.types_map.copy()
  146.     extensions_map.update({
  147.         '': 'application/octet-stream',
  148.         '.py': 'text/plain',
  149.         '.c': 'text/plain',
  150.         '.h': 'text/plain' })
  151.  
  152.  
  153. def test(HandlerClass = SimpleHTTPRequestHandler, ServerClass = BaseHTTPServer.HTTPServer):
  154.     BaseHTTPServer.test(HandlerClass, ServerClass)
  155.  
  156. if __name__ == '__main__':
  157.     test()
  158.  
  159.