PageRenderTime 53ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

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

http://github.com/django/django
Python | 158 lines | 156 code | 2 blank | 0 comment | 2 complexity | 11ceafd828b500af490d0f7aa51d11e9 MD5 | raw file
Possible License(s): BSD-3-Clause, MIT
  1. import errno
  2. import os
  3. import re
  4. import socket
  5. import sys
  6. from datetime import datetime
  7. from django.conf import settings
  8. from django.core.management.base import BaseCommand, CommandError
  9. from django.core.servers.basehttp import (
  10. WSGIServer, get_internal_wsgi_application, run,
  11. )
  12. from django.utils import autoreload
  13. from django.utils.regex_helper import _lazy_re_compile
  14. naiveip_re = _lazy_re_compile(r"""^(?:
  15. (?P<addr>
  16. (?P<ipv4>\d{1,3}(?:\.\d{1,3}){3}) | # IPv4 address
  17. (?P<ipv6>\[[a-fA-F0-9:]+\]) | # IPv6 address
  18. (?P<fqdn>[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*) # FQDN
  19. ):)?(?P<port>\d+)$""", re.X)
  20. class Command(BaseCommand):
  21. help = "Starts a lightweight Web server for development."
  22. # Validation is called explicitly each time the server is reloaded.
  23. requires_system_checks = False
  24. stealth_options = ('shutdown_message',)
  25. default_addr = '127.0.0.1'
  26. default_addr_ipv6 = '::1'
  27. default_port = '8000'
  28. protocol = 'http'
  29. server_cls = WSGIServer
  30. def add_arguments(self, parser):
  31. parser.add_argument(
  32. 'addrport', nargs='?',
  33. help='Optional port number, or ipaddr:port'
  34. )
  35. parser.add_argument(
  36. '--ipv6', '-6', action='store_true', dest='use_ipv6',
  37. help='Tells Django to use an IPv6 address.',
  38. )
  39. parser.add_argument(
  40. '--nothreading', action='store_false', dest='use_threading',
  41. help='Tells Django to NOT use threading.',
  42. )
  43. parser.add_argument(
  44. '--noreload', action='store_false', dest='use_reloader',
  45. help='Tells Django to NOT use the auto-reloader.',
  46. )
  47. def execute(self, *args, **options):
  48. if options['no_color']:
  49. # We rely on the environment because it's currently the only
  50. # way to reach WSGIRequestHandler. This seems an acceptable
  51. # compromise considering `runserver` runs indefinitely.
  52. os.environ["DJANGO_COLORS"] = "nocolor"
  53. super().execute(*args, **options)
  54. def get_handler(self, *args, **options):
  55. """Return the default WSGI handler for the runner."""
  56. return get_internal_wsgi_application()
  57. def handle(self, *args, **options):
  58. if not settings.DEBUG and not settings.ALLOWED_HOSTS:
  59. raise CommandError('You must set settings.ALLOWED_HOSTS if DEBUG is False.')
  60. self.use_ipv6 = options['use_ipv6']
  61. if self.use_ipv6 and not socket.has_ipv6:
  62. raise CommandError('Your Python does not support IPv6.')
  63. self._raw_ipv6 = False
  64. if not options['addrport']:
  65. self.addr = ''
  66. self.port = self.default_port
  67. else:
  68. m = re.match(naiveip_re, options['addrport'])
  69. if m is None:
  70. raise CommandError('"%s" is not a valid port number '
  71. 'or address:port pair.' % options['addrport'])
  72. self.addr, _ipv4, _ipv6, _fqdn, self.port = m.groups()
  73. if not self.port.isdigit():
  74. raise CommandError("%r is not a valid port number." % self.port)
  75. if self.addr:
  76. if _ipv6:
  77. self.addr = self.addr[1:-1]
  78. self.use_ipv6 = True
  79. self._raw_ipv6 = True
  80. elif self.use_ipv6 and not _fqdn:
  81. raise CommandError('"%s" is not a valid IPv6 address.' % self.addr)
  82. if not self.addr:
  83. self.addr = self.default_addr_ipv6 if self.use_ipv6 else self.default_addr
  84. self._raw_ipv6 = self.use_ipv6
  85. self.run(**options)
  86. def run(self, **options):
  87. """Run the server, using the autoreloader if needed."""
  88. use_reloader = options['use_reloader']
  89. if use_reloader:
  90. autoreload.run_with_reloader(self.inner_run, **options)
  91. else:
  92. self.inner_run(None, **options)
  93. def inner_run(self, *args, **options):
  94. # If an exception was silenced in ManagementUtility.execute in order
  95. # to be raised in the child process, raise it now.
  96. autoreload.raise_last_exception()
  97. threading = options['use_threading']
  98. # 'shutdown_message' is a stealth option.
  99. shutdown_message = options.get('shutdown_message', '')
  100. quit_command = 'CTRL-BREAK' if sys.platform == 'win32' else 'CONTROL-C'
  101. self.stdout.write("Performing system checks...\n\n")
  102. self.check(display_num_errors=True)
  103. # Need to check migrations here, so can't use the
  104. # requires_migrations_check attribute.
  105. self.check_migrations()
  106. now = datetime.now().strftime('%B %d, %Y - %X')
  107. self.stdout.write(now)
  108. self.stdout.write((
  109. "Django version %(version)s, using settings %(settings)r\n"
  110. "Starting development server at %(protocol)s://%(addr)s:%(port)s/\n"
  111. "Quit the server with %(quit_command)s."
  112. ) % {
  113. "version": self.get_version(),
  114. "settings": settings.SETTINGS_MODULE,
  115. "protocol": self.protocol,
  116. "addr": '[%s]' % self.addr if self._raw_ipv6 else self.addr,
  117. "port": self.port,
  118. "quit_command": quit_command,
  119. })
  120. try:
  121. handler = self.get_handler(*args, **options)
  122. run(self.addr, int(self.port), handler,
  123. ipv6=self.use_ipv6, threading=threading, server_cls=self.server_cls)
  124. except OSError as e:
  125. # Use helpful error messages instead of ugly tracebacks.
  126. ERRORS = {
  127. errno.EACCES: "You don't have permission to access that port.",
  128. errno.EADDRINUSE: "That port is already in use.",
  129. errno.EADDRNOTAVAIL: "That IP address can't be assigned to.",
  130. }
  131. try:
  132. error_text = ERRORS[e.errno]
  133. except KeyError:
  134. error_text = e
  135. self.stderr.write("Error: %s" % error_text)
  136. # Need to use an OS exit because sys.exit doesn't work in a thread
  137. os._exit(1)
  138. except KeyboardInterrupt:
  139. if shutdown_message:
  140. self.stdout.write(shutdown_message)
  141. sys.exit(0)