Ticket #7735: ticket7735.diff

File ticket7735.diff, 9.3 KB (added by Łukasz Rekucki, 13 years ago)

Updated patch: Use "[200::addr:1]:port" format for IPv6 and detect it. Enable ipv6 in testserver.

  • django/core/management/commands/runserver.py

    diff --git a/django/core/management/commands/runserver.py b/django/core/management/commands/runserver.py
    index 49e270d..618fa4d 100644
    a b  
    11from optparse import make_option
    22import os
     3import socket
    34import sys
    45import warnings
     6import re
    57
    68from django.core.management.base import BaseCommand, CommandError
    79
     10naiveip_re = r'^(?:(?P<addr>\d{1,3}(?:\.\d{1,3}){3}|\[[a-fA-F0-9:]+\]):)?(?P<port>\d+)$'
     11DEFAULT_PORT = "8000"
     12
    813class Command(BaseCommand):
    914    option_list = BaseCommand.option_list + (
     15        make_option('--ipv6', '-6', action='store_true', dest='enable_ipv6', default=False,
     16            help='Force the use of IPv6 address.'),
    1017        make_option('--noreload', action='store_false', dest='use_reloader', default=True,
    1118            help='Tells Django to NOT use the auto-reloader.'),
    1219        make_option('--nostatic', action="store_false", dest='use_static_handler', default=True,
    class Command(BaseCommand):  
    2734        from django.core.servers.basehttp import run, AdminMediaHandler, WSGIServerException
    2835        from django.core.handlers.wsgi import WSGIHandler
    2936        from django.contrib.staticfiles.handlers import StaticFilesHandler
     37        enable_ipv6 = options.get('enable_ipv6')
     38        if enable_ipv6 and not hasattr(socket, 'AF_INET6'):
     39            raise CommandError("Your Python does not support IPv6.")
     40
    3041        if args:
    3142            raise CommandError('Usage is runserver %s' % self.args)
    3243        if not addrport:
    3344            addr = ''
    34             port = '8000'
     45            port = DEFAULT_PORT
    3546        else:
    36             try:
    37                 addr, port = addrport.split(':')
    38             except ValueError:
    39                 addr, port = '', addrport
     47            m = re.match(naiveip_re, addrport)
     48            if m is None:
     49                raise CommandError("%r is not a valid port number or address:port pair." % addrport)
     50            addr, port = m.groups()
     51            if not port.isdigit():
     52                raise CommandError("%r is not a valid port number." % port)
     53            if addr:
     54                if addr[0] == '[' and addr[-1] == ']':
     55                    enable_ipv6 = True
     56                    addr = addr[1:-1]
     57                elif enable_ipv6:
     58                    raise CommandError("IPv6 addresses must be surrounded with brackets")
    4059        if not addr:
    41             addr = '127.0.0.1'
    42 
    43         if not port.isdigit():
    44             raise CommandError("%r is not a valid port number." % port)
    45 
     60            addr = (enable_ipv6 and '::1') or '127.0.0.1'
    4661        use_reloader = options.get('use_reloader', True)
    4762        admin_media_path = options.get('admin_media_path', '')
    4863        shutdown_message = options.get('shutdown_message', '')
    class Command(BaseCommand):  
    5671            print "Validating models..."
    5772            self.validate(display_num_errors=True)
    5873            print "\nDjango version %s, using settings %r" % (django.get_version(), settings.SETTINGS_MODULE)
    59             print "Development server is running at http://%s:%s/" % (addr, port)
     74            fmt_addr = (enable_ipv6 and '[%s]' % addr) or addr
     75            print "Development server is running at http://%s:%s/" % (fmt_addr, port)
    6076            print "Quit the server with %s." % quit_command
    6177
    6278            # django.core.management.base forces the locale to en-us. We should
    class Command(BaseCommand):  
    7389                    handler = StaticFilesHandler(handler)
    7490                # serve admin media like old-school (deprecation pending)
    7591                handler = AdminMediaHandler(handler, admin_media_path)
    76                 run(addr, int(port), handler)
     92                run(addr, int(port), handler, enable_ipv6=enable_ipv6)
    7793            except WSGIServerException, e:
    7894                # Use helpful error messages instead of ugly tracebacks.
    7995                ERRORS = {
  • django/core/management/commands/testserver.py

    diff --git a/django/core/management/commands/testserver.py b/django/core/management/commands/testserver.py
    index d3d9698..be5b9ea 100644
    a b class Command(BaseCommand):  
    99        make_option('--addrport', action='store', dest='addrport',
    1010            type='string', default='',
    1111            help='port number or ipaddr:port to run the server on'),
     12        make_option('--ipv6', '-6', action='store_true', dest='enable_ipv6', default=False,
     13            help='Forces IPv6 support.'),
    1214    )
    1315    help = 'Runs a development server with data from the given fixture(s).'
    1416    args = '[fixture ...]'
    class Command(BaseCommand):  
    3335        # a strange error -- it causes this handle() method to be called
    3436        # multiple times.
    3537        shutdown_message = '\nServer stopped.\nNote that the test database, %r, has not been deleted. You can explore it on your own.' % db_name
    36         call_command('runserver', addrport=addrport, shutdown_message=shutdown_message, use_reloader=False)
     38        call_command('runserver', addrport=addrport, shutdown_message=shutdown_message, use_reloader=False, enable_ipv6=options['enable_ipv6'])
  • django/core/servers/basehttp.py

    diff --git a/django/core/servers/basehttp.py b/django/core/servers/basehttp.py
    index 9c92a88..35bb990 100644
    a b been reviewed for security issues. Don't use it for production use.  
    1010from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
    1111import os
    1212import re
     13import socket
    1314import sys
    1415import urllib
    1516import warnings
    class AdminMediaHandler(StaticFilesHandler):  
    678679        """
    679680        return path.startswith(self.media_url[2]) and not self.media_url[1]
    680681
     682class WSGIServerV6(WSGIServer):
     683    address_family = socket.AF_INET6
    681684
    682 def run(addr, port, wsgi_handler):
     685def run(addr, port, wsgi_handler, enable_ipv6=False):
    683686    server_address = (addr, port)
    684     httpd = WSGIServer(server_address, WSGIRequestHandler)
     687    server_class = (enable_ipv6 and WSGIServerV6) or WSGIServer
     688    httpd = server_class(server_address, WSGIRequestHandler)
    685689    httpd.set_app(wsgi_handler)
    686690    httpd.serve_forever()
  • docs/ref/django-admin.txt

    diff --git a/docs/ref/django-admin.txt b/docs/ref/django-admin.txt
    index 14fc69f..0570fcb 100644
    a b runserver [port or ipaddr:port]  
    630630.. django-admin:: runserver
    631631
    632632Starts a lightweight development Web server on the local machine. By default,
    633 the server runs on port 8000 on the IP address 127.0.0.1. You can pass in an
     633the server runs on port 8000 on the IP address ``127.0.0.1``. You can pass in an
    634634IP address and port number explicitly.
    635635
    636636If you run this script as a user with normal privileges (recommended), you
    them to standard output, but it won't stop the server.  
    654654You can run as many servers as you want, as long as they're on separate ports.
    655655Just execute ``django-admin.py runserver`` more than once.
    656656
    657 Note that the default IP address, 127.0.0.1, is not accessible from other
     657Note that the default IP address, ``127.0.0.1``, is not accessible from other
    658658machines on your network. To make your development server viewable to other
    659659machines on the network, use its own IP address (e.g. ``192.168.2.1``) or
    660 ``0.0.0.0``.
     660``0.0.0.0`` or ``::`` (with IPv6 enabled).
     661
     662.. versionchanged:: 1.3
     663
     664You can also provide an IPv6 address surrounded by brackets
     665(eg. ``[200a::1]:8000``). This will automaticaly enable IPv6 support.
    661666
    662667.. django-admin-option:: --adminmedia
    663668
    Example usage::  
    706711
    707712    django-admin.py runserver --insecure
    708713
     714.. django-admin-option:: --ipv6, -6
     715
     716.. versionadded:: 1.3
     717
     718Use the ``--ipv6`` (or shorter ``-6``) option to tell Django to use IPv6 for
     719the development server. This changes the default IP address from
     720``127.0.0.1`` to ``::1``.
     721
     722Example usage::
     723
     724    django-admin.py runserver --ipv6
     725
    709726Examples of using different ports and addresses
    710727~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    711728
    712 Port 8000 on IP address 127.0.0.1::
     729Port 8000 on IP address ``127.0.0.1``::
    713730
    714         django-admin.py runserver
     731    django-admin.py runserver
    715732
    716 Port 8000 on IP address 1.2.3.4::
     733Port 8000 on IP address ``1.2.3.4``::
    717734
    718         django-admin.py runserver 1.2.3.4:8000
     735    django-admin.py runserver 1.2.3.4:8000
    719736
    720 Port 7000 on IP address 127.0.0.1::
     737Port 7000 on IP address ``127.0.0.1``::
    721738
    722739    django-admin.py runserver 7000
    723740
    724 Port 7000 on IP address 1.2.3.4::
     741Port 7000 on IP address ``1.2.3.4``::
    725742
    726743    django-admin.py runserver 1.2.3.4:7000
    727744
     745Port 8000 on IPv6 address ``::1``::
     746
     747    django-admin.py runserver -6
     748
     749Port 7000 on IPv6 address ``::1``::
     750
     751    django-admin.py runserver -6 7000
     752
     753Port 7000 on IPv6 address ``2001:0db8:1234:5678::9``::
     754
     755    django-admin.py runserver [2001:0db8:1234:5678::9]:7000
     756
    728757Serving static files with the development server
    729758~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    730759
    templates.  
    9881017.. django-admin-option:: --addrport [port number or ipaddr:port]
    9891018
    9901019Use ``--addrport`` to specify a different port, or IP address and port, from
    991 the default of 127.0.0.1:8000. This value follows exactly the same format and
     1020the default of ``127.0.0.1:8000``. This value follows exactly the same format and
    9921021serves exactly the same function as the argument to the ``runserver`` command.
    9931022
    9941023Examples:
Back to Top