PageRenderTime 43ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://github.com/insane/django
Python | 141 lines | 139 code | 2 blank | 0 comment | 2 complexity | 9f614cb8aee0d819d68078f289ec22df MD5 | raw file
Possible License(s): BSD-3-Clause
  1. from optparse import make_option
  2. from datetime import datetime
  3. import os
  4. import re
  5. import sys
  6. import socket
  7. from django.core.management.base import BaseCommand, CommandError
  8. from django.core.servers.basehttp import run, WSGIServerException, get_internal_wsgi_application
  9. from django.utils import autoreload
  10. naiveip_re = re.compile(r"""^(?:
  11. (?P<addr>
  12. (?P<ipv4>\d{1,3}(?:\.\d{1,3}){3}) | # IPv4 address
  13. (?P<ipv6>\[[a-fA-F0-9:]+\]) | # IPv6 address
  14. (?P<fqdn>[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*) # FQDN
  15. ):)?(?P<port>\d+)$""", re.X)
  16. DEFAULT_PORT = "8000"
  17. class Command(BaseCommand):
  18. option_list = BaseCommand.option_list + (
  19. make_option('--ipv6', '-6', action='store_true', dest='use_ipv6', default=False,
  20. help='Tells Django to use a IPv6 address.'),
  21. make_option('--nothreading', action='store_false', dest='use_threading', default=True,
  22. help='Tells Django to NOT use threading.'),
  23. make_option('--noreload', action='store_false', dest='use_reloader', default=True,
  24. help='Tells Django to NOT use the auto-reloader.'),
  25. )
  26. help = "Starts a lightweight Web server for development."
  27. args = '[optional port number, or ipaddr:port]'
  28. # Validation is called explicitly each time the server is reloaded.
  29. requires_model_validation = False
  30. def get_handler(self, *args, **options):
  31. """
  32. Returns the default WSGI handler for the runner.
  33. """
  34. return get_internal_wsgi_application()
  35. def handle(self, addrport='', *args, **options):
  36. from django.conf import settings
  37. if not settings.DEBUG and not settings.ALLOWED_HOSTS:
  38. raise CommandError('You must set settings.ALLOWED_HOSTS if DEBUG is False.')
  39. self.use_ipv6 = options.get('use_ipv6')
  40. if self.use_ipv6 and not socket.has_ipv6:
  41. raise CommandError('Your Python does not support IPv6.')
  42. if args:
  43. raise CommandError('Usage is runserver %s' % self.args)
  44. self._raw_ipv6 = False
  45. if not addrport:
  46. self.addr = ''
  47. self.port = DEFAULT_PORT
  48. else:
  49. m = re.match(naiveip_re, addrport)
  50. if m is None:
  51. raise CommandError('"%s" is not a valid port number '
  52. 'or address:port pair.' % addrport)
  53. self.addr, _ipv4, _ipv6, _fqdn, self.port = m.groups()
  54. if not self.port.isdigit():
  55. raise CommandError("%r is not a valid port number." % self.port)
  56. if self.addr:
  57. if _ipv6:
  58. self.addr = self.addr[1:-1]
  59. self.use_ipv6 = True
  60. self._raw_ipv6 = True
  61. elif self.use_ipv6 and not _fqdn:
  62. raise CommandError('"%s" is not a valid IPv6 address.' % self.addr)
  63. if not self.addr:
  64. self.addr = '::1' if self.use_ipv6 else '127.0.0.1'
  65. self._raw_ipv6 = bool(self.use_ipv6)
  66. self.run(*args, **options)
  67. def run(self, *args, **options):
  68. """
  69. Runs the server, using the autoreloader if needed
  70. """
  71. use_reloader = options.get('use_reloader')
  72. if use_reloader:
  73. autoreload.main(self.inner_run, args, options)
  74. else:
  75. self.inner_run(*args, **options)
  76. def inner_run(self, *args, **options):
  77. from django.conf import settings
  78. from django.utils import translation
  79. threading = options.get('use_threading')
  80. shutdown_message = options.get('shutdown_message', '')
  81. quit_command = 'CTRL-BREAK' if sys.platform == 'win32' else 'CONTROL-C'
  82. self.stdout.write("Validating models...\n\n")
  83. self.validate(display_num_errors=True)
  84. self.stdout.write((
  85. "%(started_at)s\n"
  86. "Django version %(version)s, using settings %(settings)r\n"
  87. "Starting development server at http://%(addr)s:%(port)s/\n"
  88. "Quit the server with %(quit_command)s.\n"
  89. ) % {
  90. "started_at": datetime.now().strftime('%B %d, %Y - %X'),
  91. "version": self.get_version(),
  92. "settings": settings.SETTINGS_MODULE,
  93. "addr": '[%s]' % self.addr if self._raw_ipv6 else self.addr,
  94. "port": self.port,
  95. "quit_command": quit_command,
  96. })
  97. # django.core.management.base forces the locale to en-us. We should
  98. # set it up correctly for the first request (particularly important
  99. # in the "--noreload" case).
  100. translation.activate(settings.LANGUAGE_CODE)
  101. try:
  102. handler = self.get_handler(*args, **options)
  103. run(self.addr, int(self.port), handler,
  104. ipv6=self.use_ipv6, threading=threading)
  105. except WSGIServerException as e:
  106. # Use helpful error messages instead of ugly tracebacks.
  107. ERRORS = {
  108. 13: "You don't have permission to access that port.",
  109. 98: "That port is already in use.",
  110. 99: "That IP address can't be assigned-to.",
  111. }
  112. try:
  113. error_text = ERRORS[e.args[0].args[0]]
  114. except (AttributeError, KeyError):
  115. error_text = str(e)
  116. self.stderr.write("Error: %s" % error_text)
  117. # Need to use an OS exit because sys.exit doesn't work in a thread
  118. os._exit(1)
  119. except KeyboardInterrupt:
  120. if shutdown_message:
  121. self.stdout.write(shutdown_message)
  122. sys.exit(0)
  123. # Kept for backward compatibility
  124. BaseRunserverCommand = Command