Code

Ticket #7735: runserver-ipv6-with-docs-r7920.diff

File runserver-ipv6-with-docs-r7920.diff, 4.5 KB (added by jbalonso, 6 years ago)

IPv6 support for runserver and docs against rev 7920

Line 
1Index: django/core/servers/basehttp.py
2===================================================================
3--- django/core/servers/basehttp.py     (revision 7920)
4+++ django/core/servers/basehttp.py     (working copy)
5@@ -658,8 +658,14 @@
6         start_response(status, headers.items())
7         return output
8 
9-def run(addr, port, wsgi_handler):
10+def run(addr, port, wsgi_handler, enable_ipv6=False):
11     server_address = (addr, port)
12-    httpd = WSGIServer(server_address, WSGIRequestHandler)
13+    if not enable_ipv6:
14+        httpd = WSGIServer(server_address, WSGIRequestHandler)
15+    else:
16+        import socket
17+        class WSGIServerV6(WSGIServer):
18+            address_family = socket.AF_INET6
19+        httpd = WSGIServerV6(server_address, WSGIRequestHandler)
20     httpd.set_app(wsgi_handler)
21     httpd.serve_forever()
22Index: django/core/management/commands/runserver.py
23===================================================================
24--- django/core/management/commands/runserver.py        (revision 7920)
25+++ django/core/management/commands/runserver.py        (working copy)
26@@ -5,6 +5,8 @@
27 
28 class Command(BaseCommand):
29     option_list = BaseCommand.option_list + (
30+        make_option('--ipv6', '-6', action='store_true', dest='enable_ipv6', default=False,
31+            help='Enables IPv6 support.'),
32         make_option('--noreload', action='store_false', dest='use_reloader', default=True,
33             help='Tells Django to NOT use the auto-reloader.'),
34         make_option('--adminmedia', dest='admin_media_path', default='',
35@@ -20,6 +22,12 @@
36         import django
37         from django.core.servers.basehttp import run, AdminMediaHandler, WSGIServerException
38         from django.core.handlers.wsgi import WSGIHandler
39+        enable_ipv6=options.get('enable_ipv6')
40+        if enable_ipv6:
41+                import socket
42+                if not hasattr(socket, 'AF_INET6'):
43+                        raise CommandError("This Python does not support IPv6.")
44+
45         if args:
46             raise CommandError('Usage is runserver %s' % self.args)
47         if not addrport:
48@@ -27,11 +35,12 @@
49             port = '8000'
50         else:
51             try:
52-                addr, port = addrport.split(':')
53+                addr, port = addrport.rsplit(':',1)
54             except ValueError:
55                 addr, port = '', addrport
56         if not addr:
57-            addr = '127.0.0.1'
58+            if not enable_ipv6: addr = '127.0.0.1'
59+            else: addr = '::1'
60 
61         if not port.isdigit():
62             raise CommandError("%r is not a valid port number." % port)
63@@ -51,7 +60,7 @@
64             try:
65                 path = admin_media_path or django.__path__[0] + '/contrib/admin/media'
66                 handler = AdminMediaHandler(WSGIHandler(), path)
67-                run(addr, int(port), handler)
68+                run(addr, int(port), handler, enable_ipv6=enable_ipv6)
69             except WSGIServerException, e:
70                 # Use helpful error messages instead of ugly tracebacks.
71                 ERRORS = {
72Index: docs/django-admin.txt
73===================================================================
74--- docs/django-admin.txt       (revision 7920)
75+++ docs/django-admin.txt       (working copy)
76@@ -498,7 +498,7 @@
77 Note that the default IP address, 127.0.0.1, is not accessible from other
78 machines on your network. To make your development server viewable to other
79 machines on the network, use its own IP address (e.g. ``192.168.2.1``) or
80-``0.0.0.0``.
81+``0.0.0.0`` or ``::`` (with IPv6 enabled).
82 
83 --adminmedia
84 ~~~~~~~~~~~~
85@@ -524,6 +524,17 @@
86 
87     django-admin.py runserver --noreload
88 
89+--ipv6
90+~~~~~~
91+
92+Use the ``--ipv6`` option to tell Django to enable IPv6 for the development
93+server. This allows you to specify IPv6 addresses in colon-delimited form and
94+changes the default IP address from 127.0.0.1 to ::1.
95+
96+Example usage::
97+
98+    django-admin.py runserver --ipv6
99+
100 Examples of using different ports and addresses
101 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
102 
103@@ -535,6 +546,10 @@
104 
105        django-admin.py runserver 1.2.3.4:8000
106 
107+Port 8000 on IPv6 address 2001:0db8:1234:5678::9::
108+
109+       django-admin.py runserver --ipv6 2001:0db8:1234:5678::9:8000
110+
111 Port 7000 on IP address 127.0.0.1::
112 
113     django-admin.py runserver 7000
114@@ -543,6 +558,10 @@
115 
116     django-admin.py runserver 1.2.3.4:7000
117 
118+Port 7000 on IPv6 address 2001:0db8:1234:5678::9::
119+
120+    django-admin.py runserver --ipv6 2001:0db8:1234:5678::9:7000
121+
122 Serving static files with the development server
123 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
124