<s> """<STR_LIT>""" <EOL> from __future__ import unicode_literals <EOL> import logging <EOL> import socket <EOL> import sys <EOL> from wsgiref import simple_server <EOL> from django . core . exceptions import ImproperlyConfigured <EOL> from django . core . handlers . wsgi import ISO_8859_1 , UTF_8 <EOL> from django . core . wsgi import get_wsgi_application <EOL> from django . utils import six <EOL> from django . utils . encoding import uri_to_iri <EOL> from django . utils . module_loading import import_string <EOL> from django . utils . six . moves import socketserver <EOL> __all__ = ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> logger = logging . getLogger ( '<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> try : <EOL> return import_string ( app_path ) <EOL> except ImportError as e : <EOL> msg = ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" % ( { <EOL> '<STR_LIT>' : app_path , <EOL> '<STR_LIT>' : e , <EOL> } ) <EOL> ) <EOL> six . reraise ( ImproperlyConfigured , ImproperlyConfigured ( msg ) , <EOL> sys . exc_info ( ) [ <NUM_LIT:2> ] ) <EOL> def is_broken_pipe_error ( ) : <EOL> exc_type , exc_value = sys . exc_info ( ) [ : <NUM_LIT:2> ] <EOL> return issubclass ( exc_type , socket . error ) and exc_value . args [ <NUM_LIT:0> ] == <NUM_LIT:32> <EOL> class WSGIServer ( simple_server . WSGIServer , object ) : <EOL> """<STR_LIT>""" <EOL> request_queue_size = <NUM_LIT:10> <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> if kwargs . pop ( '<STR_LIT>' , False ) : <EOL> self . address_family = socket . AF_INET6 <EOL> self . allow_reuse_address = kwargs . pop ( '<STR_LIT>' , True ) <EOL> super ( WSGIServer , self ) . __init__ ( * args , ** kwargs ) <EOL> def server_bind ( self ) : <EOL> """<STR_LIT>""" <EOL> super ( WSGIServer , self ) . server_bind ( ) <EOL> self . setup_environ ( ) <EOL> def handle_error ( self , request , client_address ) : <EOL> if is_broken_pipe_error ( ) : <EOL> logger . info ( "<STR_LIT>" , client_address ) <EOL> else : <EOL> super ( WSGIServer , self ) . handle_error ( request , client_address ) <EOL> class ServerHandler ( simple_server . ServerHandler , object ) : <EOL> def handle_error ( self ) : <EOL> if not is_broken_pipe_error ( ) : <EOL> super ( ServerHandler , self ) . handle_error ( ) <EOL> class WSGIRequestHandler ( simple_server . WSGIRequestHandler , object ) : <EOL> def address_string ( self ) : <EOL> return self . client_address [ <NUM_LIT:0> ] <EOL> def log_message ( self , format , * args ) : <EOL> extra = { <EOL> '<STR_LIT>' : self . request , <EOL> '<STR_LIT>' : self . log_date_time_string ( ) , <EOL> } <EOL> if args [ <NUM_LIT:1> ] [ <NUM_LIT:0> ] == '<STR_LIT:4>' : <EOL> if args [ <NUM_LIT:0> ] . startswith ( str ( '<STR_LIT>' ) ) : <EOL> extra [ '<STR_LIT>' ] = <NUM_LIT> <EOL> logger . error ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" , extra = extra , <EOL> ) <EOL> return <EOL> if args [ <NUM_LIT:1> ] . isdigit ( ) and len ( args [ <NUM_LIT:1> ] ) == <NUM_LIT:3> : <EOL> status_code = int ( args [ <NUM_LIT:1> ] ) <EOL> extra [ '<STR_LIT>' ] = status_code <EOL> if status_code >= <NUM_LIT> : <EOL> level = logger . error <EOL> elif status_code >= <NUM_LIT> : <EOL> level = logger . warning <EOL> else : <EOL> level = logger . info <EOL> else : <EOL> level = logger . info <EOL> level ( format , * args , extra = extra ) <EOL> def get_environ ( self ) : <EOL> for k , v in self . headers . items ( ) : <EOL> if '<STR_LIT:_>' in k : <EOL> del self . headers [ k ] <EOL> env = super ( WSGIRequestHandler , self ) . get_environ ( ) <EOL> path = self . path <EOL> if '<STR_LIT:?>' in path : <EOL> path = path . partition ( '<STR_LIT:?>' ) [ <NUM_LIT:0> ] <EOL> path = uri_to_iri ( path ) . encode ( UTF_8 ) <EOL> env [ '<STR_LIT>' ] = path . decode ( ISO_8859_1 ) if six . PY3 else path <EOL> return env <EOL> def handle ( self ) : <EOL> """<STR_LIT>""" <EOL> self . raw_requestline = self . rfile . readline ( <NUM_LIT> ) <EOL> if len ( self . raw_requestline ) > <NUM_LIT> : <EOL> self . requestline = '<STR_LIT>' <EOL> self . request_version = '<STR_LIT>' <EOL> self . command = '<STR_LIT>' <EOL> self . send_error ( <NUM_LIT> ) <EOL> return <EOL> if not self . parse_request ( ) : <EOL> return <EOL> handler = ServerHandler ( <EOL> self . rfile , self . wfile , self . get_stderr ( ) , self . get_environ ( ) <EOL> ) <EOL> handler . request_handler = self <EOL> handler . run ( self . server . get_app ( ) ) <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> if threading : <EOL> httpd . daemon_threads = True <EOL> httpd . set_app ( wsgi_handler ) <EOL> httpd . serve_forever ( ) </s>
