2013-09-22 21:55:09 +08:00
|
|
|
import errno
|
2007-08-16 14:06:55 +08:00
|
|
|
import os
|
2010-11-26 21:33:53 +08:00
|
|
|
import re
|
|
|
|
import socket
|
2015-01-28 20:35:27 +08:00
|
|
|
import sys
|
|
|
|
from datetime import datetime
|
2010-10-20 09:33:24 +08:00
|
|
|
|
2015-03-08 00:22:16 +08:00
|
|
|
from django.conf import settings
|
2010-10-20 09:33:24 +08:00
|
|
|
from django.core.management.base import BaseCommand, CommandError
|
2017-01-09 23:41:33 +08:00
|
|
|
from django.core.servers.basehttp import (
|
|
|
|
WSGIServer, get_internal_wsgi_application, run,
|
|
|
|
)
|
2016-12-01 18:38:01 +08:00
|
|
|
from django.utils import autoreload
|
2019-10-26 22:42:32 +08:00
|
|
|
from django.utils.regex_helper import _lazy_re_compile
|
2007-08-16 14:06:55 +08:00
|
|
|
|
2019-10-26 22:42:32 +08:00
|
|
|
naiveip_re = _lazy_re_compile(r"""^(?:
|
2011-01-15 14:31:38 +08:00
|
|
|
(?P<addr>
|
|
|
|
(?P<ipv4>\d{1,3}(?:\.\d{1,3}){3}) | # IPv4 address
|
|
|
|
(?P<ipv6>\[[a-fA-F0-9:]+\]) | # IPv6 address
|
|
|
|
(?P<fqdn>[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*) # FQDN
|
|
|
|
):)?(?P<port>\d+)$""", re.X)
|
2010-11-26 21:33:53 +08:00
|
|
|
|
2011-06-17 21:08:36 +08:00
|
|
|
|
2012-04-09 05:13:32 +08:00
|
|
|
class Command(BaseCommand):
|
2021-07-23 14:48:16 +08:00
|
|
|
help = "Starts a lightweight web server for development."
|
2007-08-16 14:06:55 +08:00
|
|
|
|
|
|
|
# Validation is called explicitly each time the server is reloaded.
|
2020-05-14 06:00:41 +08:00
|
|
|
requires_system_checks = []
|
2017-12-21 02:08:45 +08:00
|
|
|
stealth_options = ('shutdown_message',)
|
2021-07-25 07:16:00 +08:00
|
|
|
suppressed_base_arguments = {'--verbosity', '--traceback'}
|
2007-08-16 14:06:55 +08:00
|
|
|
|
2017-01-28 02:30:27 +08:00
|
|
|
default_addr = '127.0.0.1'
|
|
|
|
default_addr_ipv6 = '::1'
|
2015-05-12 14:24:03 +08:00
|
|
|
default_port = '8000'
|
2017-01-09 23:41:33 +08:00
|
|
|
protocol = 'http'
|
|
|
|
server_cls = WSGIServer
|
2015-05-12 14:24:03 +08:00
|
|
|
|
2014-06-07 04:39:33 +08:00
|
|
|
def add_arguments(self, parser):
|
2016-03-29 06:33:29 +08:00
|
|
|
parser.add_argument(
|
|
|
|
'addrport', nargs='?',
|
|
|
|
help='Optional port number, or ipaddr:port'
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
2017-04-02 08:03:56 +08:00
|
|
|
'--ipv6', '-6', action='store_true', dest='use_ipv6',
|
2016-03-29 06:33:29 +08:00
|
|
|
help='Tells Django to use an IPv6 address.',
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
2017-04-02 08:03:56 +08:00
|
|
|
'--nothreading', action='store_false', dest='use_threading',
|
2016-03-29 06:33:29 +08:00
|
|
|
help='Tells Django to NOT use threading.',
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
2017-04-02 08:03:56 +08:00
|
|
|
'--noreload', action='store_false', dest='use_reloader',
|
2016-03-29 06:33:29 +08:00
|
|
|
help='Tells Django to NOT use the auto-reloader.',
|
|
|
|
)
|
2020-12-24 23:29:54 +08:00
|
|
|
parser.add_argument(
|
|
|
|
'--skip-checks', action='store_true',
|
|
|
|
help='Skip system checks.',
|
|
|
|
)
|
2014-06-07 04:39:33 +08:00
|
|
|
|
2014-10-20 19:01:03 +08:00
|
|
|
def execute(self, *args, **options):
|
2016-03-03 10:01:36 +08:00
|
|
|
if options['no_color']:
|
2014-10-20 19:01:03 +08:00
|
|
|
# We rely on the environment because it's currently the only
|
|
|
|
# way to reach WSGIRequestHandler. This seems an acceptable
|
|
|
|
# compromise considering `runserver` runs indefinitely.
|
2017-01-20 17:20:53 +08:00
|
|
|
os.environ["DJANGO_COLORS"] = "nocolor"
|
2017-01-21 21:13:44 +08:00
|
|
|
super().execute(*args, **options)
|
2014-10-20 19:01:03 +08:00
|
|
|
|
2010-11-14 02:41:55 +08:00
|
|
|
def get_handler(self, *args, **options):
|
2017-01-26 03:02:33 +08:00
|
|
|
"""Return the default WSGI handler for the runner."""
|
2011-10-22 12:30:10 +08:00
|
|
|
return get_internal_wsgi_application()
|
2010-11-14 02:41:55 +08:00
|
|
|
|
2014-06-07 04:39:33 +08:00
|
|
|
def handle(self, *args, **options):
|
2013-06-10 03:07:23 +08:00
|
|
|
if not settings.DEBUG and not settings.ALLOWED_HOSTS:
|
|
|
|
raise CommandError('You must set settings.ALLOWED_HOSTS if DEBUG is False.')
|
|
|
|
|
2016-03-03 10:01:36 +08:00
|
|
|
self.use_ipv6 = options['use_ipv6']
|
2011-01-15 14:31:38 +08:00
|
|
|
if self.use_ipv6 and not socket.has_ipv6:
|
2010-11-26 21:33:53 +08:00
|
|
|
raise CommandError('Your Python does not support IPv6.')
|
2011-01-15 14:31:38 +08:00
|
|
|
self._raw_ipv6 = False
|
2016-03-03 10:01:36 +08:00
|
|
|
if not options['addrport']:
|
2010-11-14 02:41:55 +08:00
|
|
|
self.addr = ''
|
2015-05-12 14:24:03 +08:00
|
|
|
self.port = self.default_port
|
2007-08-16 14:06:55 +08:00
|
|
|
else:
|
2014-06-07 04:39:33 +08:00
|
|
|
m = re.match(naiveip_re, options['addrport'])
|
2010-11-26 21:33:53 +08:00
|
|
|
if m is None:
|
2011-01-15 14:31:38 +08:00
|
|
|
raise CommandError('"%s" is not a valid port number '
|
2014-06-07 04:39:33 +08:00
|
|
|
'or address:port pair.' % options['addrport'])
|
2011-01-15 14:31:38 +08:00
|
|
|
self.addr, _ipv4, _ipv6, _fqdn, self.port = m.groups()
|
2010-11-26 21:33:53 +08:00
|
|
|
if not self.port.isdigit():
|
|
|
|
raise CommandError("%r is not a valid port number." % self.port)
|
|
|
|
if self.addr:
|
2011-01-15 14:31:38 +08:00
|
|
|
if _ipv6:
|
2010-11-26 21:33:53 +08:00
|
|
|
self.addr = self.addr[1:-1]
|
|
|
|
self.use_ipv6 = True
|
2011-01-15 14:31:38 +08:00
|
|
|
self._raw_ipv6 = True
|
|
|
|
elif self.use_ipv6 and not _fqdn:
|
|
|
|
raise CommandError('"%s" is not a valid IPv6 address.' % self.addr)
|
2010-11-14 02:41:55 +08:00
|
|
|
if not self.addr:
|
2017-01-28 02:30:27 +08:00
|
|
|
self.addr = self.default_addr_ipv6 if self.use_ipv6 else self.default_addr
|
2016-03-03 10:01:36 +08:00
|
|
|
self._raw_ipv6 = self.use_ipv6
|
2014-06-07 04:39:33 +08:00
|
|
|
self.run(**options)
|
2007-08-16 14:06:55 +08:00
|
|
|
|
2014-06-07 04:39:33 +08:00
|
|
|
def run(self, **options):
|
2017-01-26 03:02:33 +08:00
|
|
|
"""Run the server, using the autoreloader if needed."""
|
2016-03-03 10:01:36 +08:00
|
|
|
use_reloader = options['use_reloader']
|
2010-11-14 02:41:55 +08:00
|
|
|
|
|
|
|
if use_reloader:
|
2019-01-14 09:33:47 +08:00
|
|
|
autoreload.run_with_reloader(self.inner_run, **options)
|
2010-11-14 02:41:55 +08:00
|
|
|
else:
|
2014-06-07 04:39:33 +08:00
|
|
|
self.inner_run(None, **options)
|
2010-11-14 02:41:55 +08:00
|
|
|
|
|
|
|
def inner_run(self, *args, **options):
|
2015-08-05 17:07:36 +08:00
|
|
|
# If an exception was silenced in ManagementUtility.execute in order
|
|
|
|
# to be raised in the child process, raise it now.
|
|
|
|
autoreload.raise_last_exception()
|
|
|
|
|
2016-03-03 10:01:36 +08:00
|
|
|
threading = options['use_threading']
|
|
|
|
# 'shutdown_message' is a stealth option.
|
2007-08-17 06:48:32 +08:00
|
|
|
shutdown_message = options.get('shutdown_message', '')
|
2013-05-17 22:33:36 +08:00
|
|
|
quit_command = 'CTRL-BREAK' if sys.platform == 'win32' else 'CONTROL-C'
|
2007-08-16 14:06:55 +08:00
|
|
|
|
2020-12-24 23:29:54 +08:00
|
|
|
if not options['skip_checks']:
|
|
|
|
self.stdout.write('Performing system checks...\n\n')
|
|
|
|
self.check(display_num_errors=True)
|
2016-02-12 18:02:36 +08:00
|
|
|
# Need to check migrations here, so can't use the
|
|
|
|
# requires_migrations_check attribute.
|
2015-05-09 23:01:40 +08:00
|
|
|
self.check_migrations()
|
2013-12-01 02:07:35 +08:00
|
|
|
now = datetime.now().strftime('%B %d, %Y - %X')
|
2015-03-08 00:22:16 +08:00
|
|
|
self.stdout.write(now)
|
2010-11-14 02:41:55 +08:00
|
|
|
self.stdout.write((
|
|
|
|
"Django version %(version)s, using settings %(settings)r\n"
|
2017-01-09 23:41:33 +08:00
|
|
|
"Starting development server at %(protocol)s://%(addr)s:%(port)s/\n"
|
2020-04-25 21:51:17 +08:00
|
|
|
"Quit the server with %(quit_command)s."
|
2010-11-14 02:41:55 +08:00
|
|
|
) % {
|
|
|
|
"version": self.get_version(),
|
|
|
|
"settings": settings.SETTINGS_MODULE,
|
2017-01-09 23:41:33 +08:00
|
|
|
"protocol": self.protocol,
|
2013-05-27 10:47:50 +08:00
|
|
|
"addr": '[%s]' % self.addr if self._raw_ipv6 else self.addr,
|
2010-11-14 02:41:55 +08:00
|
|
|
"port": self.port,
|
|
|
|
"quit_command": quit_command,
|
|
|
|
})
|
2008-08-31 04:17:58 +08:00
|
|
|
|
2010-11-14 02:41:55 +08:00
|
|
|
try:
|
|
|
|
handler = self.get_handler(*args, **options)
|
2011-06-17 21:08:36 +08:00
|
|
|
run(self.addr, int(self.port), handler,
|
2017-01-09 23:41:33 +08:00
|
|
|
ipv6=self.use_ipv6, threading=threading, server_cls=self.server_cls)
|
2019-01-28 23:01:35 +08:00
|
|
|
except OSError as e:
|
2010-11-14 02:41:55 +08:00
|
|
|
# Use helpful error messages instead of ugly tracebacks.
|
|
|
|
ERRORS = {
|
2013-09-22 21:55:09 +08:00
|
|
|
errno.EACCES: "You don't have permission to access that port.",
|
|
|
|
errno.EADDRINUSE: "That port is already in use.",
|
2015-03-08 00:22:16 +08:00
|
|
|
errno.EADDRNOTAVAIL: "That IP address can't be assigned to.",
|
2010-11-14 02:41:55 +08:00
|
|
|
}
|
2007-08-16 14:06:55 +08:00
|
|
|
try:
|
2013-09-22 21:55:09 +08:00
|
|
|
error_text = ERRORS[e.errno]
|
|
|
|
except KeyError:
|
2017-03-04 22:47:49 +08:00
|
|
|
error_text = e
|
2012-10-10 23:54:27 +08:00
|
|
|
self.stderr.write("Error: %s" % error_text)
|
2010-11-14 02:41:55 +08:00
|
|
|
# Need to use an OS exit because sys.exit doesn't work in a thread
|
|
|
|
os._exit(1)
|
|
|
|
except KeyboardInterrupt:
|
|
|
|
if shutdown_message:
|
2012-05-19 19:51:54 +08:00
|
|
|
self.stdout.write(shutdown_message)
|
2010-11-14 02:41:55 +08:00
|
|
|
sys.exit(0)
|