Created
January 6, 2018 06:19
-
-
Save raminfp/c9387f09119f7f3d10db567be3a6e1d8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /usr/local/lib/python2.7/dist-packages/django/core/management/commands/runserver.py | |
| from __future__ import unicode_literals | |
| import errno | |
| import os | |
| import re | |
| import socket | |
| import sys | |
| from datetime import datetime | |
| from django.conf import settings | |
| from django.core.management.base import BaseCommand, CommandError | |
| from django.core.servers.basehttp import ( | |
| WSGIServer, get_internal_wsgi_application, run, | |
| ) | |
| from django.utils import autoreload, six | |
| from django.utils.encoding import force_text, get_system_encoding | |
| 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) | |
| class Command(BaseCommand): | |
| help = "Starts a lightweight Web server for development." | |
| # Validation is called explicitly each time the server is reloaded. | |
| requires_system_checks = False | |
| leave_locale_alone = True | |
| default_port = '8000' | |
| default_host = '127.0.0.1' | |
| protocol = 'http' | |
| server_cls = WSGIServer | |
| def add_arguments(self, parser): | |
| parser.add_argument( | |
| 'addrport', nargs='?', | |
| help='Optional port number, or ipaddr:port' | |
| ) | |
| parser.add_argument( | |
| '--ipv6', '-6', action='store_true', dest='use_ipv6', default=False, | |
| help='Tells Django to use an IPv6 address.', | |
| ) | |
| parser.add_argument( | |
| '--nothreading', action='store_false', dest='use_threading', default=True, | |
| help='Tells Django to NOT use threading.', | |
| ) | |
| parser.add_argument( | |
| '--noreload', action='store_false', dest='use_reloader', default=True, | |
| help='Tells Django to NOT use the auto-reloader.', | |
| ) | |
| def execute(self, *args, **options): | |
| if options['no_color']: | |
| # We rely on the environment because it's currently the only | |
| # way to reach WSGIRequestHandler. This seems an acceptable | |
| # compromise considering `runserver` runs indefinitely. | |
| os.environ[str("DJANGO_COLORS")] = str("nocolor") | |
| super(Command, self).execute(*args, **options) | |
| def get_handler(self, *args, **options): | |
| """ | |
| Returns the default WSGI handler for the runner. | |
| """ | |
| return get_internal_wsgi_application() | |
| def handle(self, *args, **options): | |
| 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.') | |
| self.use_ipv6 = options['use_ipv6'] | |
| if self.use_ipv6 and not socket.has_ipv6: | |
| raise CommandError('Your Python does not support IPv6.') | |
| self._raw_ipv6 = False | |
| if not options['addrport']: | |
| self.addr = '' | |
| self.port = self.default_port | |
| else: | |
| m = re.match(naiveip_re, options['addrport']) | |
| if m is None: | |
| raise CommandError('"%s" is not a valid port number ' | |
| 'or address:port pair.' % options['addrport']) | |
| self.addr, _ipv4, _ipv6, _fqdn, self.port = m.groups() | |
| if not self.port.isdigit(): | |
| raise CommandError("%r is not a valid port number." % self.port) | |
| if self.addr: | |
| if _ipv6: | |
| self.addr = self.addr[1:-1] | |
| self.use_ipv6 = True | |
| self._raw_ipv6 = True | |
| elif self.use_ipv6 and not _fqdn: | |
| raise CommandError('"%s" is not a valid IPv6 address.' % self.addr) | |
| if not self.addr: | |
| self.addr = '::1' if self.use_ipv6 else self.default_host | |
| #self.addr = self.default_host | |
| self._raw_ipv6 = self.use_ipv6 | |
| self.run(**options) | |
| def run(self, **options): | |
| """ | |
| Runs the server, using the autoreloader if needed | |
| """ | |
| use_reloader = options['use_reloader'] | |
| if use_reloader: | |
| autoreload.main(self.inner_run, None, options) | |
| else: | |
| self.inner_run(None, **options) | |
| def inner_run(self, *args, **options): | |
| # If an exception was silenced in ManagementUtility.execute in order | |
| # to be raised in the child process, raise it now. | |
| autoreload.raise_last_exception() | |
| threading = options['use_threading'] | |
| # 'shutdown_message' is a stealth option. | |
| shutdown_message = options.get('shutdown_message', '') | |
| quit_command = 'CTRL-BREAK' if sys.platform == 'win32' else 'CONTROL-C' | |
| self.stdout.write("Performing system checks...\n\n") | |
| self.check(display_num_errors=True) | |
| # Need to check migrations here, so can't use the | |
| # requires_migrations_check attribute. | |
| self.check_migrations() | |
| now = datetime.now().strftime('%B %d, %Y - %X') | |
| if six.PY2: | |
| now = now.decode(get_system_encoding()) | |
| self.stdout.write(now) | |
| self.stdout.write(( | |
| "Django version %(version)s, using settings %(settings)r\n" | |
| "Starting development server at %(protocol)s://%(addr)s:%(port)s/\n" | |
| "Quit the server with %(quit_command)s.\n" | |
| ) % { | |
| "version": self.get_version(), | |
| "settings": settings.SETTINGS_MODULE, | |
| "protocol": self.protocol, | |
| "addr": '[%s]' % self.addr if self._raw_ipv6 else self.addr, | |
| "port": self.port, | |
| "quit_command": quit_command, | |
| }) | |
| try: | |
| handler = self.get_handler(*args, **options) | |
| run(self.addr, int(self.port), handler, | |
| ipv6=self.use_ipv6, threading=threading, server_cls=self.server_cls) | |
| except socket.error as e: | |
| # Use helpful error messages instead of ugly tracebacks. | |
| ERRORS = { | |
| 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.", | |
| } | |
| try: | |
| error_text = ERRORS[e.errno] | |
| except KeyError: | |
| error_text = force_text(e) | |
| self.stderr.write("Error: %s" % error_text) | |
| # Need to use an OS exit because sys.exit doesn't work in a thread | |
| os._exit(1) | |
| except KeyboardInterrupt: | |
| if shutdown_message: | |
| self.stdout.write(shutdown_message) | |
| sys.exit(0) | |
| # Kept for backward compatibility | |
| BaseRunserverCommand = Command | |
| ############################################################################3 | |
| manage.py | |
| #!/usr/bin/env python | |
| import os | |
| import sys | |
| if __name__ == "__main__": | |
| os.environ.setdefault("DJANGO_SETTINGS_MODULE", "Seek.settings") | |
| try: | |
| from django.core.management import execute_from_command_line | |
| from django.core.management.commands.runserver import Command as runserver | |
| runserver.default_port = "8080" | |
| runserver.default_host = "192.168.131.2" | |
| except ImportError: | |
| # The above import may fail for some other reason. Ensure that the | |
| # issue is really that Django is missing to avoid masking other | |
| # exceptions on Python 2. | |
| try: | |
| import django | |
| except ImportError: | |
| raise ImportError( | |
| "Couldn't import Django. Are you sure it's installed and " | |
| "available on your PYTHONPATH environment variable? Did you " | |
| "forget to activate a virtual environment?" | |
| ) | |
| raise | |
| execute_from_command_line(sys.argv) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment