Bottle's request and wsgi protocol

First draw a simple sequence diagram of the request http process:

    bottle is a framework that supports wsgi. When the client initiates an http request, the http message will first be processed into an environ dictionary by the wsgi server, and the bottle will encapsulate this dictionary into a global request object, and the user code will get the information encapsulated by the request to carry out business logic The processing returns the response object, which is processed by the wsgi server into an http res message and returned to the http client.

    Bottle's request is used to process the encapsulated environ dictionary, which is generated by the wsgi server. What's in the environ. A simple way is to run python -m wsgiref.simple_server, then the default browser will be opened, and a local http get request on port 8000 will be initiated. On the page, you will see the environ generated after the get request is processed by the wsgi server. The content of the dictionary, what each field means, you can read this chapter of pep3333 https://www.python.org/dev/peps/pep-3333/#environ-variables , the default development server of bottle is also wsgiref.simple_server. Let's talk about the processing of GET and POST requests here.

1、GET

    In the page you just opened, you can see the two key key-values ​​of the get request, one is REQUEST_METHOD = 'GET', and the other is QUERY_STRING = 'abc' (the content in the header is not discussed here). Go to the source code to see how the bottle handles these two keyvalues. Bottle uses the Bottle class object as the wsgi protocol function, so the Bottle object is callable, and Bottle will implement the __call__ method. The calling process is Bottle .__ call__  --> Bottle.wsgi --> Bottle._handle--> request.bind, which uses the environ dictionary to initialize and generate a global request object. If you locate the BaseRequest.GET method, you can see that GET is an alias for query. The query method adds a key 'bottle.get' to self.environ, then parses the QUERY_STRING, and returns a dictionary-like read-only object through the decorator for the user to use. The user code can get the request parameters of the GET request through request.GET.

2、POST

    The data of the POST request is placed in the body for row submission. If the user submits a form, here are the four key-values ​​in environ:

REQUEST_METHOD = 'POST'
CONTENT_LENGTH = ''
CONTENT_TYPE = 'multipart/form-data; boundary=----WebKitFormBoundary5N9kIKpwXYrgi5jb'
wsgi.input = <_io.BufferedReader name=636>

    Not much to say about the first three. wsgi.input is a file object generated by the wsgi server and passed to the bottle through the environ. The bottle reads the content in the body through the file object and then parses it. Check the BaseRequest.POST method and check the environment['wsgi. input'] to process and return a BytesIO() object, and then use the cgi.FieldStorage type to parse the body data, and return the form key-value pair and file object. You can run this code https://github.com/kagxin/recipes/blob/master/bottle/wsgi_server.py  This code is used, and client tools such as postman initiate GET and POST requests to view the content of the environ variable. Locate the demo_app and go to the wsgiref.simple_server to see the demo_app and the main function to know why python -m wsigref.simple_server is the phenomenon just now. simple_server uses the original HttpServer BaseHTTPRequestHandler to parse the http protocol message, and adds some key-values ​​required by wsgi to the original parsing content to obtain the wsgi standard environ.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325435176&siteId=291194637