Deep parsing of the Wsgi interface in Python

Source: Internet
Author: User
Tags response code

This article mainly introduces the deep analysis Python in the Wsgi interface, the Wsgi interface is the Python network framework to connect the server the essential tool, needs the friend may refer to under

Overview

The Wsgi interface consists of two aspects: Server/gateway and application/framework.

The server invokes the callable object provided by application.

There may also be a middleware called middleware between the server and the application.

Callable objects refer to: Functions, methods, classes, or instances with callable methods.

About application

Callable objects, such as functions, methods, classes, and instances with callable methods, can be used as the Application object.

WSGI protocol Requirements:

The Application object accepts two parameters and can be called multiple times

The two parameters are:

A dictionary of 1.CGI type;

2. Callback function: Application is used to pass HTTP status codes/messages to the server/http headers

In addition, the Protocol requires that the callable object must encapsulate the response body into an iterative strings return.

?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24-25 # the Application object. You can use a different name, # but you must use MOD_WSGI to "Application" def application (environ, start_response): # function accepts two parameters: # environ: Contains CGI-type environment changes A dictionary of quantities, provided by the server for content # Start_response: A callback function provided by the server that returns the status code and response headers to the server # construct response body, encapsulating the response_body = ' in an iterative string Request method is%s '% environ[' Request_method '] # HTTP response code and message status = ' OK ' # provided to the client's response header. # encapsulated in 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))] # The response code/ The message and response headers are returned by the incoming Start_reponse callback function to the server Start_response (status, response_headers) # response body as the return value # Note this is encapsulated in the list. return [Response_body]

About the server

As you can see from the overview, the WSGI server must call application, and the protocol requirements from application:

1. WSGI server must provide environmental parameters to application, and therefore itself must be able to obtain environmental parameters.

2. WSGI server receives the return value of application as the response body.

The simplest WSGI server for Python's own wsgiref.simple_server

Examples are as follows:

?

1 2 3 From wsgiref.simple_server import make_server srv = 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.

Suppose a callable object that conforms to the application standard, which takes the callable object as a parameter and returns an object of the callable object.

So for the server, it's a standard callable object, so it's application.

For application, it can call application and therefore is the server.

Such callable objects are called middleware.

The concept of middleware is very close to decorator.

Example of a routing example:

?

/table>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 The import re   # This is a standard Application Object Def index (E Nviron, Start_response): Start_response (' OK ', [(' Content-type ', ' text/html ']]) return [' Index page ']   # This is a standard Application Object def hello (environ, start_response): Start_response (' OK ', [(' Content-type ', ' text/html ')] return [' Hello page ']   # This is a standard application object Def not_found (environ, start_response): start_response (' 404 Not Found ', [(' Content-type ', ' Text/plain ')] return [' Don't Found Page ']   # map URLs to functions URLs = [(R ' ^$ ', index], (R ' hello/?$ ', hello)] # This is a middleware # returns different Application Object Def application (environ, start_response) according to different route: Path = environ.get (' path_info ', '). Lstrip ('/') for Regex, callback in Urls:match = Re.search (regex, path) if the not None:
Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.