学了django有一段时间了,也没深入了解过它内部的实现,正好看到一篇介绍django运行原理的,有图有代码,深度好文,值得收藏。
-- xxmcf 2015.09.28 22:29
原文链接:
- Usage: manage.py runserver [options] [optional port number, or ipaddr:port]
- # python manager.py runserver # default port is 8000
- # python manager.py runserver 8080
- # python manager.py runserver 127.0.0.1:9090
- 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
- )
- <!-- 端口 -->
- <socket>:7600</socket>
- <stats>:40000</stats>
- <!-- 系统环境变量 -->
- <env>DJANGO_SETTINGS_MODULE=geek_blog.settings</env>
- <!-- 指定的python WSGI模块 -->
- <module>django.core.handlers.wsgi:WSGIHandler()</module>
- <processes>6</processes>
- <master />
- <master-as-root />
- <!-- 超时设置 -->
- <harakiri>60</harakiri>
- <harakiri-verbose/>
- <daemonize>/var/app/log/blog/uwsgi.log</daemonize>
- <!-- socket的监听队列大小 -->
- <listen>32768</listen>
- <!-- 内部超时时间 -->
- <socket-timeout>60</socket-timeout>
- </uwsgi>
- uwsgi --pidfile=/var/run/geek-blog.pid -x uwsgi.xml --uid blog --gid nogroup
- 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)
- # Sets the callable application as the WSGI application that will receive requests
- httpd.set_app(wsgi_handler)
- httpd.serve_forever()
- MIDDLEWARE_CLASSES = (
- ‘django.middleware.cache.UpdateCacheMiddleware‘,
- ‘django.middleware.common.CommonMiddleware‘,
- ‘django.middleware.cache.FetchFromCacheMiddleware‘,
- ‘django.contrib.sessions.middleware.SessionMiddleware‘,
- ‘django.middleware.csrf.CsrfViewMiddleware‘,
- ‘django.contrib.auth.middleware.AuthenticationMiddleware‘,
- ‘django.contrib.messages.middleware.MessageMiddleware‘,
- ‘django.middleware.locale.LocaleMiddleware‘,
- ‘geek_blog.middlewares.MobileDetectionMiddleware‘, # 自定义的Middleware
- )