Deeply parse the WSGI interface in Python and parse the pythonwsgi Interface
Overview
WSGI interfaces include server/gateway and application/framework.
The server calls the callable object provided by the application.
In addition, there may be a middleware called middleware between the server and application.
Callable objects refer to functions, methods, classes, or instances with callable methods.
About application
Functions, methods, classes, and instances with callable methods can all be called as the application object.
WSGI Protocol requirements:
The application object accepts two parameters and can be called multiple times.
The two parameters are:
1. CGI-type dictionary;
2. callback function: the application is used to pass the http status code/message/http header to the server.
In addition, the Protocol requires that the callable object must encapsulate the response body into an iterative strings return.
# The application object. you can use another name. # However, when using mod_wsgi, it must be "application" def application (environ, start_response): # The function accepts two parameters: # environ: A dictionary containing CGI Environment variables. The server is responsible for providing the content # start_response: the callback function provided by the server, the function is to return the status code and response header to the server # construct the response body, response_body = 'the request method was % s' % environ ['request _ method'] # HTTP response code and message status = '200 OK '# provided to The client in The form of an iterative string response header. # encapsulation into the form of list of tuple pairs: # format requirements: [(Header name, Header value)]. response_headers = [('content-type', 'text/plain '), ('content-length', str (len (response_body)] # Return the response code/message and response header to the server start_response (status, response_headers) through the input start_reponse callback function # Return the response body as the return value # note that the response is encapsulated in the list. return [response_body]
About server
As you can see in the overview, the WSGI server must call the application. At the same time, we can see from the Protocol requirements of the application:
1. WSGI server must provide environment parameters to the application. Therefore, it must be able to obtain environment parameters.
2. The WSGI server receives the return value of the application as the response body.
The simplest WSGI server is the wsgiref. simple_server that comes with Python.
Example:
from wsgiref.simple_server import make_serversrv = make_server('localhost', 8080, hello_world)srv.serve_forever()
About middleware
The concept of middleware is not as easy to understand as appllication and server.
Assume that an application-compliant callable object accepts the callable object as a parameter and returns an callable object.
For the server, it is a standard callable object, so it is an application.
For application, it can call application, so it is a server.
This callable object is called middleware.
The concept of middleware is very similar to decorator.
Example of a route:
Import re # This is a standard application objectdef index (environ, start_response): start_response ('2017 OK ', [('content-type', 'text/html')]) return ['index page'] # This is a standard application objectdef hello (environ, start_response): start_response ('2014, 200 OK ', [('content-type ', 'text/html')]) return ['Hello page'] # This is a standard application objectdef not_found (environ, start_response): start_response ('2017 not found ', [('conten T-type', 'text/plain ')]) return ['not Found page'] # map urls to functionsurls = [(r' ^ $ ', index ), (r'hello /? $ ', Hello)] # This is a middleware # Different application objectdef application (environ, start_response) are returned based on different route: path = environ. get ('path _ info ',''). lstrip ('/') for regex, callback in urls: match = re. search (regex, path) if match is not None: