Django搭建及源碼分析(二)

上節針對linux最小系統,如何安裝Django,以及配置簡單的Django環境進行了說明。php

本節從由Django生成的manage.py開始,分析Django源碼。python版本2.6,Django版本1.6.11。html

manage.py代碼很簡單。python

#!/usr/bin/env python
import os
import sys

if __name__ == "__main__":
    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "MyDjProj.settings")

    from django.core.management import execute_from_command_line

    execute_from_command_line(sys.argv)

  首先看os.environ.setdefault(),environ爲類_Environ的一個實例,它繼承自IterableUserDict,而IterableUserDict繼承自UserDict,包括setdefault()這個方法也是UserDict的一個方法,看一下 setdefault實現的功能。linux

    def setdefault(self, key, failobj=None):
        if key not in self:
            self[key] = failobj
        return self[key]

  其中的key是 DJANGO_SETTINGS_MODULE,這裏的self是一個字典,判斷key是否是在裏面,不在裏面就作一個鍵值綁定。而後返回。git

  下面進入execute_from_command_line(sys.argv),sys.argv位一個參數執行的列表,像咱們執行python manage.py runserver時,manage.py位sys.argv[0],runserver爲sys.argv[1],以此類推,若是有更多參數加的話。sql

  進入django的core模塊下,找到management模塊。在__init__.py下能夠找到該函數django

def execute_from_command_line(argv=None):
    """
    A simple method that runs a ManagementUtility.
    """
    utility = ManagementUtility(argv)
    utility.execute()

  咱們輸入的參數便傳到了這個函數裏,其中ManagementUtility爲一個類,傳進去的參數列表會進入一個簡單的初始化。cookie

class ManagementUtility(object):
    """
    Encapsulates the logic of the django-admin.py and manage.py utilities.

    A ManagementUtility has a number of commands, which can be manipulated
    by editing the self.commands dictionary.
    """
    def __init__(self, argv=None):
        self.argv = argv or sys.argv[:]
        self.prog_name = os.path.basename(self.argv[0])

  將參數列表賦給argv,而後返回sys.argv[0]的文件名字給prog_name。實例化完了以後,再看utility.execute()即類ManagementUtility的execute()方法。 網絡

    def execute(self):
        """
        Given the command-line arguments, this figures out which subcommand is
        being run, creates a parser appropriate to that command, and runs it.
        """
        # Preprocess options to extract --settings and --pythonpath.
        # These options could affect the commands that are available, so they
        # must be processed early.
        parser = LaxOptionParser(usage="%prog subcommand [options] [args]",
                                 version=get_version(),
                                 option_list=BaseCommand.option_list)
        self.autocomplete()
        try:
            options, args = parser.parse_args(self.argv)
            handle_default_options(options)
        except:
            pass # Ignore any option errors at this point.

        try:
            subcommand = self.argv[1]
        except IndexError:
            subcommand = 'help' # Display help if no arguments were given.

        if subcommand == 'help':
            if len(args) <= 2:
                parser.print_lax_help()
                sys.stdout.write(self.main_help_text() + '\n')
            elif args[2] == '--commands':
                sys.stdout.write(self.main_help_text(commands_only=True) + '\n')
            else:
                self.fetch_command(args[2]).print_help(self.prog_name, args[2])
        elif subcommand == 'version':
            sys.stdout.write(parser.get_version() + '\n')
        # Special-cases: We want 'django-admin.py --version' and
        # 'django-admin.py --help' to work, for backwards compatibility.
        elif self.argv[1:] == ['--version']:
            # LaxOptionParser already takes care of printing the version.
            pass
        elif self.argv[1:] in (['--help'], ['-h']):
            parser.print_lax_help()
            sys.stdout.write(self.main_help_text() + '\n')
        else:
            self.fetch_command(subcommand).run_from_argv(self.argv)

  第一個語句爲詞法分析,LaxOptionParser這個類繼承自OptionParser這個類,它來自於optparse模塊,在OptionParser模塊裏有一個init()函數,會接受若干個參數,進行初始化,這裏只是傳遞了3個參數,usage傳遞了一個字符串,version爲版本,BaseCommand.option_list爲一個關於命令行的元組,其中調用了make_option這個類init進行一些初始化,主要初始化_short_opts和_long_opts這兩個參數,而且進行了一些檢查。_short_opts爲短命令,_long_opts爲長命令,如-h,--help分別爲短命令和長命令。多線程

  parser爲LaxOptionParser的一個實例。 parser.parse_args(self.argv)這個函數會 根據上面初始化的內容進行解析命令行參數,以後返回兩個值:options,它是一個對象,保存有命令行參數值。只要知道命令行參數名,如 file,就能夠訪問其對應的值: options.file 。args,它是一個由 positional arguments 組成的列表。

  執行handle_default_options在django.core.management.base中(management/__init__.py前有導入,from django.core.management.base import BaseCommand, CommandError, handle_default_options)

def handle_default_options(options):
    """
    Include any default options that all commands should accept here
    so that ManagementUtility can handle them before searching for
    user commands.

    """
    if options.settings:
        os.environ['DJANGO_SETTINGS_MODULE'] = options.settings
    if options.pythonpath:
        sys.path.insert(0, options.pythonpath)

  handle_default_options將解析出來的options對象當作參數,判斷settings和python path是否存在,而後設置環境變量和python模塊的搜索路徑。接下來是得到命令行的命令參數self.argv[1],並判斷這個命令,包括錯誤處理,是否時help,是不是version,根據不一樣的狀況展現不一樣的信息。

最重要的是最後一句,即前面的狀況都不是,就進入self.fetch_command(subcommand).run_from_argv(self.argv)

    def fetch_command(self, subcommand):
        """
        Tries to fetch the given subcommand, printing a message with the
        appropriate command called from the command line (usually
        "django-admin.py" or "manage.py") if it can't be found.
        """
        # Get commands outside of try block to prevent swallowing exceptions
        commands = get_commands()
        try:
            app_name = commands[subcommand]
        except KeyError:
            sys.stderr.write("Unknown command: %r\nType '%s help' for usage.\n" % \
                (subcommand, self.prog_name))
            sys.exit(1)
        if isinstance(app_name, BaseCommand):
            # If the command is already loaded, use it directly.
            klass = app_name
        else:
            klass = load_command_class(app_name, subcommand)
        return klass

  得到django/core/management/commands目錄下與INSTALLED_APPS/management/commands目錄下的子命令對應的模塊前綴,執行load_command_class

def load_command_class(app_name, name):
    """
    Given a command name and an application name, returns the Command
    class instance. All errors raised by the import process
    (ImportError, AttributeError) are allowed to propagate.
    """
    module = import_module('%s.management.commands.%s' % (app_name, name))
    return module.Command()

  返回Command類的實例。進入management/commands下進入每一個文件會發現,每一個都有Command類對應相應的命令。

  self.fetch_command(subcommand).run_from_argv(self.argv)找到咱們輸入的命令參數,而且使用run_from_argv執行。

  runserver命令爲例,進入runserver這個命令下看一下,Command這個類繼承了BaseCommand這個類,BaseCommand在Base.py中,裏面有run_from_arv這個方法。

    def run_from_argv(self, argv):
        """
        Set up any environment changes requested (e.g., Python path
        and Django settings), then run this command. If the
        command raises a ``CommandError``, intercept it and print it sensibly
        to stderr. If the ``--traceback`` option is present or the raised
        ``Exception`` is not ``CommandError``, raise it.
        """
    """
    如下爲上該函數中文說明:
    #設置環境變量,而後運行這個命令    
    #如pythonmanage.pyrunserver,manage.py就是argv[0],runserver是argv[1]
    #create_parser直接調用OptionParser(prog=prog_name,usage=self.usage(subcommand),version=self.get_version(),option_list=self.option_list)
            #將manage.py 和runserver加入參數列表
    #接下來用parser.parse_args進行解析,argv[2]以後爲默認參數,ip地址還有端口號,咱們也能夠顯示的傳進去,默認是127.0.0.1,端口號8000.
    #下面又到了handle_default_options函數。
    #接下來是執行execute函數,還有異常捕獲。
    """
        parser = self.create_parser(argv[0], argv[1])
        options, args = parser.parse_args(argv[2:])
        handle_default_options(options)
        try:
            self.execute(*args, **options.__dict__)
        except Exception as e:
            if options.traceback or not isinstance(e, CommandError):
                raise

            # self.stderr is not guaranteed to be set here
            stderr = getattr(self, 'stderr', OutputWrapper(sys.stderr, self.style.ERROR))
            stderr.write('%s: %s' % (e.__class__.__name__, e))
            sys.exit(1)                    
執行self.execute,該類的execute方法。
def execute(self, *args, **options):
        """
        Try to execute this command, performing model validation if
        needed (as controlled by the attribute
        ``self.requires_model_validation``, except if force-skipped).
        """
        self.stdout = OutputWrapper(options.get('stdout', sys.stdout))
        self.stderr = OutputWrapper(options.get('stderr', sys.stderr), self.style.ERROR)

        if self.can_import_settings:
            from django.conf import settings

        saved_locale = None
        if not self.leave_locale_alone:
            # Only mess with locales if we can assume we have a working
            # settings file, because django.utils.translation requires settings
            # (The final saying about whether the i18n machinery is active will be
            # found in the value of the USE_I18N setting)
            if not self.can_import_settings:
                raise CommandError("Incompatible values of 'leave_locale_alone' "
                                   "(%s) and 'can_import_settings' (%s) command "
                                   "options." % (self.leave_locale_alone,
                                                 self.can_import_settings))
            # Switch to US English, because django-admin.py creates database
            # content like permissions, and those shouldn't contain any
            # translations.
            from django.utils import translation
            saved_locale = translation.get_language()
            translation.activate('en-us')

        try:
            if self.requires_model_validation and not options.get('skip_validation'):
                self.validate()
            output = self.handle(*args, **options)
            if output:
                if self.output_transaction:
                    # This needs to be imported here, because it relies on
                    # settings.
                    from django.db import connections, DEFAULT_DB_ALIAS
                    connection = connections[options.get('database', DEFAULT_DB_ALIAS)]
                    if connection.ops.start_transaction_sql():
                        self.stdout.write(self.style.SQL_KEYWORD(connection.ops.start_transaction_sql()))
                self.stdout.write(output)
                if self.output_transaction:
                    self.stdout.write('\n' + self.style.SQL_KEYWORD("COMMIT;"))
        finally:
            if saved_locale is not None:
                translation.activate(saved_locale)

  前邊是一些設置和錯誤檢查,最主要的是 output = self.handle(*args, **options),能夠發現handle在BaseCommand裏是空的,每一個命令對其進行了重寫。runserver也是。看看runserver的handle:

 
   def handle(self, addrport='', *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.get('use_ipv6')
        if self.use_ipv6 and not socket.has_ipv6:
            raise CommandError('Your Python does not support IPv6.')
        if args:
            raise CommandError('Usage is runserver %s' % self.args)
        self._raw_ipv6 = False
        if not addrport:
            self.addr = ''
            self.port = DEFAULT_PORT
        else:        
       #若是設置了IP地址,用正則匹配出來 m
= re.match(naiveip_re, addrport) if m is None: raise CommandError('"%s" is not a valid port number ' 'or address:port pair.' % 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:
       #若是沒有設置ip地址用127.0.0.1代替。 self.addr
= '::1' if self.use_ipv6 else '127.0.0.1' self._raw_ipv6 = bool(self.use_ipv6)
#運行命令 self.run(
*args, **options)
 

runserver裏的run方法主要時調用了inner_run(*args, **options)這個方法。

   def run(self, *args, **options):
        """
        Runs the server, using the autoreloader if needed
        """
        use_reloader = options.get('use_reloader')

        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

        threading = options.get('use_threading')
        shutdown_message = options.get('shutdown_message', '')
        quit_command = 'CTRL-BREAK' if sys.platform == 'win32' else 'CONTROL-C'

        self.stdout.write("Validating models...\n\n")
        self.validate(display_num_errors=True)
        now = datetime.now().strftime('%B %d, %Y - %X')
        if six.PY2:
            now = now.decode(get_system_encoding())
    
    #這裏是開始運行時,打印到終端的信息 self.stdout.write((
"%(started_at)s\n" "Django version %(version)s, using settings %(settings)r\n" "Starting development server at http://%(addr)s:%(port)s/\n" "Quit the server with %(quit_command)s.\n" ) % { "started_at": now, "version": self.get_version(), "settings": settings.SETTINGS_MODULE, "addr": '[%s]' % self.addr if self._raw_ipv6 else self.addr, "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) try: handler = self.get_handler(*args, **options) run(self.addr, int(self.port), handler, ipv6=self.use_ipv6, threading=threading) 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 = str(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)
關鍵是:handler = self.get_handler(*args, **options)和run(self.addr, int(self.port), handler,
get_handle是self.因此是該類的方法。run不是本類中定義的方法,而由runserver.py開頭定義from django.core.servers.basehttp import run, get_internal_wsgi_application
    def get_handler(self, *args, **options):
        """
        Returns the default WSGI handler for the runner.
        """
        return get_internal_wsgi_application()

get_internal_wsgi_application()和run(self.addr, int(self.port), handler,ipv6=self.use_ipv6, threading=threading)都在django.core.servers.basehttp中。

def get_internal_wsgi_application():
    """
    Loads and returns the WSGI application as configured by the user in
    ``settings.WSGI_APPLICATION``. With the default ``startproject`` layout,
    this will be the ``application`` object in ``projectname/wsgi.py``.

    This function, and the ``WSGI_APPLICATION`` setting itself, are only useful
    for Django's internal servers (runserver, runfcgi); external WSGI servers
    should just be configured to point to the correct application object
    directly.

    If settings.WSGI_APPLICATION is not set (is ``None``), we just return
    whatever ``django.core.wsgi.get_wsgi_application`` returns.

    """
    from django.conf import settings
    app_path = getattr(settings, 'WSGI_APPLICATION')
    if app_path is None:
        return get_wsgi_application()

    return import_by_path(
        app_path,
        error_prefix="WSGI application '%s' could not be loaded; " % app_path
    )
get_wsgi_application,在django/core下wsig.py中。  在settings模塊中並無找到WSGI_APPLICATION這個環境變量,所以app_path爲空,執行
def get_wsgi_application():
    """
    The public interface to Django's WSGI support. Should return a WSGI
    callable.

    Allows us to avoid making django.core.handlers.WSGIHandler public API, in
    case the internal WSGI implementation changes or moves in the future.

    """
    return WSGIHandler()

  返回一個WSGIHandler實例。

class WSGIHandler(base.BaseHandler):
    initLock = Lock()
    request_class = WSGIRequest    #WSGIRequest實現了wsgi規範

  下面進入run方法:run(self.addr, int(self.port), handler,ipv6=self.use_ipv6, threading=threading),標準的wsgi實現:

def run(addr, port, wsgi_handler, ipv6=False, threading=False):
    server_address = (addr, port)
    if threading:
        httpd_cls = type(str('WSGIServer'), (socketserver.ThreadingMixIn, WSGIServer), {})
    else:
        httpd_cls = WSGIServer
    httpd = httpd_cls(server_address, WSGIRequestHandler, ipv6=ipv6)
    httpd.set_app(wsgi_handler)
    httpd.serve_forever()

  httpd_cls(server_address, WSGIRequestHandler, ipv6=ipv6)爲實例化一個WSGIServer類,最終的實例化方法在父類SocketServer中的TCPServer和 BaseServer中。包括初始化線程,初始化網絡句柄,像下面的__is_shut_down都是在其中初始化的。

  SocketServer.py中的BaseServer中serve_forever()的定義。

    def serve_forever(self, poll_interval=0.5):
        """Handle one request at a time until shutdown.

        Polls for shutdown every poll_interval seconds. Ignores
        self.timeout. If you need to do periodic tasks, do them in
        another thread.
        """
        self.__serving = True
        #__is_shut_down爲一個初始化的threading.Event()的句柄,用於線程間通訊        
        #.clear()將標識設置爲false
        self.__is_shut_down.clear()
        while self.__serving:
            # XXX: Consider using another file descriptor or
            # connecting to the socket to wake this up instead of
            # polling. Polling reduces our responsiveness to a
            # shutdown request and wastes cpu at all other times.
            #下面的函數就是一個封裝好了的select函數,後面的四個爲傳遞給select函數的參數,分別表示輸入對象列表,輸出對象列表,錯誤對象列表以及超時時間。                
            #返回值爲三個列表,表明可寫,可讀,錯誤的事件的對象列表。                
            r, w, e = select.select([self], [], [], poll_interval)
            #判斷self事件是否在可讀對象列表中,在就進入進行處理
            if r:
                #處理鏈接請求
                self._handle_request_noblock()
        self.__is_shut_down.set()

  調用本類中的_handle_request_noblock()

    def _handle_request_noblock(self):
        """Handle one request, without blocking.

        I assume that select.select has returned that the socket is
        readable before this function was called, so there should be
        no risk of blocking in get_request().
        """
        try:
            #返回請求句柄,客戶端地址,get_request()中調用了self.socket.accept()來實現客戶端的鏈接
            request, client_address = self.get_request()
        except socket.error:
            return
        if self.verify_request(request, client_address):
            try:
                #真正的處理鏈接請求的地方
                self.process_request(request, client_address)
            except:
                self.handle_error(request, client_address)
                self.close_request(request)

  在self.process_request中執行了self.finish_request(request, client_address)和self.close_request(request)。

    def finish_request(self, request, client_address):
        """Finish one request by instantiating RequestHandlerClass."""
        self.RequestHandlerClass(request, client_address, self)

  此處的RequestHandlerClass爲初始化的時候傳進來的WSGIRequestHandler類,實現了回調。回到httpd = httpd_cls(server_address, WSGIRequestHandler, ipv6=ipv6),看WSGIRequestHandler類的__init__函數。 

class WSGIRequestHandler(simple_server.WSGIRequestHandler, object):

    def __init__(self, *args, **kwargs):
        from django.conf import settings
        self.admin_static_prefix = urljoin(settings.STATIC_URL, 'admin/')
        # We set self.path to avoid crashes in log_message() on unsupported
        # requests (like "OPTIONS").
        self.path = ''
        self.style = color_style()
        super(WSGIRequestHandler, self).__init__(*args, **kwargs)

調用父類的WSGIRequestHandler進行初始化,它的父類是simple_server.py裏的WSGIRequestHandler,它裏面沒有__init__,繼續找它的父類 #如此重複,在最終的基類中能夠找到__init__方法,位於SocketServer.py中的 BaseRequestHandler類。

class BaseRequestHandler:


    def __init__(self, request, client_address, server):
        self.request = request
        self.client_address = client_address
        self.server = server
        try:
            self.setup()
            self.handle()
            self.finish()
        finally:
            sys.exc_traceback = None    # Help garbage collection

    def setup(self):
        pass

    def handle(self):
        pass

    def finish(self):
        pass

能夠看到裏面的方法並無實現,只是給出了定義,實現都在繼承該類的子類中。再回到WSGIRequestHandler類中,在simple_server.py中實現了handle函數,實現對請求的處理。

    def handle(self):
        """Handle a single HTTP request"""

        self.raw_requestline = self.rfile.readline()
        if not self.parse_request(): # An error code has been sent, just exit
            return
        #傳入的參數,讀,寫,錯誤,環境變量。在其父類SimpleHandler中進行了初始化,而且打開了多線程和多進程選項
        handler = ServerHandler(
            self.rfile, self.wfile, self.get_stderr(), self.get_environ()
        )
        handler.request_handler = self      # backpointer for logging 
        #在SimpleHandler的父類BaseHandler中含實現了run方法。此處get_app()是上面的run方法中咱們傳進去的wsgi_handler,httpd.set_app(wsgi_handler)    
        handler.run(self.server.get_app())

handlers.py中BaseHandler中run的定義。

    def run(self, application):
        """Invoke the application"""
        # Note to self: don't move the close()!  Asynchronous servers shouldn't
        # call close() from finish_response(), so if you close() anywhere but
        # the double-error branch here, you'll break asynchronous servers by
        # prematurely closing.  Async servers must return from 'run()' without
        # closing if there might still be output to iterate over.
        try:
              #設置環境變量    
            self.setup_environ()
            #執行WSGIHandler(self.environ,self.start_response)
            #由於類中實現了__call__方法,調用類的實例就至關於調用了__call__方法            
            self.result = application(self.environ, self.start_response)
            self.finish_response()
        except:
            try:
                self.handle_error()
            except:
                # If we get an error handling an error, just give up already!
                self.close()
                raise   # ...and let the actual server figure it out.

  執行WSGIHandler類的__call__方法。

class WSGIHandler(base.BaseHandler):
    initLock = Lock()
    request_class = WSGIRequest

    def __call__(self, environ, start_response):
        # Set up middleware if needed. We couldn't do this earlier, because
        # settings weren't available.
        if self._request_middleware is None:
            with self.initLock:
                #嘗試加載中間件
                try:
                    # Check that middleware is still uninitialised.
                    if self._request_middleware is None:
                        self.load_middleware()
                except:
                    # Unload whatever middleware we got
                    self._request_middleware = None
                    raise

        set_script_prefix(base.get_script_name(environ))
        signals.request_started.send(sender=self.__class__)
        try:
            request = self.request_class(environ)
        except UnicodeDecodeError:
            logger.warning('Bad Request (UnicodeDecodeError)',
                exc_info=sys.exc_info(),
                extra={
                    'status_code': 400,
                }
            )
            response = http.HttpResponseBadRequest()
        else:
            response = self.get_response(request)

        response._handler_class = self.__class__

        status = '%s %s' % (response.status_code, response.reason_phrase)
        response_headers = [(str(k), str(v)) for k, v in response.items()]
        for c in response.cookies.values():
            response_headers.append((str('Set-Cookie'), str(c.output(header=''))))
        start_response(force_str(status), response_headers)
        return response
相關文章
相關標籤/搜索