Django搭建及源码分析(二)

上节针对linux最小系统,如何安装Django,以及配置简单的Django环境进行了说明。

本节从由Django生成的manage.py开始,分析Django源码。python版本2.6,Django版本1.6.11。

manage.py代码很简单。

#!/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实现的功能。

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

  其中的key是 DJANGO_SETTINGS_MODULE,这里的self是一个字典,判断key是不是在里面,不在里面就做一个键值绑定。然后返回。

  下面进入execute_from_command_line(sys.argv),sys.argv位一个参数执行的列表,像我们执行python manage.py runserver时,manage.py位sys.argv[0],runserver为sys.argv[1],以此类推,如果有更多参数加的话。

  进入django的core模块下,找到management模块。在__init__.py下可以找到该函数

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

  我们输入的参数便传到了这个函数里,其中ManagementUtility为一个类,传进去的参数列表会进入一个简单的初始化。

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
)
   在settings模块中并没有找到WSGI_APPLICATION这个环境变量,因此app_path为空,执行get_wsgi_application,在django/core下wsig.py中。
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
上一篇:Photon开发实战(2)——开发框架、第一个Photon程序


下一篇:Django如何启动源码分析