Python-WSGI interface

Python WSGI specifies a standard interface between a web server and a Python web application or web framework, mainly to promote the portability of web applications on various web servers.

The above sentence is translated from the official Python PEP333 standard: PEP 333-Python Web Server Gateway Interface v1.0

WSGI interface overview#

The meaning of WSGI: Web Server Gateway Interface (Web server network management interface).

The WSGI interface includes two aspects: the server/gateway side and the application/framework side. The server and application will be used directly later to illustrate, instead of using gateway and framework. The server side directly calls the callable object provided by the application side. In addition, there can be a kind of middleware called middleware between the server and the application. The middleware is an application for the server, but the middleware is a server for the application.

The above-mentioned callable objects refer to functions, methods, classes or instances with __call__ methods.

The following are three parts: application side, server side and middleware

Application end#

Callable objects such as functions, methods, classes, and instances with callable methods can all be used as application objects. The application object accepts two parameters and can be called multiple times.

parameter

The meaning of the three parameters of start_response is as follows:

response_headers =[('Content-Type','text/plain'),('Content-Length',str(len(response_body)))]

return value

The application object must return a response body, which is in the form of list of str, which means that the return value is a list of one or more strings.

The following is an example of a function as an application object

def simple_app(environ, start_response):"""The simplest application object"""
 status ='200 OK'
 response_headers =[('Content-type','text/plain')]start_response(status, response_headers)return['Hello world!\n']

The following is an example of a class as an application object

classAppClass:"""
 AppClass()Will return an AppClass class object as application, and then it will be called when iterating__iter__Method, and then you can produce the same output.
 If we can also achieve__call__The method directly treats the instance as application
    """

 def __init__(self, environ, start_response):
  self.environ = environ
  self.start = start_response

 def __iter__(self):
  status ='200 OK'
  response_headers =[('Content-type','text/plain')]
  self.start(status, response_headers)yield"Hello world!\n"

Server side#

The WSGI server must call the application, and use the positional parameter to call the application. At the same time, we can see from the application protocol requirements:

The simplest WSGI server is wsgiref.simple_server that comes with Python.

Code

from wsgiref.simple_server import make_server
server =make_server('localhost',8080, application)
server.serve_forever()

Middleware

The middleware is located between the WSGI server and the WSGI application. Part of the code reference about the middleware:

code show as below

from wsgiref.simple_server import make_server

def application(environ, start_response):

 response_body ='hello world!'

 status ='200 OK'

 response_headers =[('Content-Type','text/plain'),('Content-Length',str(len(response_body)))]start_response(status, response_headers)return[response_body]

# Middleware
classUpperware:
 def __init__(self, app):
  self.wrapped_app = app

 def __call__(self, environ, start_response):for data in self.wrapped_app(environ, start_response):yield data.upper()

wrapped_app =Upperware(application)

httpd =make_server('localhost',8051, wrapped_app)

httpd.serve_forever()

print 'end'

Recommended Posts

Python-WSGI interface