<s> import datetime <EOL> import sys <EOL> import threading <EOL> from django . conf import settings <EOL> from django . core . management . commands . runserver import Command as RunserverCommand <EOL> from django . utils import six <EOL> from django . utils . encoding import get_system_encoding <EOL> from channels import DEFAULT_CHANNEL_LAYER , channel_layers <EOL> from channels . handler import ViewConsumer <EOL> from channels . log import setup_logger <EOL> from channels . staticfiles import StaticFilesConsumer <EOL> from channels . worker import Worker <EOL> class Command ( RunserverCommand ) : <EOL> def add_arguments ( self , parser ) : <EOL> super ( Command , self ) . add_arguments ( parser ) <EOL> parser . add_argument ( '<STR_LIT>' , action = '<STR_LIT>' , dest = '<STR_LIT>' , default = True , <EOL> help = '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , action = '<STR_LIT>' , dest = '<STR_LIT>' , default = True , <EOL> help = '<STR_LIT>' ) <EOL> def handle ( self , * args , ** options ) : <EOL> self . verbosity = options . get ( "<STR_LIT>" , <NUM_LIT:1> ) <EOL> self . logger = setup_logger ( '<STR_LIT>' , self . verbosity ) <EOL> super ( Command , self ) . handle ( * args , ** options ) <EOL> def inner_run ( self , * args , ** options ) : <EOL> if not options . get ( "<STR_LIT>" , True ) or DEFAULT_CHANNEL_LAYER not in channel_layers : <EOL> return RunserverCommand . inner_run ( self , * args , ** options ) <EOL> self . channel_layer = channel_layers [ DEFAULT_CHANNEL_LAYER ] <EOL> self . channel_layer . router . check_default ( <EOL> http_consumer = self . get_consumer ( ) , <EOL> ) <EOL> self . stdout . write ( "<STR_LIT>" ) <EOL> self . check ( display_num_errors = True ) <EOL> self . check_migrations ( ) <EOL> quit_command = '<STR_LIT>' if sys . platform == '<STR_LIT:win32>' else '<STR_LIT>' <EOL> now = datetime . datetime . now ( ) . strftime ( '<STR_LIT>' ) <EOL> if six . PY2 : <EOL> now = now . decode ( get_system_encoding ( ) ) <EOL> self . stdout . write ( now ) <EOL> self . stdout . write ( ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> ) % { <EOL> "<STR_LIT:version>" : self . get_version ( ) , <EOL> "<STR_LIT>" : settings . SETTINGS_MODULE , <EOL> "<STR_LIT>" : '<STR_LIT>' % self . addr if self . _raw_ipv6 else self . addr , <EOL> "<STR_LIT:port>" : self . port , <EOL> "<STR_LIT>" : quit_command , <EOL> "<STR_LIT>" : self . channel_layer , <EOL> } ) <EOL> if options . get ( "<STR_LIT>" , True ) : <EOL> for _ in range ( <NUM_LIT:4> ) : <EOL> worker = WorkerThread ( self . channel_layer , self . logger ) <EOL> worker . daemon = True <EOL> worker . start ( ) <EOL> self . logger . debug ( "<STR_LIT>" , self . addr , self . port ) <EOL> try : <EOL> from daphne . server import Server <EOL> Server ( <EOL> channel_layer = self . channel_layer , <EOL> host = self . addr , <EOL> port = int ( self . port ) , <EOL> signal_handlers = not options [ '<STR_LIT>' ] , <EOL> action_logger = self . log_action , <EOL> http_timeout = <NUM_LIT> , <EOL> ) . run ( ) <EOL> self . logger . debug ( "<STR_LIT>" ) <EOL> except KeyboardInterrupt : <EOL> shutdown_message = options . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> if shutdown_message : <EOL> self . stdout . write ( shutdown_message ) <EOL> return <EOL> def log_action ( self , protocol , action , details ) : <EOL> """<STR_LIT>""" <EOL> msg = "<STR_LIT>" % datetime . datetime . now ( ) . strftime ( "<STR_LIT>" ) <EOL> if protocol == "<STR_LIT:http>" and action == "<STR_LIT>" : <EOL> msg += "<STR_LIT>" % details <EOL> if <NUM_LIT:200> <= details [ '<STR_LIT:status>' ] < <NUM_LIT> : <EOL> msg = self . style . HTTP_SUCCESS ( msg ) <EOL> elif <NUM_LIT:100> <= details [ '<STR_LIT:status>' ] < <NUM_LIT:200> : <EOL> msg = self . style . HTTP_INFO ( msg ) <EOL> elif details [ '<STR_LIT:status>' ] == <NUM_LIT> : <EOL> msg = self . style . HTTP_NOT_MODIFIED ( msg ) <EOL> elif <NUM_LIT> <= details [ '<STR_LIT:status>' ] < <NUM_LIT> : <EOL> msg = self . style . HTTP_REDIRECT ( msg ) <EOL> elif details [ '<STR_LIT:status>' ] == <NUM_LIT> : <EOL> msg = self . style . HTTP_NOT_FOUND ( msg ) <EOL> elif <NUM_LIT> <= details [ '<STR_LIT:status>' ] < <NUM_LIT> : <EOL> msg = self . style . HTTP_BAD_REQUEST ( msg ) <EOL> else : <EOL> msg = self . style . HTTP_SERVER_ERROR ( msg ) <EOL> elif protocol == "<STR_LIT>" and action == "<STR_LIT>" : <EOL> msg += "<STR_LIT>" % details <EOL> elif protocol == "<STR_LIT>" and action == "<STR_LIT>" : <EOL> msg += "<STR_LIT>" % details <EOL> sys . stderr . write ( msg ) <EOL> def get_consumer ( self , * args , ** options ) : <EOL> """<STR_LIT>""" <EOL> use_static_handler = options . get ( '<STR_LIT>' , True ) <EOL> insecure_serving = options . get ( '<STR_LIT>' , False ) <EOL> if use_static_handler and ( settings . DEBUG or insecure_serving ) : <EOL> return StaticFilesConsumer ( ) <EOL> else : <EOL> return ViewConsumer ( ) <EOL> class WorkerThread ( threading . Thread ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , channel_layer , logger ) : <EOL> super ( WorkerThread , self ) . __init__ ( ) <EOL> self . channel_layer = channel_layer <EOL> self . logger = logger <EOL> def run ( self ) : <EOL> self . logger . debug ( "<STR_LIT>" ) <EOL> worker = Worker ( channel_layer = self . channel_layer , signal_handlers = False ) <EOL> worker . run ( ) <EOL> self . logger . debug ( "<STR_LIT>" ) </s>
