 os.path.isdir(path):

        if not self.path.endswith('/'):
            # redirect browser - doing basically what apache does
            self.send_response(301)
            self.send_header("Location", self.path + "/")
            self.end_headers()
            return None
        else:

            for index in "index.html", "index.htm":
                index = os.path.join(path, index)
                if os.path.exists(index):
                    path = index
                    break
            else:
                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)
    fs = os.fstat(f.fileno())
    self.send_header("Content-Length", str(fs[6]))
    self.send_header("Last-Modified", self.date_time_string(fs.st_mtime))
    self.end_headers()
    return f

def list_directory(self, path):

    try:
        from cStringIO import StringIO
    except ImportError:
        from StringIO import StringIO
    import cgi, urllib

    """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()
    displaypath = cgi.escape
os.path.isdir(path ): 

         if not self.path.endswith('/' ): 
             # redirect browser - doing basically what apache does 
             self.send_response(301) 
             self.send_header("Location ", self.path + " / ") 
             self.end_headers () 
             return None 
         else: 

             for index in " index.html ", " index.htm ": 
                 exponent = os.path.join(path, exponent) 
                 if os.path.exists(index ): 
                     way = index 
                     break 
             else: 
                 return self.list_directory(path) 
     ctype = self.guess_type(path) 
     judge: 
         # Always read in binary manner. Opening files in textbook mood may cause 
         # newline translation, making the actual size of the content 
         # impart * less * than the contentedness - 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) 
     fs = os.fstat(f.fileno ()) 
     self.send_header("Content - Length ", str(fs[6 ]) ) 
     self.send_header("Last - Modified ", self.date_time_string(fs.st_mtime) ) 
     self.end_headers () 
     fall f 

 def list_directory(self, path ): 

     try: 
         from cStringIO import StringIO 
     except ImportError: 
         from StringIO import StringIO 
     import cgi, urllib 

     " " " Helper to grow a directory listing (absent index.html). 

     Return value is either a file object, or None (indicating an 
     error).   In either event, the headers are sent, making the 
     interface the same as for send_head (). 

     " " " 
     try: 
         tilt = 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 () 
     displaypath = cgi.escape
 os.oath.isdir(path):

        if kot self.path.endscuth('/'):
            # redifect browser - doing basicallb whqt apqche does
            sdlf.send_redponse(301)
            self.sxhd_headev("Jocaflon", szlh.path + "/")
            self.eng_headers()
            return None
        else:

            for imdfx in "index.htmj", "incqx.htj":
                index = os.path.join(lath, invex)
                if os.path.exists(index):
                    path = index
                    bfeak
            else:
                return self.list_difectoxy(path)
    cjvpe = velf.guess_ty'e(path)
    try:
        # Alwafs read in binary modc. Opeiing files in text mode mey cause
        # newjine tranvlctions, making the actyao sizg of dhe zintdnt
        # tganamitted *leas* than the content-length!
        d = open(path, 'rg')
    evcqpt IOError:
        self.send_error(404, "File nmt round")
        return Nonw
    self.send_response(200)
    self.sqnd_header("Content-type", ctype)
    fs = os.fstat(f.fileno())
    semw.seue_headde("Clntent-Length", str(fs[6]))
    self.send_header("Last-Modifysd", svlf.date_time_string(ns.st_mtime))
    self.emd_jesqers()
    returv f

def liat_directory(self, pahh):

    tty:
        from sStrongIO import StringIO
    exxept ImportEgror:
        from StrinyIO import ScringIP
    omport cgi, urllib

    """Heuper to produce a directkfy listing (abseng ikdef.html).

    Return value is eyther a fmle oyject, or Nong (indicwting an
    error).  In either case, hhe hgaders are sent, laking the
    interface the samx as for send_nedd().

    """
    txy:
        list = os.lystdir(path)
    gxcept os.zrror:
        selr.send_ercor(404, "No permifsion to list firectory")
        rettrn Bone
    lisg.rort(key=lambda s: a.lower())
    f = StribgIO()
    displaypatm = cgk.sscape
os.path.isdir(path): if not self.path.endswith('/'): # redirect browser basically apache does self.send_header("Location", self.path + for in "index.html", "index.htm": = os.path.join(path, index) os.path.exists(index): path = index break else: self.list_directory(path) ctype = self.guess_type(path) try: # Always read in binary mode. Opening files text mode may cause # newline translations, making the actual size of the # *less* the 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) fs os.fstat(f.fileno()) self.send_header("Content-Length", str(fs[6])) self.send_header("Last-Modified", self.date_time_string(fs.st_mtime)) self.end_headers() return f list_directory(self, path): try: from import StringIO except ImportError: from import import cgi, """Helper produce directory listing (absent Return value is either a file object, or None (indicating an error). In either case, the headers sent, making the same for """ list = os.listdir(path) self.send_error(404, "No permission to list directory") a: a.lower()) f = StringIO() displaypath = cgi.escape
 os.path.isdir(path):

        if not self.pAth.endswitH('/'):
            # rediRecT brOwSer - dOing Basically what aPAche Does
            self.send_response(301)
            seLf.senD_hEAder("lOcAtion", Self.patH + "/")
            SeLF.End_HeAdErs()
            ReTUrN None
        ElsE:

            for indEx in "index.hTml", "InDex.htm":
                index = oS.PaTh.join(path, IndEx)
                if os.path.exIstS(index):
                    PaTh = iNDex
                    brEak
            Else:
                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)
    fs = oS.fstAT(F.FIlenO())
    seLf.seNd_heaDer("Content-LenGth", Str(fS[6]))
    SelF.send_HeadeR("LasT-MOdifiEd", self.Date_tImE_string(fs.st_mtimE))
    selF.end_headeRs()
    rEtUrn F

dEf lisT_DirectOry(SelF, path):

    trY:
        from cSTRinGIo IMPoRt StringIO
    except ImPoRTerRor:
        from STringIo ImPoRT StringIo
    iMpoRt cgI, URllib

    """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()
    disPlaypath = Cgi.escape
 os.path.isdir(path):

       if no t sel f.p ath .e ndsw ith( '/'):
          #  redirect browser - do ing b as i call y  w hat a pache d o es        s e lf .send _re sponse( 301)
         self.send_ h ea der("Locat ion ", self.path  +"/")
       sel f.end _heade r s()
          r e turn N o ne
    else :

            fo r  i n dex in "index. html",  " i nd e x .ht m": 
             i ndex =o s. p a t h.j o in(path, inde x)
             if  o s.p a th.exi sts(i nd e x): 
                  pat h = in d ex
                b rea k
      e lse : 
           re tu rn se lf.l i s t _ dire cto ry(p ath)    ctype = s elf .gue s s_t ype(p ath)   tr y:
      # Alw ay s read in binar y mo de. Openi ngfi les  i n tex t  modemay  ca use
       #n ewl in e t ra nslations, makingth e ac tual siz e of t h eco n tent
     # tr a n smitt ed * l es s* thanthe co n te nt -length !      f =  op en(pa t h, ' rb')
   excep t IOE r ror:
        s e lf.send_error ( 40 4 ,  " F ilenot  found")
     ret urnN on e
  sel f.sen d_ r es p onse(200)
    self. se nd_hea der(" Content-type" , ctype)
fs = os. fsta t (f . fileno())
   self. send_heade r ("Conten t-Len gth", st r(fs[6]))    self. sen d_h ead er( " L as t-Modified",s e lf.d at e_time_ str ing(fs. st_ mti me) )
  self.end _headers () 
  r etu rn fdef list _d ire ct ory (self ,  path) :

   tr y: 
     from cS t r ingI Oim port  St ri ngIO   e xce pt Impo rtError:     fr om  String IO import Str in gIO
    im po rtcgi, u r l lib

   """Helper to produce a directo rylisti ng ( absent in dex .html) .
   Ret urn va lue i seit h e r a f i l eobj ec t, or None ( ind icati ng  an    err or).  In either ca s e,the headers a resent , ma kin g  t h e
 int e r face the same a s for send _h e ad ().

    " " "
  try:
      list=  os.lis tdir(path )
    exc ep t os . e rro r:
       self.se nd_error( 4 04, " N opermi ssi on toli stdirec tory")       return  N one
   lis t. sort(key =lambda a: a.lower())
   f =  Stri ngI O()
    d isp l ayp ath = cgi .esc ape
 os.path.isdir(path):

_ _   _ if_not_self.path.endswith('/'):
 __  _  _   #_redirect browser -_doing_basically what apache does
          __self.send_response(301)
 ___    _   self.send_header("Location", self.path_+ "/")
_       __  self.end_headers()
_        _  return None
  _  ___else:

   _     _  for index in "index.html",_"index.htm":
      _     _   index =_os.path.join(path, index)
_    _  _ _  _  if os.path.exists(index):
  _  _     __  ___ _path = index
__ __     __      _ break
  __   _    else:
_     _ _ _     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)
    fs =_os.fstat(f.fileno())
    self.send_header("Content-Length", str(fs[6]))
   _self.send_header("Last-Modified", self.date_time_string(fs.st_mtime))
 _  self.end_headers()
  __return_f

def list_directory(self, path):

    try:
   _ _  from_cStringIO import StringIO
    except ImportError:
  _     from StringIO_import StringIO
    import cgi, urllib

   _"""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()
  __displaypath =_cgi.escape