This paper records some knowledge points of learning Wsgi, which deserves attention in follow-up study.
WSGI application interface As long as it is a callable object on the line, this callable object requires:
1. Accept two position parameters:
A. A dictionary containing the CGI form variables;
B. The callback function to which the call is applied, the function of which is to return the status code and header of the HTTP response to the server.
2. The contents of the response body part as a (several) string wrapped in an object that can be iterated over.
Description
1. The first parameter of the application env is a dictionary that contains the CGI-style environment variables that are populated by the server based on the client request.
2. When building, headers must adhere to the following rules:
[(Header name1, header value1), (header name2, header value2),]
The response header and the response HTTP status code are sent back to the server by the second parameter that is applied, the callback function.
3.body at the time of construction, the following rules must be followed:
[Response_body]
That is, the response body must be wrapped in an iterative object and returned to the server through a return statement.
Here is an example from the official wsgi.org tutorial:
Note the meaning of the Environ parameter, the role of the Start_response function, response_headers and status are returned by WHO to the server, what form response_body needs, how to return to the server, and so on.
From Wsgiref.simple_server import make_serverdef application (environ, start_response): # response_body in a list re Sponse_body = ['%s:%s '% (key, value) for key, value in sorted (Environ.items ())] response_body = ' \ n '. Join (respon se_body) response_body = [' The begining\n ', ' * ' * + ' \ n ', Response_bod Y, ' \ n ' + ' * ' *, ' \nthe End ' content_length = 0 for s in resp Onse_body:content_length + = Len (s) #status code and Response_headers status = ' OK ' Response_header S =[(' Content-type ', ' Text/plain '), (' Content-length ', str (CONTENT_LENGTH))] # Use callback function to send BAC K Status code # and Response headers Start_response (status, response_headers) # return response Body THRUOGH RET Urn statement return response_bodyhttpd = make_server (' localhost ', 8051,application) httpd.handle_request ()
Python WSGI Development Essay