<s> """<STR_LIT>""" <EOL> __version__ = "<STR_LIT>" <EOL> __all__ = [ "<STR_LIT>" ] <EOL> import os <EOL> import posixpath <EOL> import BaseHTTPServer <EOL> import urllib <EOL> import cgi <EOL> import sys <EOL> import shutil <EOL> import mimetypes <EOL> try : <EOL> from cStringIO import StringIO <EOL> except ImportError : <EOL> from StringIO import StringIO <EOL> class SimpleHTTPRequestHandler ( BaseHTTPServer . BaseHTTPRequestHandler ) : <EOL> """<STR_LIT>""" <EOL> server_version = "<STR_LIT>" + __version__ <EOL> def do_GET ( self ) : <EOL> """<STR_LIT>""" <EOL> f = self . send_head ( ) <EOL> if f : <EOL> self . copyfile ( f , self . wfile ) <EOL> f . close ( ) <EOL> def do_HEAD ( self ) : <EOL> """<STR_LIT>""" <EOL> f = self . send_head ( ) <EOL> if f : <EOL> f . close ( ) <EOL> def send_head ( self ) : <EOL> """<STR_LIT>""" <EOL> path = self . translate_path ( self . path ) <EOL> f = None <EOL> if os . path . isdir ( path ) : <EOL> if not self . path . endswith ( '<STR_LIT:/>' ) : <EOL> self . send_response ( <NUM_LIT> ) <EOL> self . send_header ( "<STR_LIT>" , self . path + "<STR_LIT:/>" ) <EOL> self . end_headers ( ) <EOL> return None <EOL> for index in "<STR_LIT>" , "<STR_LIT>" : <EOL> index = os . path . join ( path , index ) <EOL> if os . path . exists ( index ) : <EOL> path = index <EOL> break <EOL> else : <EOL> return self . list_directory ( path ) <EOL> ctype = self . guess_type ( path ) <EOL> try : <EOL> f = open ( path , '<STR_LIT:rb>' ) <EOL> except IOError : <EOL> self . send_error ( <NUM_LIT> , "<STR_LIT>" ) <EOL> return None <EOL> self . send_response ( <NUM_LIT:200> ) <EOL> self . send_header ( "<STR_LIT>" , ctype ) <EOL> fs = os . fstat ( f . fileno ( ) ) if hasattr ( os , '<STR_LIT>' ) else os . stat ( path ) <EOL> self . send_header ( "<STR_LIT>" , str ( fs [ <NUM_LIT:6> ] ) ) <EOL> self . send_header ( "<STR_LIT>" , self . date_time_string ( fs . st_mtime ) ) <EOL> self . end_headers ( ) <EOL> return f <EOL> def list_directory ( self , path ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> list = os . listdir ( path ) <EOL> except os . error : <EOL> self . send_error ( <NUM_LIT> , "<STR_LIT>" ) <EOL> return None <EOL> list . sort ( key = lambda a : a . lower ( ) ) <EOL> f = StringIO ( ) <EOL> displaypath = cgi . escape ( urllib . unquote ( self . path ) ) <EOL> f . write ( '<STR_LIT>' ) <EOL> f . write ( "<STR_LIT>" % displaypath ) <EOL> f . write ( "<STR_LIT>" % displaypath ) <EOL> f . write ( "<STR_LIT>" ) <EOL> for name in list : <EOL> fullname = os . path . join ( path , name ) <EOL> displayname = linkname = name <EOL> if os . path . isdir ( fullname ) : <EOL> displayname = name + "<STR_LIT:/>" <EOL> linkname = name + "<STR_LIT:/>" <EOL> if os . path . islink ( fullname ) : <EOL> displayname = name + "<STR_LIT:@>" <EOL> f . write ( '<STR_LIT>' <EOL> % ( urllib . quote ( linkname ) , cgi . escape ( displayname ) ) ) <EOL> f . write ( "<STR_LIT>" ) <EOL> length = f . tell ( ) <EOL> f . seek ( <NUM_LIT:0> ) <EOL> self . send_response ( <NUM_LIT:200> ) <EOL> if not sys . platform . startswith ( "<STR_LIT>" ) : <EOL> encoding = sys . getfilesystemencoding ( ) <EOL> self . send_header ( "<STR_LIT>" , "<STR_LIT>" % encoding ) <EOL> self . send_header ( "<STR_LIT>" , str ( length ) ) <EOL> self . end_headers ( ) <EOL> return f <EOL> def translate_path ( self , path ) : <EOL> """<STR_LIT>""" <EOL> path = path . split ( '<STR_LIT:?>' , <NUM_LIT:1> ) [ <NUM_LIT:0> ] <EOL> path = path . split ( '<STR_LIT:#>' , <NUM_LIT:1> ) [ <NUM_LIT:0> ] <EOL> path = posixpath . normpath ( urllib . unquote ( path ) ) <EOL> words = path . split ( '<STR_LIT:/>' ) <EOL> words = filter ( None , words ) <EOL> path = os . getcwd ( ) <EOL> for word in words : <EOL> drive , word = os . path . splitdrive ( word ) <EOL> head , word = os . path . split ( word ) <EOL> if word in ( os . curdir , os . pardir ) : continue <EOL> path = os . path . join ( path , word ) <EOL> return path <EOL> def copyfile ( self , source , outputfile ) : <EOL> """<STR_LIT>""" <EOL> shutil . copyfileobj ( source , outputfile ) <EOL> def guess_type ( self , path ) : <EOL> """<STR_LIT>""" <EOL> base , ext = posixpath . splitext ( path ) <EOL> if ext in self . extensions_map : <EOL> return self . extensions_map [ ext ] <EOL> ext = ext . lower ( ) <EOL> if ext in self . extensions_map : <EOL> return self . extensions_map [ ext ] <EOL> else : <EOL> return self . extensions_map [ '<STR_LIT>' ] <EOL> if not mimetypes . inited : <EOL> mimetypes . init ( ) <EOL> extensions_map = mimetypes . types_map . copy ( ) <EOL> extensions_map . update ( { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } ) <EOL> def test ( HandlerClass = SimpleHTTPRequestHandler , <EOL> ServerClass = BaseHTTPServer . HTTPServer ) : <EOL> BaseHTTPServer . test ( HandlerClass , ServerClass ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> test ( ) </s>
