<s> import BaseHTTPServer <EOL> import errno <EOL> import os <EOL> import mimetypes <EOL> import socket <EOL> import sys <EOL> import threading <EOL> class MinimalistHTTPServer ( BaseHTTPServer . HTTPServer ) : <EOL> '<STR_LIT>' <EOL> def set_file ( self , allowed_file ) : <EOL> '<STR_LIT>' <EOL> self . allowed_file = os . path . realpath ( allowed_file ) <EOL> class MinimalistHTTPRequestHandler ( BaseHTTPServer . BaseHTTPRequestHandler ) : <EOL> '''<STR_LIT>''' <EOL> def do_GET ( self ) : <EOL> '<STR_LIT>' <EOL> if self . path != '<STR_LIT:/>' + os . path . basename ( self . server . allowed_file ) : <EOL> self . send_error ( <NUM_LIT> ) <EOL> return <EOL> try : <EOL> fout = open ( self . server . allowed_file ) <EOL> except IOError : <EOL> self . send_error ( <NUM_LIT> ) <EOL> return <EOL> self . send_response ( <NUM_LIT:200> ) <EOL> mimetypes . init ( ) <EOL> mime_guess = mimetypes . guess_type ( self . server . allowed_file ) <EOL> if mime_guess [ <NUM_LIT:0> ] is not None : <EOL> self . send_header ( '<STR_LIT:Content-Type>' , mime_guess [ <NUM_LIT:0> ] ) <EOL> if mime_guess [ <NUM_LIT:1> ] is not None : <EOL> self . send_header ( '<STR_LIT>' , mime_guess [ <NUM_LIT:1> ] ) <EOL> statinfo = os . stat ( self . server . allowed_file ) <EOL> self . send_header ( '<STR_LIT>' , statinfo . st_size ) <EOL> self . end_headers ( ) <EOL> try : <EOL> self . wfile . write ( fout . read ( ) ) <EOL> except socket . error , e : <EOL> if errno . errorcode [ e [ <NUM_LIT:0> ] ] == '<STR_LIT>' : <EOL> pass <EOL> fout . close ( ) <EOL> return <EOL> class HTTPServerLauncher ( object ) : <EOL> '<STR_LIT>' <EOL> def __init__ ( self , server_addr , file ) : <EOL> self . server = MinimalistHTTPServer ( server_addr , <EOL> MinimalistHTTPRequestHandler ) <EOL> self . server . set_file ( file ) <EOL> def request_shutdown ( self ) : <EOL> if sys . version_info >= ( <NUM_LIT:2> , <NUM_LIT:6> ) : <EOL> self . server . shutdown ( ) <EOL> else : <EOL> self . run_it = False <EOL> def run ( self ) : <EOL> if sys . version_info >= ( <NUM_LIT:2> , <NUM_LIT:6> ) : <EOL> self . server . serve_forever ( ) <EOL> else : <EOL> self . run_it = True <EOL> while self . run_it == True : <EOL> self . server . handle_request ( ) </s>
