Ticket #10435: runserver-custom-handler2.diff

File runserver-custom-handler2.diff, 3.5 KB (added by Trevor Caira, 16 years ago)

Only accept subclasses of WSGIHandler (this prevents strange issues when the object was found but is not a WSGIHandler). Handle options in such a way that testserver works.

  • docs/ref/django-admin.txt

     
    512512
    513513    django-admin.py runserver --noreload
    514514
     515--handler
     516~~~~~~~~~
     517
     518Use the ``--handler`` option to specify an alternate WSGI handler class.
     519This allows you to use the development server with a custom handler
     520subclass instead of the default
     521``django.core.handlers.wsgi.WSGIHandler``. Overriding the WSGI handler
     522is useful in case you want to override any aspect of request handling,
     523such as customizing 500 error handling. This option should be passed a
     524dotted path to a ``WSGIHandler`` subclass.
     525
     526Example usage::
     527
     528    django-admin.py runserver --handler=wsgi.handler.WSGIHandler
     529
    515530Examples of using different ports and addresses
    516531~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    517532
  • django/core/management/commands/runserver.py

     
    33import os
    44import sys
    55
     6from django.core.handlers import wsgi
     7
     8def load_wsgi_handler(dotted_path=None):
     9    if dotted_path is None:
     10        return wsgi.WSGIHandler
     11    try:
     12        dot = dotted_path.rindex('.')
     13    except ValueError:
     14        raise ValueError, "%r isn't a WSGI handler class" % dotted_path
     15    handler_module, handler_classname = dotted_path[:dot], \
     16                                        dotted_path[dot+1:]
     17    __import__(handler_module, {}, {}, [])
     18    mod = sys.modules[handler_module]
     19    try:
     20        handler_class = getattr(mod, handler_classname)
     21    except AttributeError:
     22        raise ValueError, 'Handler module "%s" does not define a "%s" \
     23class' % (handler_module, handler_classname)
     24    try:
     25        is_subclass = issubclass(handler_class, wsgi.WSGIHandler)
     26    except TypeError:
     27        is_subclass = False
     28    if not is_subclass:
     29        raise ValueError, 'Handler class "%s" is not a WSGIHandler \
     30subclass.' % dotted_path
     31    return handler_class
     32
    633class Command(BaseCommand):
    734    option_list = BaseCommand.option_list + (
    835        make_option('--noreload', action='store_false', dest='use_reloader', default=True,
    936            help='Tells Django to NOT use the auto-reloader.'),
    1037        make_option('--adminmedia', dest='admin_media_path', default='',
    1138            help='Specifies the directory from which to serve admin media.'),
     39        make_option('--handler', dest='handler',
     40                    help='Specify an alterate WSGI handler class'),
    1241    )
    1342    help = "Starts a lightweight Web server for development."
    1443    args = '[optional port number, or ipaddr:port]'
     
    1948    def handle(self, addrport='', *args, **options):
    2049        import django
    2150        from django.core.servers.basehttp import run, AdminMediaHandler, WSGIServerException
    22         from django.core.handlers.wsgi import WSGIHandler
    2351        if args:
    2452            raise CommandError('Usage is runserver %s' % self.args)
    2553        if not addrport:
     
    3563
    3664        if not port.isdigit():
    3765            raise CommandError("%r is not a valid port number." % port)
    38 
     66        WSGIHandler = load_wsgi_handler(options.get('handler'))
    3967        use_reloader = options.get('use_reloader', True)
    4068        admin_media_path = options.get('admin_media_path', '')
    4169        shutdown_message = options.get('shutdown_message', '')
Back to Top