deflog_message(self, format, *args): extra = { 'request': self.request, 'server_time': self.log_date_time_string(), } if args[1][0] == '4': # 0x16 = Handshake, 0x03 = SSL 3.0 or TLS 1.x if args[0].startswith('\x16\x03'): extra['status_code'] = 500 logger.error( "You're accessing the development server over HTTPS, but " "it only supports HTTP.\n", extra=extra, ) return
defcleanup_headers(self): super().cleanup_headers() # HTTP/1.1 requires support for persistent connections. Send 'close' if # the content length is unknown to prevent clients from reusing the # connection. if'Content-Length'notin self.headers: self.headers['Connection'] = 'close' # Mark the connection for closing if it's set as such above or if the # application sent the header. if self.headers.get('Connection') == 'close': self.request_handler.close_connection = True
defrun(addr, port, wsgi_handler, ipv6=False, threading=False, server_cls=WSGIServer): server_address = (addr, port) if threading: httpd_cls = type('WSGIServer', (socketserver.ThreadingMixIn, server_cls), {}) else: httpd_cls = server_cls httpd = httpd_cls(server_address, WSGIRequestHandler, ipv6=ipv6) if threading: # ThreadingMixIn.daemon_threads indicates(指出) how threads will behave on an # abrupt shutdown; like quitting the server by the user or restarting # by the auto-reloader. True means the server will not wait for thread # termination before it quits. This will make auto-reloader faster # and will prevent the need to kill the server manually if a thread # isn't terminating correctly. httpd.daemon_threads = True# daemon_thread 守护进程 httpd.set_app(wsgi_handler) httpd.serve_forever()
if __name__ == "__main__": clazz_A = type("A", (A, C), {}) t = clazz_A() t.bar() t.foo() print(type(t)) clazz_B = type("B", (A, C), {}) t = clazz_B() t.bar() t.foo() print(type(t)) clazz_A = type("A", (B, C), {}) t = clazz_A() t.bar() t.foo() print(type(t))
""" You Called C.bar You Called A.foo <class '__main__.A'> You Called C.bar You Called A.foo <class '__main__.B'> You Called C.bar You Called B.foo <class '__main__.A'> """
这个就是启动函数,会创建一个 WSGI Server 的实例,和我们在上一篇中的流程图唯一有一个不同就是使用的是 WSGI Handler类,而不是模块里写的一个 demo_app。
from django.core.wsgi import get_wsgi_application defget_internal_wsgi_application(): from django.conf import settings app_path = getattr(settings, 'WSGI_APPLICATION') if app_path isNone: return get_wsgi_application()
try: return import_string(app_path) except ImportError as err: raise ImproperlyConfigured( "WSGI application '%s' could not be loaded; " "Error importing module." % app_path ) from err
# wsgi.py from django.core.handlers.wsgi import WSGIHandler defget_wsgi_application(): django.setup(set_prefix=False) return WSGIHandler()
import copy import os from importlib import import_module from importlib.util import find_spec as importlib_find
defimport_string(dotted_path): """ Import a dotted module path and return the attribute/class designated by the last name in the path. Raise ImportError if the import failed. """ try: module_path, class_name = dotted_path.rsplit('.', 1) except ValueError as err: raise ImportError("%s doesn't look like a module path" % dotted_path) from err
module = import_module(module_path)
try: returngetattr(module, class_name) except AttributeError as err: raise ImportError('Module "%s" does not define a "%s" attribute/class' % ( module_path, class_name) ) from err