Django源码学习总结

Django源码阅读:

1. Django runserver 127.0.0.1 8000 –noreload

入口:manage.py 

代码执行调用 追根溯源

execute_from_command_line(sys.argv)

||

ManagementUtility(argv)   utility.execute()

||

fetch_command(subcommand).run_from_argv(self.argv) // fetch_command('runserver')返回runserverCommand 执行根父类 run_from_argv()
继承关系 Command <- RunserverCommand <- BaseCommand(run_from_argv)

||

Command(inner_run)  // 执行inner_run()           

run(self.addr, int(self.port), handler,ipv6=self.use_ipv6, threading=threading)

bashhttp (WSGIRequestHandler) run // 启动http tcpserver

httpd = httpd_cls(server_address, WSGIRequestHandler, ipv6=ipv6)

httpd.set_app(wsgi_handler)

继承关系 WSGIServer <- HTTPServer <- SocketServer.TCPServer <- BaseServer

   - __init__(server_address, RequestHandlerClass, bind_and_activate=True)

2. Django 相应http请求:

接口调用追根溯源 WSGIHandler(base.BaseHandler)(__call__()) 

|| 

 
  
	handler = convert_exception_to_response(self._get_response)
	handler = convert_exception_to_response(mw_instance)
	self._middleware_chain(request)

	调用父类 get_response(request)
middleware层 处理request
返回response

3. 总结:
make_server(host, port, app, server_class=WSGIServer, handler_class=WSGIRequestHandler)

启动 WSGIServer; 入口:WSGIHandler(__call__()); URL-views匹配:URLResolvers

ps:第一次写点东西,不足之处还望大神指教

你可能感兴趣的:(后端开发技术)