What is WSGI in python, WSGI in Python, and pythonwsgi

Source: Internet
Author: User

What is WSGI in python, WSGI in Python, and pythonwsgi

To give you a better understanding of WSGI in python, let's start with the simplest understanding of WSGI, and then introduce several frequently used WSGI interfaces, understand the basic usage and functions. Finally, let's take a look at how WSGI is used in actual projects through examples.

What is WSGI?

Wsgi is an interface for web Components. wsgi classifies web components into three types: web servers, web middleware, and web applications.

Wsgi basic processing mode: wsgi Server-> wsgi middleware-> wsgi application

WSGI, short for Web Server Gateway Interface or Python Web Server Gateway Interface, is a simple and common Interface between the Web Server and Web application or framework defined for the Python language. Since WSGI was developed, similar interfaces have also appeared in many other languages.

The official definition of WSGI is the Python Web Server Gateway Interface. It can be seen from the name that this is a Gateway, that is, a Gateway. The gateway is used to convert between protocols.

WSGI is a low-level interface between a Web server and a Web application or application framework, to improve the commonality of Web application development that can be transplanted. WSGI is designed based on the existing CGI standards.

Many frameworks have built-in WSGI servers, such as Flask, webpy, Django, and CherryPy. Of course, the performance is not good. The built-in web server is mostly for testing purposes. During the release, the WSGI server in the production environment or the uwsgi with nginx is used.

That is to say, WSGI is like a bridge connecting web servers and Applications of users. However, the function of this bridge is very weak. Sometimes other bridges are needed for help.

Function of WSGI

WSGI has two sides: "server" or "Gateway", and "application" or "application framework. The service provider calls the application, provides the environment information, and a callback function (provided to the application for passing the message header to the server), and receives the Web content as the return value.

The so-called WSGI middleware implements both APIs at the same time, so it can mediate between the WSGI service and the WSGI application: From the Perspective of the WSGI server, middleware acts as an application, from the application perspective, middleware acts as a server. The middleware component can perform the following functions:

After the environment variable is overwritten, the request message is routed to different application objects based on the target URL.

Allows you to run multiple applications or application frameworks simultaneously in a process.

Server Load balancer and remote processing are used to forward requests and response messages over the network.

After content processing, such as applying XSLT style sheets.

Wsgi server:

It is understood as a web server that complies with wsgi specifications, receives request requests, encapsulates a series of environment variables, calls the registered wsgi app according to wsgi specifications, and finally returns response to the client.

Workflow:

1. The server creates a socket, listens to the port, and waits for the client to connect

2. When the request comes, the server parses the client msg and puts it in the environment variable environ, and calls the bound handler for processing.

3. handler parses the http request and places the request message, such as method and path, into environ.

4. wsgi handler puts some server-side messages into environ. Finally, server msg, client msg, And the request msg are all saved to the environment variable envrion;

5. wsgi handler calls the registered wsgi app and sends the envrion and callback functions to the wsgi app.

6. The wsgi app returns the reponse header/status/body to wsgi handler.

7. handler uses socket to return response msg to the client

WSGI Application

Wsgi application is a common callable object. When a request arrives, wsgi server calls this wsgi app. This object receives two parameters, usually environ and start_response. As mentioned earlier, environ can be understood as an environment variable,

All information related to a request is stored in this environment variable, including server information, client information, and request information. Start_response is a callback function. wsgi application returns response headers/status to wsgi server by calling start_response. In addition, this wsgi app will return an iterator object, which is the response body.

Dispatcher Middleware, used to implement URL routing: (code description)

#! /Usr/bin/python # encoding = UTF-8 # Use wsgiref as wsgi serverfrom wsgiref. simple_server import make_server "def simple_app (environ, start_response): status = '000000' response _ headers = [('content-type', 'text/plain ')] # Set the http header start_response (status, response_headers) return [u "test wsgi app ". encode ('utf-8')] class AppClass (object): def _ call _ (self, environ, start_response): status = "200 OK" response_headers = [('Content-type', 'text/plain ')] start_response (status, response_headers) return [u "class AppClass ". encode ('utf-8')] "" # As long as the wsgi app is a callable object, not necessarily if the function # An example of implementing the _ call _ method is also OK # httpd = make_server ('', 8080, simple_app)" app = AppClass () httpd = make_server ('', 8080, app) httpd. serve_forever () "" URL_PATTERNS = ('aa/', 'aa _ app'), ('bb/', 'bb _ app '),) class Dispatcher (object): # implement the routing function: def _ match (self, pa Th): path = path. split ('/') [1] for url, app in URL_PATTERNS: if path in url: return appdef _ call _ (self, environ, start_response): path = environ. get ('path _ info', '/') app = self. _ match (path) if app: app = globals () [app] return app (environ, start_response) else: start_response ("404 not found ", [('content-type', 'text/plain ')]) return ["page dose not exists"] def AA_app (environ, start_response): start_response (" 200 OK ", [('content-type', 'text/html')]) return [" AA page "] def BB_app (environ, start_response ): start_response ("200 OK", [('content-type', 'text/html')]) return ["BB page"] app = Dispatcher () httpd = make_server ('', 8090, app) httpd. serve_forever () Test Result: server: root @ u163 :~ /Cp163/python # python wsgi_app.py 192.168.20.2--[04/Nov/2015 18:44:06] "GET/aa http/1.1" 200 7192.168.20.2--[04/Nov/2015 18:44:22] "GET /bb http/1.1 "200 7client: root @ u162 :~ # Curl http: // 192.168.20.3: 8090/AAAA pageroot @ u162 :~ # Curl http: // 192.168.20.3: 8090/BBBB pageroot @ u162 :~ #

Next we recommend a detailed introduction to the shutdown interface: in-depth analysis of the WSGI interface in Python

Related Article

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.