<s> from BaseHTTPServer import HTTPServer , BaseHTTPRequestHandler <EOL> from SocketServer import ThreadingMixIn , ForkingMixIn <EOL> import sys <EOL> import os <EOL> import cgi <EOL> import mimetypes <EOL> import shutil <EOL> import urlparse <EOL> import posixpath <EOL> import urllib <EOL> class Server : <EOL> def __init__ ( self ) : <EOL> server_address = ( '<STR_LIT>' , <NUM_LIT> ) <EOL> httpd = TestHTTPServer ( server_address , TestRequestHandler ) <EOL> httpd . serve_forever ( ) <EOL> class TestHTTPServer ( ThreadingMixIn , HTTPServer ) : <EOL> pass <EOL> class TestRequestHandler ( BaseHTTPRequestHandler ) : <EOL> def __init__ ( self , request , client_address , server ) : <EOL> BaseHTTPRequestHandler . __init__ ( self , request , client_address , server ) <EOL> self . protocol_version = '<STR_LIT>' <EOL> def do_GET ( self ) : <EOL> self . handle_data ( ) <EOL> def do_POST ( self ) : <EOL> self . form = cgi . FieldStorage ( <EOL> fp = self . rfile , <EOL> headers = self . headers , <EOL> environ = { '<STR_LIT>' : '<STR_LIT:POST>' , <EOL> '<STR_LIT>' : self . headers [ '<STR_LIT:Content-Type>' ] , <EOL> } , <EOL> keep_blank_values = True , <EOL> strict_parsing = False ) <EOL> self . handle_data ( ) <EOL> def handle_data ( self ) : <EOL> if self . path == '<STR_LIT:/>' : <EOL> p = '<STR_LIT>' <EOL> elif self . path . endswith ( '<STR_LIT>' ) : <EOL> self . handleUpload ( ) <EOL> return <EOL> else : <EOL> p = self . path <EOL> path = self . translate_path ( p ) <EOL> if not os . path . exists ( path ) : <EOL> p = '<STR_LIT>' + p <EOL> path = self . translate_path ( p ) <EOL> ctype = self . guess_type ( path ) <EOL> try : <EOL> f = open ( path ) <EOL> except IOError : <EOL> print '<STR_LIT>' % path <EOL> self . send_error ( <NUM_LIT> , '<STR_LIT>' ) <EOL> return <EOL> self . send_response ( <NUM_LIT:200> ) <EOL> self . send_header ( '<STR_LIT>' , ctype ) <EOL> self . send_header ( '<STR_LIT>' , self . date_time_string ( ) ) <EOL> self . end_headers ( ) <EOL> self . copyfile ( f , self . wfile ) <EOL> f . close ( ) <EOL> def handleUpload ( self ) : <EOL> self . send_response ( <NUM_LIT:200> ) <EOL> self . end_headers ( ) <EOL> fileitem = self . form [ '<STR_LIT>' ] <EOL> filename = os . path . basename ( fileitem . filename ) <EOL> filepath = os . path . join ( os . getcwd ( ) , '<STR_LIT>' , filename ) <EOL> f = open ( filepath , '<STR_LIT:wb>' , <NUM_LIT> ) <EOL> def fbuffer ( f , chunk_size = <NUM_LIT> ) : <EOL> while True : <EOL> chunk = f . read ( chunk_size ) <EOL> if not chunk : break <EOL> yield chunk <EOL> for chunk in fbuffer ( fileitem . file ) : <EOL> f . write ( chunk ) <EOL> f . close ( ) <EOL> self . wfile . write ( '<STR_LIT>' ) <EOL> return <EOL> def translate_path ( self , path ) : <EOL> path = path . decode ( '<STR_LIT:utf-8>' ) <EOL> path = urlparse . urlparse ( path ) [ <NUM_LIT:2> ] <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> shutil . copyfileobj ( source , outputfile ) <EOL> def guess_type ( self , path ) : <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> } ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> Server ( ) </s>
