Ticket #14928: 14928-2.diff
File 14928-2.diff, 6.4 KB (added by , 14 years ago) |
---|
-
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 9 9 from django.core.servers.basehttp import AdminMediaHandler, run, WSGIServerException 10 10 from django.utils import autoreload 11 11 12 naiveip_re = r'^(?:(?P<addr>\d{1,3}(?:\.\d{1,3}){3}|\[[a-fA-F0-9:]+\]):)?(?P<port>\d+)$' 12 naiveip_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) 13 18 DEFAULT_PORT = "8000" 14 19 15 20 class BaseRunserverCommand(BaseCommand): … … class BaseRunserverCommand(BaseCommand): 33 38 34 39 def handle(self, addrport='', *args, **options): 35 40 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: 37 42 raise CommandError('Your Python does not support IPv6.') 38 43 if args: 39 44 raise CommandError('Usage is runserver %s' % self.args) 45 self._raw_ipv6 = False 40 46 if not addrport: 41 47 self.addr = '' 42 48 self.port = DEFAULT_PORT 43 49 else: 44 50 m = re.match(naiveip_re, addrport) 45 51 if m is None: 46 raise CommandError(' %r is not a valid port number'52 raise CommandError('"%s" is not a valid port number ' 47 53 'or address:port pair.' % addrport) 48 self.addr, self.port = m.groups()54 self.addr, _ipv4, _ipv6, _fqdn, self.port = m.groups() 49 55 if not self.port.isdigit(): 50 56 raise CommandError("%r is not a valid port number." % self.port) 51 57 if self.addr: 52 if self.addr.startswith('[') and self.addr.endswith(']'):58 if _ipv6: 53 59 self.addr = self.addr[1:-1] 54 60 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) 58 64 if not self.addr: 59 65 self.addr = self.use_ipv6 and '::1' or '127.0.0.1' 60 66 self.run(*args, **options) … … class BaseRunserverCommand(BaseCommand): 86 92 ) % { 87 93 "version": self.get_version(), 88 94 "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, 90 96 "port": self.port, 91 97 "quit_command": quit_command, 92 98 }) -
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:: 616 616 617 617 Run a FastCGI server as a daemon and write the spawned PID in a file. 618 618 619 runserver [port or ipaddr:port]619 runserver [port or address:port] 620 620 ------------------------------- 621 621 622 622 .. django-admin:: runserver … … machines on the network, use its own IP address (e.g. ``192.168.2.1``) or 653 653 654 654 .. versionchanged:: 1.3 655 655 656 You can also provide an IPv6 address surrounded by brackets 657 (eg. ``[200a::1]:8000``). This will automaticaly enable IPv6 support. 656 You can provide an IPv6 address surrounded by brackets 657 (e.g. ``[200a::1]:8000``). This will automatically enable IPv6 support. 658 659 A hostname containing ASCII-only characters can also be used. 658 660 659 661 .. django-admin-option:: --adminmedia 660 662 … … Port 7000 on IPv6 address ``2001:0db8:1234:5678::9``:: 721 723 722 724 django-admin.py runserver [2001:0db8:1234:5678::9]:7000 723 725 726 Port 8000 on IPv4 address of host ``localhost``:: 727 728 django-admin.py runserver localhost:8000 729 730 Port 8000 on IPv6 address of host ``localhost``:: 731 732 django-admin.py runserver -6 localhost:8000 733 724 734 Serving static files with the development server 725 735 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 726 736 -
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): 999 999 self.assertNoOutput(err) 1000 1000 self.assertOutput(out, '0 errors found') 1001 1001 1002 class 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 1002 1030 ########################################################################## 1003 1031 # COMMAND PROCESSING TESTS 1004 1032 # Check that user-space commands are correctly handled - in particular,