<s> from datetime import datetime <EOL> from optparse import make_option <EOL> from distutils . version import LooseVersion <EOL> import os <EOL> import ssl <EOL> import sys <EOL> from django . core . servers . basehttp import WSGIRequestHandler <EOL> from django . core . servers . basehttp import WSGIServer <EOL> from django . core . management . base import CommandError <EOL> from django . core . management . commands import runserver <EOL> from django . contrib . staticfiles . handlers import StaticFilesHandler <EOL> from django import get_version <EOL> try : <EOL> from django . core . servers . basehttp import WSGIServerException <EOL> except ImportError : <EOL> from socket import error as WSGIServerException <EOL> if LooseVersion ( get_version ( ) ) >= LooseVersion ( '<STR_LIT>' ) : <EOL> from django . utils . _os import upath <EOL> else : <EOL> upath = unicode <EOL> class SecureHTTPServer ( WSGIServer ) : <EOL> def __init__ ( self , address , handler_cls , certificate , key ) : <EOL> super ( SecureHTTPServer , self ) . __init__ ( address , handler_cls ) <EOL> self . socket = ssl . wrap_socket ( self . socket , certfile = certificate , <EOL> keyfile = key , server_side = True , <EOL> ssl_version = ssl . PROTOCOL_TLSv1 , <EOL> cert_reqs = ssl . CERT_NONE ) <EOL> class WSGIRequestHandler ( WSGIRequestHandler ) : <EOL> def get_environ ( self ) : <EOL> env = super ( WSGIRequestHandler , self ) . get_environ ( ) <EOL> env [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> return env <EOL> def default_ssl_files_dir ( ) : <EOL> import sslserver as app_module <EOL> mod_path = os . path . dirname ( upath ( app_module . __file__ ) ) <EOL> ssl_dir = os . path . join ( mod_path , "<STR_LIT>" ) <EOL> return ssl_dir <EOL> class Command ( runserver . Command ) : <EOL> def add_arguments ( self , parser ) : <EOL> super ( Command , self ) . add_arguments ( parser ) <EOL> parser . add_argument ( "<STR_LIT>" , <EOL> default = os . path . join ( default_ssl_files_dir ( ) , <EOL> "<STR_LIT>" ) , <EOL> help = "<STR_LIT>" ) , <EOL> parser . add_argument ( "<STR_LIT>" , <EOL> default = os . path . join ( default_ssl_files_dir ( ) , <EOL> "<STR_LIT>" ) , <EOL> help = "<STR_LIT>" ) , <EOL> parser . add_argument ( "<STR_LIT>" , dest = '<STR_LIT>' , <EOL> action = '<STR_LIT>' , default = None ) , <EOL> parser . add_argument ( "<STR_LIT>" , dest = '<STR_LIT>' , <EOL> action = '<STR_LIT:store_true>' ) , <EOL> help = "<STR_LIT>" <EOL> def get_handler ( self , * args , ** options ) : <EOL> """<STR_LIT>""" <EOL> handler = super ( Command , self ) . get_handler ( * args , ** options ) <EOL> insecure_serving = options . get ( '<STR_LIT>' , False ) <EOL> if self . should_use_static_handler ( options ) : <EOL> return StaticFilesHandler ( handler ) <EOL> return handler <EOL> def should_use_static_handler ( self , options ) : <EOL> from django . conf import settings <EOL> use_static_handler = options . get ( '<STR_LIT>' ) <EOL> if use_static_handler : <EOL> return True <EOL> if ( use_static_handler is None and <EOL> '<STR_LIT>' in settings . INSTALLED_APPS ) : <EOL> return True <EOL> return False <EOL> def check_certs ( self , key_file , cert_file ) : <EOL> if not os . path . exists ( key_file ) : <EOL> raise CommandError ( "<STR_LIT>" % key_file ) <EOL> if not os . path . exists ( cert_file ) : <EOL> raise CommandError ( "<STR_LIT>" % <EOL> cert_file ) <EOL> def inner_run ( self , * args , ** options ) : <EOL> key_file = options . get ( "<STR_LIT:key>" ) <EOL> cert_file = options . get ( "<STR_LIT>" ) <EOL> self . check_certs ( key_file , cert_file ) <EOL> from django . conf import settings <EOL> from django . utils import translation <EOL> threading = options . get ( '<STR_LIT>' ) <EOL> shutdown_message = options . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> quit_command = ( sys . platform == '<STR_LIT:win32>' ) and '<STR_LIT>' or '<STR_LIT>' <EOL> self . stdout . write ( "<STR_LIT>" ) <EOL> self . check ( display_num_errors = True ) <EOL> self . stdout . write ( ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> ) % { <EOL> "<STR_LIT>" : datetime . now ( ) . strftime ( '<STR_LIT>' ) , <EOL> "<STR_LIT:version>" : self . get_version ( ) , <EOL> "<STR_LIT>" : settings . SETTINGS_MODULE , <EOL> "<STR_LIT>" : self . _raw_ipv6 and '<STR_LIT>' % self . addr or self . addr , <EOL> "<STR_LIT:port>" : self . port , <EOL> "<STR_LIT>" : quit_command , <EOL> "<STR_LIT>" : cert_file , <EOL> "<STR_LIT:key>" : key_file <EOL> } ) <EOL> translation . activate ( settings . LANGUAGE_CODE ) <EOL> try : <EOL> handler = self . get_handler ( * args , ** options ) <EOL> server = SecureHTTPServer ( ( self . addr , int ( self . port ) ) , <EOL> WSGIRequestHandler , <EOL> cert_file , key_file ) <EOL> server . set_app ( handler ) <EOL> server . serve_forever ( ) <EOL> except WSGIServerException : <EOL> e = sys . exc_info ( ) [ <NUM_LIT:1> ] <EOL> ERRORS = { <EOL> <NUM_LIT> : "<STR_LIT>" , <EOL> <NUM_LIT> : "<STR_LIT>" , <EOL> <NUM_LIT> : "<STR_LIT>" , <EOL> } <EOL> try : <EOL> error_text = ERRORS [ e . args [ <NUM_LIT:0> ] . args [ <NUM_LIT:0> ] ] <EOL> except ( AttributeError , KeyError ) : <EOL> error_text = str ( e ) <EOL> self . stderr . write ( "<STR_LIT>" % error_text ) <EOL> os . _exit ( <NUM_LIT:1> ) <EOL> except KeyboardInterrupt : <EOL> if shutdown_message : <EOL> self . stdout . write ( shutdown_message ) <EOL> sys . exit ( <NUM_LIT:0> ) </s>
