Flask Quick Start Note Three _ Context object: Flask core mechanism

Source: Internet
Author: User

The first statement: Most of the content from Huizhiwang, just take notes.

1 Request: WSGI

WSGI, full name Web server Gateway Interface, or Python Web server gateway Interface, is a Web server defined for the Python language and W a simple and versatile interface between EB applications.

WSGI divides a Web service into two parts: servers and applications. The WGSI server is only responsible for two things related to the network: receiving the HTTP request from the browser, sending an HTTP response to the browser, and the specific processing logic for the HTTP request by invoking the WSGI application.

To implement a WSGI application, you need to meet only 3 requirements:

    1. Is callable, such as a function, or an instance of a callable class (with a __call__ method)
    2. The WSGI application should return a value that can be iterated (iterable), such as a list of strings
    3. WSGI application should call the WSGI server incoming start_response function to send the status code and HTTP headers before returning

Minimum WSGI application

One of the simplest applications to satisfy the WSGI protocol is to implement a function of the specified form:

 fromWsgiref.simple_serverImportMake_serverdefWsgi_app (environ,start_response): Start_response ('OK',[('Context-type','Text/plain')])    return 'such a tiny Wsgi app!'httpd= Make_server ('0.0.0.0', 80, Wsgi_app) httpd.serve_forever ()

environ is a dictionary/dict that contains all the HTTP request information and is generated by the WSGI server unpacking the HTTP request.

Using classes to implement WSGI applications

You can also use classes to implement WSGI applications, and an instance of a callable class can be called like a function, so just pass the instance object to Make_server ().

This class-based implementation is the choice of most frameworks because it is easy to extend a wide variety of processing logic.

The following example establishes a minimized WSGI application in the form of a Python class:

class Wsgi_app:     def __call__ (self,environ,start_response):        start_response (' kOK', [(' Context-type ','text/plain')])        return' such a tiny Wsgi app! '  = Wsgi_app ()  

Because of the existence of WSGI, different Web servers and different Web applications can be paired, as long as they follow the WSGI specification.

Flask Framework is used in the development of the app part, in the development environment we can simply use the flask built-in development with the Web server, and in the production environment with better performance of professional Web servers, such as Apache, Gunicorn and so on.

2 Flask Processing Process

The Flask class is the implementation of the WSGI Application specification in the Flask framework. Whenever an HTTP request is received by the WSGI server, the instance object of the Flask class is called for processing, with the following logic:

  1. creates the context object , which includes the RequestContext object that characterizes the current HTTP request information and the AppContext object that characterizes the current application.

    The RequestContext object consists of two objects that are related to the request information: The request object is basically based on the WSGI server's incoming environment variable environ encapsulation, which represents all the information from the client; The session object re-loads the conversation information associated with the visitor based on the cookie in the request.

    The Appcontext object primarily provides the user with a valid storage space during a request cycle. For example:g.

  2. into the stack . Push two context objects into two global stacks, respectively:_request_ctx_stack and _app_ctx_stack

  3. Request Distribution . Call the dispatch_request() function to handle the object at the top of the _request_ctx_stack stack, which is the RequestContext object we just pushed in. The main task of Dispatch_request () is to locate and invoke the view function through the routing table based on the URL information of the HTTP request.

    It is worth noting that dispatch_request () is not directly into the request object generated by the preamble, but rather requires the request object to use the Reqestcontext at the top of the _request_ctx_stack stack . This means that all request-related processing (including our defined view functions) within dispatch_request () must depend on the data context provided by _request_ctx_stack-so called context/context.

    The global object request that we use actually points to the top of the stack object, so trying to access the request outside of the declaration cycle of the requested answer is not valid.

    Dispatch_request () returns a Response object.

  4. out of the stack. To stack two context objects

  5. response Wsgi. Invokes the response object, returning its result to the WSGI server as the HTTP body. The response object is a callable object, and when the call occurs, the WSGI server's incoming Start_response () function is executed first to send a status code and an HTTP header.

Flask Quick Start Note Three _ Context object: Flask core mechanism

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.