01 flask source code analysis of werkzurg understand wsgi

01 werkzurg understand wsgi

1. wsgi

django and internal flask did not realize socket, but wsgi achieve.
wsgi web services gateway interface, he is a protocol to implement its agreements are: wsgiref / werkzurg / uwsgi

  1. Before django

    from wsgiref.simple_server import make_server
    
    def run(environ, start_response):
        start_response('200 OK', [('Content-Type', 'text/html')])
        return [bytes('<h1>Hello, web!</h1>', encoding='utf-8'), ]
    
    if __name__ == '__main__':
        httpd = make_server('127.0.0.1', 8000, run)
        httpd.serve_forever()
  2. Before flask

    from werkzeug.serving import run_simple
    from werkzeug.wrappers import BaseResponse
    
    
    def func(environ, start_response):
        print('请求来了')
        response = BaseResponse('你好')
        return response(environ, start_response)
    
    
    if __name__ == '__main__':
        run_simple('127.0.0.1', 5000, func)

2. flask of werkzurg source process

  1. Commencement of proceedingsapp.run()

    from flask import Flask
    
    app = Flask(__name__)
    
    @app.route('/index')
    def index():
        return 'hello world'
    
    if __name__ == '__main__':
        app.run()
  2. Performed inside run_simplemethod bracketed third parameter

    def run(self, host=None, port=None, debug=None, load_dotenv=True, **options):
          from werkzeug.serving import run_simple
          run_simple(host, port, self, **options)
  3. Trigger the execution __call__method, and then to perform the wsgi_appmethod

    def __call__(self, environ, start_response):
     return self.wsgi_app(environ, start_response)
  4. Execution wsgi_appmethod inside the full_dispatch_requestmethod

    def wsgi_app(self, environ, start_response):
     response = self.full_dispatch_request()
     return response(environ, start_response)

    full_dispatch_request

    def full_dispatch_request(self):
        return self.finalize_request(rv)
  5. Performing finalize_requestthe method, carrying rv parameter, the return value is a view rv

    def finalize_request(self, rv, from_error_handler=False):
        response = self.make_response(rv)
        return response
  6. Execution finalize_requestmethod inside the make_responsemethod

    def make_response(self, rv):
     if not isinstance(rv, self.response_class):
             if isinstance(rv, (text_type, bytes, bytearray)):
                 rv = self.response_class(rv, status=status, headers=headers)
                 return rv
  7. response_class

    • response_class=Response
    • ResponseThe inheritance werkzurgBaseResponse

3. Summary

  • Request came after a series, and finally returned by BaseResponse
from werkzeug.serving import run_simple
from werkzeug.wrappers import BaseResponse

def func(environ, start_response):
    print('请求来了')
    response = BaseResponse('你好')
    return response(environ, start_response)


if __name__ == '__main__':
    run_simple('127.0.0.1', 5000, func)

Guess you like

Origin www.cnblogs.com/liubing8/p/11930066.html