<s> """<STR_LIT>""" <EOL> from __future__ import unicode_literals <EOL> import os <EOL> import socket <EOL> import sys <EOL> import traceback <EOL> try : <EOL> from urllib . parse import urljoin <EOL> except ImportError : <EOL> from urlparse import urljoin <EOL> from django . utils . six . moves import socketserver <EOL> from wsgiref import simple_server <EOL> from wsgiref . util import FileWrapper <EOL> import django <EOL> from django . core . exceptions import ImproperlyConfigured <EOL> from django . core . management . color import color_style <EOL> from django . core . wsgi import get_wsgi_application <EOL> from django . utils . importlib import import_module <EOL> __all__ = [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> def get_internal_wsgi_application ( ) : <EOL> """<STR_LIT>""" <EOL> from django . conf import settings <EOL> app_path = getattr ( settings , '<STR_LIT>' ) <EOL> if app_path is None : <EOL> return get_wsgi_application ( ) <EOL> module_name , attr = app_path . rsplit ( '<STR_LIT:.>' , <NUM_LIT:1> ) <EOL> try : <EOL> mod = import_module ( module_name ) <EOL> except ImportError as e : <EOL> raise ImproperlyConfigured ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" % ( app_path , module_name , e ) ) <EOL> try : <EOL> app = getattr ( mod , attr ) <EOL> except AttributeError as e : <EOL> raise ImproperlyConfigured ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> % ( app_path , attr , module_name , e ) ) <EOL> return app <EOL> class WSGIServerException ( Exception ) : <EOL> pass <EOL> class ServerHandler ( simple_server . ServerHandler , object ) : <EOL> error_status = str ( "<STR_LIT>" ) <EOL> def write ( self , data ) : <EOL> """<STR_LIT>""" <EOL> assert isinstance ( data , bytes ) , "<STR_LIT>" <EOL> if not self . status : <EOL> raise AssertionError ( "<STR_LIT>" ) <EOL> elif not self . headers_sent : <EOL> self . bytes_sent = len ( data ) <EOL> self . send_headers ( ) <EOL> else : <EOL> self . bytes_sent += len ( data ) <EOL> length = len ( data ) <EOL> if length > <NUM_LIT> : <EOL> offset = <NUM_LIT:0> <EOL> while offset < length : <EOL> chunk_size = min ( <NUM_LIT> , length ) <EOL> self . _write ( data [ offset : offset + chunk_size ] ) <EOL> self . _flush ( ) <EOL> offset += chunk_size <EOL> else : <EOL> self . _write ( data ) <EOL> self . _flush ( ) <EOL> def error_output ( self , environ , start_response ) : <EOL> super ( ServerHandler , self ) . error_output ( environ , start_response ) <EOL> return [ '<STR_LIT:\n>' . join ( traceback . format_exception ( * sys . exc_info ( ) ) ) ] <EOL> def finish_response ( self ) : <EOL> try : <EOL> if not self . result_is_file ( ) or not self . sendfile ( ) : <EOL> for data in self . result : <EOL> self . write ( data ) <EOL> self . finish_content ( ) <EOL> finally : <EOL> self . close ( ) <EOL> class WSGIServer ( simple_server . WSGIServer , object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> if kwargs . pop ( '<STR_LIT>' , False ) : <EOL> self . address_family = socket . AF_INET6 <EOL> super ( WSGIServer , self ) . __init__ ( * args , ** kwargs ) <EOL> def server_bind ( self ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> super ( WSGIServer , self ) . server_bind ( ) <EOL> except Exception as e : <EOL> raise WSGIServerException ( e ) <EOL> self . setup_environ ( ) <EOL> class WSGIRequestHandler ( simple_server . WSGIRequestHandler , object ) : <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> from django . conf import settings <EOL> self . admin_static_prefix = urljoin ( settings . STATIC_URL , '<STR_LIT>' ) <EOL> self . path = '<STR_LIT>' <EOL> self . style = color_style ( ) <EOL> super ( WSGIRequestHandler , self ) . __init__ ( * args , ** kwargs ) <EOL> def address_string ( self ) : <EOL> return self . client_address [ <NUM_LIT:0> ] <EOL> def log_message ( self , format , * args ) : <EOL> if ( self . path . startswith ( self . admin_static_prefix ) <EOL> or self . path == '<STR_LIT>' ) : <EOL> return <EOL> msg = "<STR_LIT>" % ( self . log_date_time_string ( ) , format % args ) <EOL> if args [ <NUM_LIT:1> ] [ <NUM_LIT:0> ] == '<STR_LIT:2>' : <EOL> msg = self . style . HTTP_SUCCESS ( msg ) <EOL> elif args [ <NUM_LIT:1> ] [ <NUM_LIT:0> ] == '<STR_LIT:1>' : <EOL> msg = self . style . HTTP_INFO ( msg ) <EOL> elif args [ <NUM_LIT:1> ] == '<STR_LIT>' : <EOL> msg = self . style . HTTP_NOT_MODIFIED ( msg ) <EOL> elif args [ <NUM_LIT:1> ] [ <NUM_LIT:0> ] == '<STR_LIT:3>' : <EOL> msg = self . style . HTTP_REDIRECT ( msg ) <EOL> elif args [ <NUM_LIT:1> ] == '<STR_LIT>' : <EOL> msg = self . style . HTTP_NOT_FOUND ( msg ) <EOL> elif args [ <NUM_LIT:1> ] [ <NUM_LIT:0> ] == '<STR_LIT:4>' : <EOL> msg = self . style . HTTP_BAD_REQUEST ( msg ) <EOL> else : <EOL> msg = self . style . HTTP_SERVER_ERROR ( msg ) <EOL> sys . stderr . write ( msg ) <EOL> def run ( addr , port , wsgi_handler , ipv6 = False , threading = False ) : <EOL> server_address = ( addr , port ) <EOL> if threading : <EOL> httpd_cls = type ( str ( '<STR_LIT>' ) , ( socketserver . ThreadingMixIn , WSGIServer ) , { } ) <EOL> else : <EOL> httpd_cls = WSGIServer <EOL> httpd = httpd_cls ( server_address , WSGIRequestHandler , ipv6 = ipv6 ) <EOL> httpd . set_app ( wsgi_handler ) <EOL> httpd . serve_forever ( ) </s>
