Ticket #14928: 14928-2.diff

File 14928-2.diff, 6.4 KB (added by Claude Paroz, 14 years ago)

Patch of lregucki with basic tests

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

    commit 47c3701da9455419805ebb34b8a41ac77c4e1d6d
    Author: Claude Paroz <claude@2xlibre.net>
    Date:   Mon Jan 10 10:11:03 2011 +0100
    
        Allow hostnames as runserver parameter
    
    diff --git a/django/core/management/commands/runserver.py b/django/core/management/commands/runserver.py
    index b63b57a..55c7869 100644
    a b from django.core.handlers.wsgi import WSGIHandler  
    99from django.core.servers.basehttp import AdminMediaHandler, run, WSGIServerException
    1010from django.utils import autoreload
    1111
    12 naiveip_re = r'^(?:(?P<addr>\d{1,3}(?:\.\d{1,3}){3}|\[[a-fA-F0-9:]+\]):)?(?P<port>\d+)$'
     12naiveip_re = re.compile(r"""^(?:
     13(?P<addr>
     14    (?P<ipv4>\d{1,3}(?:\.\d{1,3}){3}) |         # IPv4 address
     15    (?P<ipv6>\[[a-fA-F0-9:]+\]) |               # IPv6 address
     16    (?P<fqdn>[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*) # FQDN
     17):)?(?P<port>\d+)$""", re.X)
    1318DEFAULT_PORT = "8000"
    1419
    1520class BaseRunserverCommand(BaseCommand):
    class BaseRunserverCommand(BaseCommand):  
    3338
    3439    def handle(self, addrport='', *args, **options):
    3540        self.use_ipv6 = options.get('use_ipv6')
    36         if self.use_ipv6 and not hasattr(socket, 'AF_INET6'):
     41        if self.use_ipv6 and not socket.has_ipv6:
    3742            raise CommandError('Your Python does not support IPv6.')
    3843        if args:
    3944            raise CommandError('Usage is runserver %s' % self.args)
     45        self._raw_ipv6 = False
    4046        if not addrport:
    4147            self.addr = ''
    4248            self.port = DEFAULT_PORT
    4349        else:
    4450            m = re.match(naiveip_re, addrport)
    4551            if m is None:
    46                 raise CommandError('%r is not a valid port number'
     52                raise CommandError('"%s" is not a valid port number '
    4753                                   'or address:port pair.' % addrport)
    48             self.addr, self.port = m.groups()
     54            self.addr, _ipv4, _ipv6, _fqdn, self.port = m.groups()
    4955            if not self.port.isdigit():
    5056                raise CommandError("%r is not a valid port number." % self.port)
    5157            if self.addr:
    52                 if self.addr.startswith('[') and self.addr.endswith(']'):
     58                if _ipv6:
    5359                    self.addr = self.addr[1:-1]
    5460                    self.use_ipv6 = True
    55                 elif self.use_ipv6:
    56                     raise CommandError('IPv6 addresses must be surrounded '
    57                                        'with brackets, e.g. [::1].')
     61                    self._raw_ipv6 = True
     62                elif self.use_ipv6 and not _fqdn:
     63                    raise CommandError('"%s" is not a valid IPv6 address.' % self.addr)
    5864        if not self.addr:
    5965            self.addr = self.use_ipv6 and '::1' or '127.0.0.1'
    6066        self.run(*args, **options)
    class BaseRunserverCommand(BaseCommand):  
    8692        ) % {
    8793            "version": self.get_version(),
    8894            "settings": settings.SETTINGS_MODULE,
    89             "addr": self.use_ipv6 and '[%s]' % self.addr or self.addr,
     95            "addr": self._raw_ipv6 and '[%s]' % self.addr or self.addr,
    9096            "port": self.port,
    9197            "quit_command": quit_command,
    9298        })
  • docs/ref/django-admin.txt

    diff --git a/docs/ref/django-admin.txt b/docs/ref/django-admin.txt
    index 0985620..1150acb 100644
    a b Example usage::  
    616616
    617617Run a FastCGI server as a daemon and write the spawned PID in a file.
    618618
    619 runserver [port or ipaddr:port]
     619runserver [port or address:port]
    620620-------------------------------
    621621
    622622.. django-admin:: runserver
    machines on the network, use its own IP address (e.g. ``192.168.2.1``) or  
    653653
    654654.. versionchanged:: 1.3
    655655
    656 You can also provide an IPv6 address surrounded by brackets
    657 (eg. ``[200a::1]:8000``). This will automaticaly enable IPv6 support.
     656You can provide an IPv6 address surrounded by brackets
     657(e.g. ``[200a::1]:8000``). This will automatically enable IPv6 support.
     658
     659A hostname containing ASCII-only characters can also be used.
    658660
    659661.. django-admin-option:: --adminmedia
    660662
    Port 7000 on IPv6 address ``2001:0db8:1234:5678::9``::  
    721723
    722724    django-admin.py runserver [2001:0db8:1234:5678::9]:7000
    723725
     726Port 8000 on IPv4 address of host ``localhost``::
     727
     728    django-admin.py runserver localhost:8000
     729
     730Port 8000 on IPv6 address of host ``localhost``::
     731
     732    django-admin.py runserver -6 localhost:8000
     733
    724734Serving static files with the development server
    725735~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    726736
  • tests/regressiontests/admin_scripts/tests.py

    diff --git a/tests/regressiontests/admin_scripts/tests.py b/tests/regressiontests/admin_scripts/tests.py
    index 94d8f93..acdc135 100644
    a b class ManageValidate(AdminScriptTestCase):  
    999999        self.assertNoOutput(err)
    10001000        self.assertOutput(out, '0 errors found')
    10011001
     1002class ManageRunserver(AdminScriptTestCase):
     1003    def setUp(self):
     1004        from django.core.management.commands.runserver import BaseRunserverCommand
     1005        def monkey_run(*args, **options): return
     1006       
     1007        self.cmd = BaseRunserverCommand()
     1008        self.cmd.run = monkey_run
     1009       
     1010    def test_runserver_addrport(self):
     1011        self.cmd.handle()
     1012        self.assertEqual((self.cmd.addr, self.cmd.port), ('127.0.0.1', '8000'))
     1013        self.cmd.handle(addrport="1.2.3.4:8000")
     1014        self.assertEqual((self.cmd.addr, self.cmd.port), ('1.2.3.4', '8000'))
     1015        self.cmd.handle(addrport="7000")
     1016        self.assertEqual((self.cmd.addr, self.cmd.port), ('127.0.0.1', '7000'))
     1017        # IPv6
     1018        self.cmd.handle(addrport="", **{'use_ipv6': True})
     1019        self.assertEqual((self.cmd.addr, self.cmd.port), ('::1', '8000'))
     1020        self.cmd.handle(addrport="7000", **{'use_ipv6': True})
     1021        self.assertEqual((self.cmd.addr, self.cmd.port), ('::1', '7000'))
     1022        self.cmd.handle(addrport="[2001:0db8:1234:5678::9]:7000")
     1023        self.assertEqual((self.cmd.addr, self.cmd.port), ('2001:0db8:1234:5678::9', '7000'))
     1024        # Hostname
     1025        self.cmd.handle(addrport="localhost:8000")
     1026        self.assertEqual((self.cmd.addr, self.cmd.port), ('localhost', '8000'))
     1027        self.cmd.handle(addrport="test.domain.local:7000")
     1028        self.assertEqual((self.cmd.addr, self.cmd.port), ('test.domain.local', '7000'))
     1029
    10021030##########################################################################
    10031031# COMMAND PROCESSING TESTS
    10041032# Check that user-space commands are correctly handled - in particular,
Back to Top