Reproduced from: http://blog.kenshinx.me/blog/wsgi-research/
A slight change.
WSGI (web Server Gateway Interface) is the interface specification for Web Components.
WSGI divides Web Components into three categories: Web server, middleware, Web application.
Request-> WSGI server/gateway-> WSGI Middleware (s)-> low level WSGI application-> High level Web Framewor K
Response <-WSGI server/gateway <-WSGI Middleware (s) <-low level WSGI application <-High level Web Framewor K
WSGI Server:
Receive request, encapsulate environment variable, call registered Wsgi app, return response to client.
WSGI Application:
A callable object. When a request arrives, WSGI server invokes the app.
Receive 2 parameters: Environ and Start_response
Environ includes server information, client information, and request information; Start_response is a callback function.
The following is one of the simplest Wsgi apps, quoted from http://www.python.org/dev/peps/pep-3333/
def simple_app (environ, start_response):
status = ' OK '
response_headers = [(' Content-type ', ' Text/plain ']]
start_response (status, Response_headers) return
[u "This is Hello Wsgi app". Encode (' UTF8 ')]
Using Wsgiref as Wsgi server, and then call the Wsgi app above, you can see the effect of the request and response directly, the code is as follows:
From Wsgiref.simple_server import make_server
def simple_app (environ, start_response):
status = ' OK '
Response_headers = [(' Content-type ', ' Text/plain ')]
start_response (status, Response_headers) return
[u] is Hello Wsgi app ". Encode (' UTF8 ')]
httpd = Make_server (', 8000, Simple_app)
print" Serving on port 8000 ... "
Httpd.serve_forever ()
Access to the http://127.0.0.1:8000 will be able to see the effect.
It says Wsgi app is just a callable object, so it's not necessarily a function, it can be an instance that implements the call method. The code is as follows:
From Wsgiref.simple_server import Make_server
class Appclass:
def __call__ (Self,environ, Start_response):
status = ' OK '
response_headers = [(' Content-type ', ' Text/plain ')]
start_response (status, Response_ Headers) return
["Hello world!"]
App = Appclass ()
httpd = Make_server (", 8000, app)
print" Serving on port 8000 ... "
Httpd.serve_forever ()
The top of the application plus a layer of middleware packaging will be some use. The following is a simple dispatcher middleware to implement URL routing:
From Wsgiref.simple_server import make_server url_patterns= (' hi/', ' Say_hi '), (' hello/', ' Say_hello '), class
Dispatcher (object): Def _match (self,path): Path = Path.split ('/') [1] for Url,app in Url_patterns: If path in Url:return app def __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 say_hi (environ, start_response): Start_response ("OK", [(' Content-type ', ' text/html ')]] return ["Finix say H
I to you! "] def say_hello (environ, start_response): Start_response ("OK", [(' Content-type ', ' text/html ')]] return ["Finix SA
Y hello to you! "] App = Dispatcher () httpd = Make_server (', 8000, app) print ' serving On port 8000 ... "httpd.serve_forever ()
The example above can be seen, middleware packaging, a simple Wsgi app has a URL dispatch function. You can then add other middleware to the app to wrap it, such as adding a permission-certified middleware:
Class Auth (object):
def __init__ (self,app):
Self.app = App
def __call__ (Self,environ, Start_response):
# Todo:add Authentication return
Self.app (environ, start_response)
app = Dispatcher ()
Auth_app = Auth (APP)
httpd = Make_server (", 8000, Auth_app)
print" Serving on port 8000 ... "
Httpd.serve_forever ()