python manage.py runserver流程解析


Posted in Python onNovember 08, 2019

这篇文章主要介绍了python manage.py runserver流程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

版本

  • python27
  • django 1.0

搭建可运行的环境

创建python27 虚拟环境

github 下载 django-1.0.tar.gz(1.0 版本的django)

解压

可以看到,有个 demo 在 examples 目录

把 django 目录拷贝到 examples 下面,这样 example 可以正确导入 django1.0

启动项目

python manage.py runserver

项目启动成功,可以修改代码来跟踪执行流程

流程

以下代码存在删减,主要展示代码流程

从 manage.py 开始,执行了 execute_manager 方法,传入 settings 模块

execute_manager(settings)

django.core.management.execute_manager 方法

def execute_manager(settings_mod, argv=None):
  # setup_environ 函数,只是设置了环境变量,执行配置模块
  # os.environ['DJANGO_SETTINGS_MODULE'] = examples.settting
  setup_environ(settings_mod)

  # admin manage 工具类
  utility = ManagementUtility(argv)
  utility.execute()

ManagementUtility 类

class ManagementUtility(object):
  def __init__(self, argv=None):
    # 初始化,例如
    self.argv = ['.../examples/manage.py', 'runserver']
    self.prog_name = 'manage.py'
  def execute(self):
    # 删除了部分代码,最终执行代码大致如下
  
    # 这是一个命令行工具类,表名能接受什么样的参数,这里主要检查两个参数
    # --settings 指定配置文件
    # --pythonpath 执行 python 环境变量
    parser = LaxOptionParser(usage="%prog subcommand [options] [args]",
                 version=get_version(),
                 option_list=BaseCommand.option_list)
  
    # 使用命令行工具类解析命令行参数,也就是获取 --settings 和 --pythonpath 的参数值
    options, args = parser.parse_args(self.argv)
    # 如果 --settings 参数存在,会覆盖之前设置的 os.environ['DJANGO_SETTINGS_MODULE']
    # 如果 --pythonpath 参数存在,会把指定路径添加到 sys.path 的第一位,优先从此处加载模块
    handle_default_options(options)
  
    # fetch_command
    # fetch_command 分析在下边
    # fetch_command 返回 django.core.management.commands.runserver.Command
    # run_from_argv
    # run_from_argv 分析在下边
    self.fetch_command(subcommand).run_from_argv(self.argv)
  
  def fetch_command(self, subcommand):
    # get_commands
    # get_commands 返回 django.core.management.commands 目录下的所有模块,每个模块处理对应的参数
    # 每个模块的值都是 django.core,app_name = 'django.core'
    app_name = get_commands()[subcommand]
  
    # load_command_class 方法
    # 返回了 django.core.management.commands.runserver.Command
    klass = load_command_class(app_name, subcommand)
  
    return klass

run_from_argv 方法

# django.core.management.commands.runserver.Command 
# 继承 django.core.management.base import BaseCommand
# run_from_argv 也是继承的
def run_from_argv(self, argv):
  # 调用 execute
  self.execute(*args, **options.__dict__)

def execute(self, *args, **options):
  # 调用 handle
  # 注意 handle 被重写了
  # 调用的是 django.core.management.commands.runserver.Command.handle
  output = self.handle(*args, **options)

handle

def handle(self, addrport='', *args, **options):
  def inner_run():
    # WSGI 处理程序
    # WSGIHandler 可调用,是 WSGI 处理程序
    # AdminMediaHandler 是对 WSGIHandler 的封装
    # AdminMediaHandler 特殊处理媒体文件请求
    # AdminMediaHandler 非媒体文件的 HTTP 请求,直接返回 WSGIHandler
    handler = AdminMediaHandler(WSGIHandler(), path)
    
    # 
    run(addr, int(port), handler)
    # run 在 django.core.servers.basehttp.run
    # run 定义如下
    # run 启动了 HTTP 服务,这个服务器只能用于开发调试
    def run(addr, port, wsgi_handler):
      # 绑定地址端口
      server_address = (addr, port)
      # 服务实例
      httpd = WSGIServer(server_address, WSGIRequestHandler)
      # 传入 WSGI 处理程序
      httpd.set_app(wsgi_handler)
      # 监听请求
      httpd.serve_forever()
  
  inner_run()

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
python 日期操作类代码
May 05 Python
python求最大值最小值方法总结
Jun 25 Python
pyqt5 使用cv2 显示图片,摄像头的实例
Jun 27 Python
python程序中的线程操作 concurrent模块使用详解
Sep 23 Python
python分别打包出32位和64位应用程序
Feb 18 Python
Python龙贝格法求积分实例
Feb 29 Python
python简单实现最大似然估计&scipy库的使用详解
Apr 15 Python
python 实现IP子网计算
Feb 18 Python
Matplotlib绘制条形图的方法你知道吗
Mar 21 Python
python中mongodb包操作数据库
Apr 19 Python
python和anaconda的区别
May 06 Python
python数据分析之单因素分析线性拟合及地理编码
Jun 25 Python
详解python中docx库的安装过程
Nov 08 #Python
numpy.array 操作使用简单总结
Nov 08 #Python
如何在python中写hive脚本
Nov 08 #Python
Python 依赖库太多了该如何管理
Nov 08 #Python
python+OpenCV实现车牌号码识别
Nov 08 #Python
python实现飞机大战小游戏
Nov 08 #Python
python 基于dlib库的人脸检测的实现
Nov 08 #Python
You might like
深入php list()函数的详解
2013/06/05 PHP
php遍历类中包含的所有元素的方法
2015/05/12 PHP
PHP使用逆波兰式计算工资的方法
2015/07/29 PHP
大家须知简单的php性能优化注意点
2016/01/04 PHP
PHP通过文件保存和更新信息的方法分析
2019/09/12 PHP
ThinkPHP 5 AJAX跨域请求头设置实现过程解析
2020/10/28 PHP
用javascript实现画板的代码
2007/09/05 Javascript
javascript中的一些注意事项 更新中
2010/12/06 Javascript
jquery 插件学习(五)
2012/08/06 Javascript
中文路径导致unitpngfix.js不正常的解决方法
2013/06/26 Javascript
基于JavaScript获取鼠标位置的各种方法
2015/12/16 Javascript
js实现滚动条滚动到某个位置便自动定位某个tr
2021/01/20 Javascript
JavaScript实现in-place思想的快速排序方法
2016/08/07 Javascript
浅析jQuery操作select控件的取值和设值
2016/12/07 Javascript
JavaScript脚本语言是什么_动力节点Java学院整理
2017/06/26 Javascript
详解Vue.js中.native修饰符
2018/04/24 Javascript
Webpack打包字体font-awesome的方法示例
2018/04/26 Javascript
JS实现生成由字母与数字组合的随机字符串功能详解
2018/05/25 Javascript
详解webpack打包后如何调试的方法步骤
2018/11/07 Javascript
在Vant的基础上封装下拉日期控件的代码示例
2018/12/05 Javascript
Vue 样式绑定的实现方法
2019/01/15 Javascript
使用Angular material主题定义自己的组件库的配色体系
2019/09/04 Javascript
vue如何在用户要关闭当前网页时弹出提示的实现
2020/05/31 Javascript
python多重继承实例
2014/10/11 Python
在Python 3中实现类型检查器的简单方法
2015/07/03 Python
利用python代码写的12306订票代码
2015/12/20 Python
Python实现查找字符串数组最长公共前缀示例
2019/03/27 Python
Django密码系统实现过程详解
2019/07/19 Python
详细分析Python垃圾回收机制
2020/07/01 Python
python opencv实现简易画图板
2020/08/27 Python
通过代码实例了解Python sys模块
2020/09/14 Python
小学教师听课制度
2014/02/01 职场文书
2014年中班元旦活动方案
2014/02/14 职场文书
双拥工作宣传标语
2014/06/26 职场文书
群众路线领导对照材料
2014/08/23 职场文书
2014年机关作风建设工作总结
2014/10/23 职场文书