2007-09-10 05:57:59 +08:00
|
|
|
from optparse import make_option
|
2012-09-10 19:35:21 +08:00
|
|
|
from datetime import datetime
|
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
|
2007-08-16 14:06:55 +08:00
|
|
|
import sys
|
2010-11-26 21:33:53 +08:00
|
|
|
import socket
|
2010-10-20 09:33:24 +08:00
|
|
|
|
|
|
|
from django.core.management.base import BaseCommand, CommandError
|
2013-09-22 21:55:09 +08:00
|
|
|
from django.core.servers.basehttp import run, get_internal_wsgi_application
|
2010-11-14 02:41:55 +08:00
|
|
|
from django.utils import autoreload
|
2007-08-16 14:06:55 +08:00
|
|
|
|
2011-01-15 14:31:38 +08:00
|
|
|
naiveip_re = re.compile(r"""^(?:
|
|
|
|
(?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
|
|
|
DEFAULT_PORT = "8000"
|
|
|
|
|
2011-06-17 21:08:36 +08:00
|
|
|
|
2012-04-09 05:13:32 +08:00
|
|
|
class Command(BaseCommand):
|
2007-09-10 05:57:59 +08:00
|
|
|
option_list = BaseCommand.option_list + (
|
2010-11-26 21:33:53 +08:00
|
|
|
make_option('--ipv6', '-6', action='store_true', dest='use_ipv6', default=False,
|
|
|
|
help='Tells Django to use a IPv6 address.'),
|
2011-06-17 21:08:36 +08:00
|
|
|
make_option('--nothreading', action='store_false', dest='use_threading', default=True,
|
2011-06-17 22:09:00 +08:00
|
|
|
help='Tells Django to NOT use threading.'),
|
2007-09-10 05:57:59 +08:00
|
|
|
make_option('--noreload', action='store_false', dest='use_reloader', default=True,
|
|
|
|
help='Tells Django to NOT use the auto-reloader.'),
|
|
|
|
)
|
2007-08-16 14:06:55 +08:00
|
|
|
help = "Starts a lightweight Web server for development."
|
2007-09-10 05:57:59 +08:00
|
|
|
args = '[optional port number, or ipaddr:port]'
|
2007-08-16 14:06:55 +08:00
|
|
|
|
|
|
|
# Validation is called explicitly each time the server is reloaded.
|
|
|
|
requires_model_validation = False
|
|
|
|
|
2010-11-14 02:41:55 +08:00
|
|
|
def get_handler(self, *args, **options):
|
|
|
|
"""
|
|
|
|
Returns 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
|
|
|
|
2007-08-16 22:34:01 +08:00
|
|
|
def handle(self, addrport='', *args, **options):
|
2013-06-10 03:07:23 +08:00
|
|
|
from django.conf import settings
|
|
|
|
|
|
|
|
if not settings.DEBUG and not settings.ALLOWED_HOSTS:
|
|
|
|
raise CommandError('You must set settings.ALLOWED_HOSTS if DEBUG is False.')
|
|
|
|
|
2010-11-26 21:33:53 +08:00
|
|
|
self.use_ipv6 = options.get('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.')
|
2007-10-14 02:45:49 +08:00
|
|
|
if args:
|
2007-08-16 22:34:01 +08:00
|
|
|
raise CommandError('Usage is runserver %s' % self.args)
|
2011-01-15 14:31:38 +08:00
|
|
|
self._raw_ipv6 = False
|
2007-08-16 14:06:55 +08:00
|
|
|
if not addrport:
|
2010-11-14 02:41:55 +08:00
|
|
|
self.addr = ''
|
2010-11-26 21:33:53 +08:00
|
|
|
self.port = DEFAULT_PORT
|
2007-08-16 14:06:55 +08:00
|
|
|
else:
|
2010-11-26 21:33:53 +08:00
|
|
|
m = re.match(naiveip_re, addrport)
|
|
|
|
if m is None:
|
2011-01-15 14:31:38 +08:00
|
|
|
raise CommandError('"%s" is not a valid port number '
|
2010-11-26 21:33:53 +08:00
|
|
|
'or address:port pair.' % 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:
|
2013-05-17 22:33:36 +08:00
|
|
|
self.addr = '::1' if self.use_ipv6 else '127.0.0.1'
|
2011-01-27 11:15:12 +08:00
|
|
|
self._raw_ipv6 = bool(self.use_ipv6)
|
2010-11-14 02:41:55 +08:00
|
|
|
self.run(*args, **options)
|
2007-08-16 14:06:55 +08:00
|
|
|
|
2010-11-14 02:41:55 +08:00
|
|
|
def run(self, *args, **options):
|
|
|
|
"""
|
|
|
|
Runs the server, using the autoreloader if needed
|
|
|
|
"""
|
2012-03-11 01:50:59 +08:00
|
|
|
use_reloader = options.get('use_reloader')
|
2010-11-14 02:41:55 +08:00
|
|
|
|
|
|
|
if use_reloader:
|
|
|
|
autoreload.main(self.inner_run, args, options)
|
|
|
|
else:
|
|
|
|
self.inner_run(*args, **options)
|
|
|
|
|
|
|
|
def inner_run(self, *args, **options):
|
|
|
|
from django.conf import settings
|
|
|
|
from django.utils import translation
|
|
|
|
|
2012-03-11 01:50:59 +08:00
|
|
|
threading = options.get('use_threading')
|
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
|
|
|
|
2010-11-14 02:41:55 +08:00
|
|
|
self.stdout.write("Validating models...\n\n")
|
|
|
|
self.validate(display_num_errors=True)
|
|
|
|
self.stdout.write((
|
2012-09-10 19:35:21 +08:00
|
|
|
"%(started_at)s\n"
|
2010-11-14 02:41:55 +08:00
|
|
|
"Django version %(version)s, using settings %(settings)r\n"
|
2013-02-02 05:25:29 +08:00
|
|
|
"Starting development server at http://%(addr)s:%(port)s/\n"
|
2010-11-14 02:41:55 +08:00
|
|
|
"Quit the server with %(quit_command)s.\n"
|
|
|
|
) % {
|
2012-09-10 19:35:21 +08:00
|
|
|
"started_at": datetime.now().strftime('%B %d, %Y - %X'),
|
2010-11-14 02:41:55 +08:00
|
|
|
"version": self.get_version(),
|
|
|
|
"settings": settings.SETTINGS_MODULE,
|
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,
|
|
|
|
})
|
|
|
|
# django.core.management.base forces the locale to en-us. We should
|
|
|
|
# set it up correctly for the first request (particularly important
|
|
|
|
# in the "--noreload" case).
|
|
|
|
translation.activate(settings.LANGUAGE_CODE)
|
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,
|
|
|
|
ipv6=self.use_ipv6, threading=threading)
|
2013-09-22 21:55:09 +08:00
|
|
|
except socket.error 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.",
|
|
|
|
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:
|
2010-11-14 02:41:55 +08:00
|
|
|
error_text = str(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)
|
2008-08-31 04:17:58 +08:00
|
|
|
|
2010-11-14 02:41:55 +08:00
|
|
|
|
2012-04-09 05:13:32 +08:00
|
|
|
# Kept for backward compatibility
|
|
|
|
BaseRunserverCommand = Command
|