从上图可以看出来,wsgi基本处理模式为 : WSGI Server -> (WSGI Middleware)* -> WSGI Application 。
- 服务器创建socket,监听端口,等待客户端连接。
- 当有请求来时,服务器解析客户端信息放到环境变量environ中,并调用绑定的handler来处理请求。
- handler解析这个http请求,将请求信息例如method,path等放到environ中。
- wsgi handler再将一些服务器端信息也放到environ中,最后服务器信息,客户端信息,本次请求信息全部都保存到了环境变量environ中。
- wsgi handler 调用注册的wsgi app,并将environ和回调函数传给wsgi app
- wsgi app 将reponse header/status/body 回传给wsgi handler
- 最终handler还是通过socket将response信息塞回给客户端。
<span class="kwd" style="font-family:inherit;color:#0088;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">def</span><span class="pln" style="font-family:inherit;color:#000000;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;"> simple_app</span><span class="pun" style="font-family:inherit;color:#66660;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">(</span><span class="pln" style="font-family:inherit;color:#000000;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">environ</span><span class="pun" style="font-family:inherit;color:#66660;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">,</span><span class="pln" style="font-family:inherit;color:#000000;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;"> start_response</span><span class="pun" style="font-family:inherit;color:#66660;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">):</span><span class="pln" style="font-family:inherit;color:#000000;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;"> status </span><span class="pun" style="font-family:inherit;color:#66660;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">=</span><span class="pln" style="font-family:inherit;color:#000000;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;"> </span><span class="str" style="font-family:inherit;color:#0880;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">'200 OK'</span><span class="pln" style="font-family:inherit;color:#000000;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;"> response_headers </span><span class="pun" style="font-family:inherit;color:#66660;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">=</span><span class="pln" style="font-family:inherit;color:#000000;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;"> </span><span class="pun" style="font-family:inherit;color:#66660;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">[(</span><span class="str" style="font-family:inherit;color:#0880;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">'Content-type'</span><span class="pun" style="font-family:inherit;color:#66660;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">,</span><span class="pln" style="font-family:inherit;color:#000000;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;"> </span><span class="str" style="font-family:inherit;color:#0880;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">'text/plain'</span><span class="pun" style="font-family:inherit;color:#66660;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">)]</span><span class="pln" style="font-family:inherit;color:#000000;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;"> start_response</span><span class="pun" style="font-family:inherit;color:#66660;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">(</span><span class="pln" style="font-family:inherit;color:#000000;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">status</span><span class="pun" style="font-family:inherit;color:#66660;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">,</span><span class="pln" style="font-family:inherit;color:#000000;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;"> response_headers</span><span class="pun" style="font-family:inherit;color:#66660;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">)</span><span class="pln" style="font-family:inherit;color:#000000;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;"> </span><span class="kwd" style="font-family:inherit;color:#0088;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">return</span><span class="pln" style="font-family:inherit;color:#000000;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;"> </span><span class="pun" style="font-family:inherit;color:#66660;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">[</span><span class="pln" style="font-family:inherit;color:#000000;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">u</span><span class="str" style="font-family:inherit;color:#0880;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">"This is hello wsgi app"</span><span class="pun" style="font-family:inherit;color:#66660;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">.</span><span class="pln" style="font-family:inherit;color:#000000;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">encode</span><span class="pun" style="font-family:inherit;color:#66660;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">(</span><span class="str" style="font-family:inherit;color:#0880;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">'utf8'</span><span class="pun" style="font-family:inherit;color:#66660;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">)]</span>
我们再用wsgiref 作为wsgi server ,然后调用这个wsgi app,就能直观看到一次request,response的效果,简单修改代码如下:
<span class="kwd" style="font-family:inherit;color:#0088;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">from</span><span class="pln" style="font-family:inherit;color:#000000;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;"> wsgiref</span><span class="pun" style="font-family:inherit;color:#66660;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">.</span><span class="pln" style="font-family:inherit;color:#000000;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">simple_server </span><span class="kwd" style="font-family:inherit;color:#0088;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">import</span><span class="pln" style="font-family:inherit;color:#000000;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;"> make_server </span><span class="kwd" style="font-family:inherit;color:#0088;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">def</span><span class="pln" style="font-family:inherit;color:#000000;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;"> simple_app</span><span class="pun" style="font-family:inherit;color:#66660;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">(</span><span class="pln" style="font-family:inherit;color:#000000;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">environ</span><span class="pun" style="font-family:inherit;color:#66660;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">,</span><span class="pln" style="font-family:inherit;color:#000000;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;"> start_response</span><span class="pun" style="font-family:inherit;color:#66660;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">):</span><span class="pln" style="font-family:inherit;color:#000000;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;"> status </span><span class="pun" style="font-family:inherit;color:#66660;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">=</span><span class="pln" style="font-family:inherit;color:#000000;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;"> </span><span class="str" style="font-family:inherit;color:#0880;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">'200 OK'</span><span class="pln" style="font-family:inherit;color:#000000;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;"> response_headers </span><span class="pun" style="font-family:inherit;color:#66660;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">=</span><span class="pln" style="font-family:inherit;color:#000000;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;"> </span><span class="pun" style="font-family:inherit;color:#66660;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">[(</span><span class="str" style="font-family:inherit;color:#0880;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">'Content-type'</span><span class="pun" style="font-family:inherit;color:#66660;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">,</span><span class="pln" style="font-family:inherit;color:#000000;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;"> </span><span class="str" style="font-family:inherit;color:#0880;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">'text/plain'</span><span class="pun" style="font-family:inherit;color:#66660;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">)]</span><span class="pln" style="font-family:inherit;color:#000000;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;"> start_response</span><span class="pun" style="font-family:inherit;color:#66660;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">(</span><span class="pln" style="font-family:inherit;color:#000000;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">status</span><span class="pun" style="font-family:inherit;color:#66660;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">,</span><span class="pln" style="font-family:inherit;color:#000000;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;"> response_headers</span><span class="pun" style="font-family:inherit;color:#66660;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">)</span><span class="pln" style="font-family:inherit;color:#000000;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;"> </span><span class="kwd" style="font-family:inherit;color:#0088;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">return</span><span class="pln" style="font-family:inherit;color:#000000;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;"> </span><span class="pun" style="font-family:inherit;color:#66660;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">[</span><span class="pln" style="font-family:inherit;color:#000000;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">u</span><span class="str" style="font-family:inherit;color:#0880;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">"This is hello wsgi app"</span><span class="pun" style="font-family:inherit;color:#66660;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">.</span><span class="pln" style="font-family:inherit;color:#000000;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">encode</span><span class="pun" style="font-family:inherit;color:#66660;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">(</span><span class="str" style="font-family:inherit;color:#0880;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">'utf8'</span><span class="pun" style="font-family:inherit;color:#66660;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">)]</span><span class="pln" style="font-family:inherit;color:#000000;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;"> httpd </span><span class="pun" style="font-family:inherit;color:#66660;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">=</span><span class="pln" style="font-family:inherit;color:#000000;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;"> make_server</span><span class="pun" style="font-family:inherit;color:#66660;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">(</span><span class="str" style="font-family:inherit;color:#0880;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">''</span><span class="pun" style="font-family:inherit;color:#66660;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">,</span><span class="pln" style="font-family:inherit;color:#000000;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;"> </span><span class="lit" style="font-family:inherit;color:#06666;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">8000</span><span class="pun" style="font-family:inherit;color:#66660;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">,</span><span class="pln" style="font-family:inherit;color:#000000;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;"> simple_app</span><span class="pun" style="font-family:inherit;color:#66660;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">)</span><span class="pln" style="font-family:inherit;color:#000000;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;"> </span><span class="kwd" style="font-family:inherit;color:#0088;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">print</span><span class="pln" style="font-family:inherit;color:#000000;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;"> </span><span class="str" style="font-family:inherit;color:#0880;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">"Serving on port 8000..."</span><span class="pln" style="font-family:inherit;color:#000000;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;"> httpd</span><span class="pun" style="font-family:inherit;color:#66660;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">.</span><span class="pln" style="font-family:inherit;color:#000000;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">serve_forever</span><span class="pun" style="font-family:inherit;color:#66660;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">()</span>
访问http://127.0.0.1:8000 就能看到效果了。
此外,上面讲到了wsgi app只要是一个callable对象就可以了,因此不一定要是函数,一个实现了__call__方法的实例也可以,示例代码如下:
<span class="kwd" style="font-family:inherit;color:#0088;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">from</span><span class="pln" style="font-family:inherit;color:#000000;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;"> wsgiref</span><span class="pun" style="font-family:inherit;color:#66660;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">.</span><span class="pln" style="font-family:inherit;color:#000000;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">simple_server </span><span class="kwd" style="font-family:inherit;color:#0088;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">import</span><span class="pln" style="font-family:inherit;color:#000000;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;"> make_server </span><span class="kwd" style="font-family:inherit;color:#0088;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">class</span><span class="pln" style="font-family:inherit;color:#000000;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;"> </span><span class="typ" style="font-family:inherit;color:#66066;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">AppClass</span><span class="pun" style="font-family:inherit;color:#66660;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">:</span><span class="pln" style="font-family:inherit;color:#000000;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;"> </span><span class="kwd" style="font-family:inherit;color:#0088;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">def</span><span class="pln" style="font-family:inherit;color:#000000;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;"> __call__</span><span class="pun" style="font-family:inherit;color:#66660;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">(</span><span class="kwd" style="font-family:inherit;color:#0088;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">self</span><span class="pun" style="font-family:inherit;color:#66660;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">,</span><span class="pln" style="font-family:inherit;color:#000000;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">environ</span><span class="pun" style="font-family:inherit;color:#66660;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">,</span><span class="pln" style="font-family:inherit;color:#000000;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;"> start_response</span><span class="pun" style="font-family:inherit;color:#66660;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">):</span><span class="pln" style="font-family:inherit;color:#000000;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;"> status </span><span class="pun" style="font-family:inherit;color:#66660;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">=</span><span class="pln" style="font-family:inherit;color:#000000;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;"> </span><span class="str" style="font-family:inherit;color:#0880;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">'200 OK'</span><span class="pln" style="font-family:inherit;color:#000000;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;"> response_headers </span><span class="pun" style="font-family:inherit;color:#66660;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">=</span><span class="pln" style="font-family:inherit;color:#000000;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;"> </span><span class="pun" style="font-family:inherit;color:#66660;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">[(</span><span class="str" style="font-family:inherit;color:#0880;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">'Content-type'</span><span class="pun" style="font-family:inherit;color:#66660;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">,</span><span class="pln" style="font-family:inherit;color:#000000;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;"> </span><span class="str" style="font-family:inherit;color:#0880;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">'text/plain'</span><span class="pun" style="font-family:inherit;color:#66660;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">)]</span><span class="pln" style="font-family:inherit;color:#000000;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;"> start_response</span><span class="pun" style="font-family:inherit;color:#66660;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">(</span><span class="pln" style="font-family:inherit;color:#000000;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">status</span><span class="pun" style="font-family:inherit;color:#66660;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">,</span><span class="pln" style="font-family:inherit;color:#000000;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;"> response_headers</span><span class="pun" style="font-family:inherit;color:#66660;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">)</span><span class="pln" style="font-family:inherit;color:#000000;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;"> </span><span class="kwd" style="font-family:inherit;color:#0088;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">return</span><span class="pln" style="font-family:inherit;color:#000000;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;"> </span><span class="pun" style="font-family:inherit;color:#66660;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">[</span><span class="str" style="font-family:inherit;color:#0880;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">"hello world!"</span><span class="pun" style="font-family:inherit;color:#66660;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">]</span><span class="pln" style="font-family:inherit;color:#000000;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;"> app </span><span class="pun" style="font-family:inherit;color:#66660;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">=</span><span class="pln" style="font-family:inherit;color:#000000;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;"> </span><span class="typ" style="font-family:inherit;color:#66066;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">AppClass</span><span class="pun" style="font-family:inherit;color:#66660;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">()</span><span class="pln" style="font-family:inherit;color:#000000;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;"> httpd </span><span class="pun" style="font-family:inherit;color:#66660;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">=</span><span class="pln" style="font-family:inherit;color:#000000;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;"> make_server</span><span class="pun" style="font-family:inherit;color:#66660;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">(</span><span class="str" style="font-family:inherit;color:#0880;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">''</span><span class="pun" style="font-family:inherit;color:#66660;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">,</span><span class="pln" style="font-family:inherit;color:#000000;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;"> </span><span class="lit" style="font-family:inherit;color:#06666;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">8000</span><span class="pun" style="font-family:inherit;color:#66660;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">,</span><span class="pln" style="font-family:inherit;color:#000000;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;"> app</span><span class="pun" style="font-family:inherit;color:#66660;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">)</span><span class="pln" style="font-family:inherit;color:#000000;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;"> </span><span class="kwd" style="font-family:inherit;color:#0088;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">print</span><span class="pln" style="font-family:inherit;color:#000000;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;"> </span><span class="str" style="font-family:inherit;color:#0880;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">"Serving on port 8000..."</span><span class="pln" style="font-family:inherit;color:#000000;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;"> httpd</span><span class="pun" style="font-family:inherit;color:#66660;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">.</span><span class="pln" style="font-family:inherit;color:#000000;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">serve_forever</span><span class="pun" style="font-family:inherit;color:#66660;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">()</span>
WSGI MiddleWare
<span class="kwd" style="font-family:inherit;color:#0088;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;"> from</span><span class="pln" style="font-family:inherit;color:#000000;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;"> wsgiref</span><span class="pun" style="font-family:inherit;color:#66660;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">.</span><span class="pln" style="font-family:inherit;color:#000000;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">simple_server </span><span class="kwd" style="font-family:inherit;color:#0088;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">import</span><span class="pln" style="font-family:inherit;color:#000000;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;"> make_server URL_PATTERNS</span><span class="pun" style="font-family:inherit;color:#66660;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">=</span><span class="pln" style="font-family:inherit;color:#000000;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;"> </span><span class="pun" style="font-family:inherit;color:#66660;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">(</span><span class="pln" style="font-family:inherit;color:#000000;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;"> </span><span class="pun" style="font-family:inherit;color:#66660;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">(</span><span class="str" style="font-family:inherit;color:#0880;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">'hi/'</span><span class="pun" style="font-family:inherit;color:#66660;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">,</span><span class="str" style="font-family:inherit;color:#0880;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">'say_hi'</span><span class="pun" style="font-family:inherit;color:#66660;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">),</span><span class="pln" style="font-family:inherit;color:#000000;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;"> </span><span class="pun" style="font-family:inherit;color:#66660;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">(</span><span class="str" style="font-family:inherit;color:#0880;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">'hello/'</span><span class="pun" style="font-family:inherit;color:#66660;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">,</span><span class="str" style="font-family:inherit;color:#0880;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">'say_hello'</span><span class="pun" style="font-family:inherit;color:#66660;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">),</span><span class="pln" style="font-family:inherit;color:#000000;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;"> </span><span class="pun" style="font-family:inherit;color:#66660;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">)</span><span class="pln" style="font-family:inherit;color:#000000;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;"> </span><span class="kwd" style="font-family:inherit;color:#0088;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">class</span><span class="pln" style="font-family:inherit;color:#000000;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;"> </span><span class="typ" style="font-family:inherit;color:#66066;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">Dispatcher</span><span class="pun" style="font-family:inherit;color:#66660;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">(</span><span class="kwd" style="font-family:inherit;color:#0088;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">object</span><span class="pun" style="font-family:inherit;color:#66660;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">):</span><span class="pln" style="font-family:inherit;color:#000000;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;"> </span><span class="kwd" style="font-family:inherit;color:#0088;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">def</span><span class="pln" style="font-family:inherit;color:#000000;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;"> _match</span><span class="pun" style="font-family:inherit;color:#66660;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">(</span><span class="kwd" style="font-family:inherit;color:#0088;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">self</span><span class="pun" style="font-family:inherit;color:#66660;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">,</span><span class="pln" style="font-family:inherit;color:#000000;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">path</span><span class="pun" style="font-family:inherit;color:#66660;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">):</span><span class="pln" style="font-family:inherit;color:#000000;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;"> path </span><span class="pun" style="font-family:inherit;color:#66660;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">=</span><span class="pln" style="font-family:inherit;color:#000000;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;"> path</span><span class="pun" style="font-family:inherit;color:#66660;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">.</span><span class="pln" style="font-family:inherit;color:#000000;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">split</span><span class="pun" style="font-family:inherit;color:#66660;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">(</span><span class="str" style="font-family:inherit;color:#0880;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">'/'</span><span class="pun" style="font-family:inherit;color:#66660;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">)[</span><span class="lit" style="font-family:inherit;color:#06666;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">1</span><span class="pun" style="font-family:inherit;color:#66660;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">]</span><span class="pln" style="font-family:inherit;color:#000000;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;"> </span><span class="kwd" style="font-family:inherit;color:#0088;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">for</span><span class="pln" style="font-family:inherit;color:#000000;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;"> url</span><span class="pun" style="font-family:inherit;color:#66660;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">,</span><span class="pln" style="font-family:inherit;color:#000000;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">app </span><span class="kwd" style="font-family:inherit;color:#0088;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">in</span><span class="pln" style="font-family:inherit;color:#000000;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;"> URL_PATTERNS</span><span class="pun" style="font-family:inherit;color:#66660;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">:</span><span class="pln" style="font-family:inherit;color:#000000;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;"> </span><span class="kwd" style="font-family:inherit;color:#0088;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">if</span><span class="pln" style="font-family:inherit;color:#000000;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;"> path </span><span class="kwd" style="font-family:inherit;color:#0088;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">in</span><span class="pln" style="font-family:inherit;color:#000000;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;"> url</span><span class="pun" style="font-family:inherit;color:#66660;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">:</span><span class="pln" style="font-family:inherit;color:#000000;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;"> </span><span class="kwd" style="font-family:inherit;color:#0088;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">return</span><span class="pln" style="font-family:inherit;color:#000000;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;"> app </span><span class="kwd" style="font-family:inherit;color:#0088;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">def</span><span class="pln" style="font-family:inherit;color:#000000;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;"> __call__</span><span class="pun" style="font-family:inherit;color:#66660;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">(</span><span class="kwd" style="font-family:inherit;color:#0088;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">self</span><span class="pun" style="font-family:inherit;color:#66660;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">,</span><span class="pln" style="font-family:inherit;color:#000000;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">environ</span><span class="pun" style="font-family:inherit;color:#66660;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">,</span><span class="pln" style="font-family:inherit;color:#000000;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;"> start_response</span><span class="pun" style="font-family:inherit;color:#66660;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">):</span><span class="pln" style="font-family:inherit;color:#000000;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;"> path </span><span class="pun" style="font-family:inherit;color:#66660;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">=</span><span class="pln" style="font-family:inherit;color:#000000;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;"> environ</span><span class="pun" style="font-family:inherit;color:#66660;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">.</span><span class="kwd" style="font-family:inherit;color:#0088;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">get</span><span class="pun" style="font-family:inherit;color:#66660;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">(</span><span class="str" style="font-family:inherit;color:#0880;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">'PATH_INFO'</span><span class="pun" style="font-family:inherit;color:#66660;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">,</span><span class="str" style="font-family:inherit;color:#0880;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">'/'</span><span class="pun" style="font-family:inherit;color:#66660;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">)</span><span class="pln" style="font-family:inherit;color:#000000;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;"> app </span><span class="pun" style="font-family:inherit;color:#66660;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">=</span><span class="pln" style="font-family:inherit;color:#000000;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;"> </span><span class="kwd" style="font-family:inherit;color:#0088;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">self</span><span class="pun" style="font-family:inherit;color:#66660;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">.</span><span class="pln" style="font-family:inherit;color:#000000;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">_match</span><span class="pun" style="font-family:inherit;color:#66660;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">(</span><span class="pln" style="font-family:inherit;color:#000000;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">path</span><span class="pun" style="font-family:inherit;color:#66660;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">)</span><span class="pln" style="font-family:inherit;color:#000000;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;"> </span><span class="kwd" style="font-family:inherit;color:#0088;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">if</span><span class="pln" style="font-family:inherit;color:#000000;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;"> app </span><span class="pun" style="font-family:inherit;color:#66660;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">:</span><span class="pln" style="font-family:inherit;color:#000000;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;"> app </span><span class="pun" style="font-family:inherit;color:#66660;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">=</span><span class="pln" style="font-family:inherit;color:#000000;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;"> globals</span><span class="pun" style="font-family:inherit;color:#66660;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">()[</span><span class="pln" style="font-family:inherit;color:#000000;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">app</span><span class="pun" style="font-family:inherit;color:#66660;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">]</span><span class="pln" style="font-family:inherit;color:#000000;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;"> </span><span class="kwd" style="font-family:inherit;color:#0088;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">return</span><span class="pln" style="font-family:inherit;color:#000000;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;"> app</span><span class="pun" style="font-family:inherit;color:#66660;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">(</span><span class="pln" style="font-family:inherit;color:#000000;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">environ</span><span class="pun" style="font-family:inherit;color:#66660;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">,</span><span class="pln" style="font-family:inherit;color:#000000;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;"> start_response</span><span class="pun" style="font-family:inherit;color:#66660;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">)</span><span class="pln" style="font-family:inherit;color:#000000;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;"> </span><span class="kwd" style="font-family:inherit;color:#0088;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">else</span><span class="pun" style="font-family:inherit;color:#66660;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">:</span><span class="pln" style="font-family:inherit;color:#000000;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;"> start_response</span><span class="pun" style="font-family:inherit;color:#66660;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">(</span><span class="str" style="font-family:inherit;color:#0880;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">"404 NOT FOUND"</span><span class="pun" style="font-family:inherit;color:#66660;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">,[(</span><span class="str" style="font-family:inherit;color:#0880;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">'Content-type'</span><span class="pun" style="font-family:inherit;color:#66660;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">,</span><span class="pln" style="font-family:inherit;color:#000000;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;"> </span><span class="str" style="font-family:inherit;color:#0880;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">'text/plain'</span><span class="pun" style="font-family:inherit;color:#66660;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">)])</span><span class="pln" style="font-family:inherit;color:#000000;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;"> </span><span class="kwd" style="font-family:inherit;color:#0088;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">return</span><span class="pln" style="font-family:inherit;color:#000000;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;"> </span><span class="pun" style="font-family:inherit;color:#66660;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">[</span><span class="str" style="font-family:inherit;color:#0880;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">"Page dose not exists!"</span><span class="pun" style="font-family:inherit;color:#66660;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">]</span><span class="pln" style="font-family:inherit;color:#000000;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;"> </span><span class="kwd" style="font-family:inherit;color:#0088;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">def</span><span class="pln" style="font-family:inherit;color:#000000;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;"> say_hi</span><span class="pun" style="font-family:inherit;color:#66660;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">(</span><span class="pln" style="font-family:inherit;color:#000000;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">environ</span><span class="pun" style="font-family:inherit;color:#66660;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">,</span><span class="pln" style="font-family:inherit;color:#000000;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;"> start_response</span><span class="pun" style="font-family:inherit;color:#66660;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">):</span><span class="pln" style="font-family:inherit;color:#000000;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;"> start_response</span><span class="pun" style="font-family:inherit;color:#66660;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">(</span><span class="str" style="font-family:inherit;color:#0880;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">"200 OK"</span><span class="pun" style="font-family:inherit;color:#66660;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">,[(</span><span class="str" style="font-family:inherit;color:#0880;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">'Content-type'</span><span class="pun" style="font-family:inherit;color:#66660;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">,</span><span class="pln" style="font-family:inherit;color:#000000;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;"> </span><span class="str" style="font-family:inherit;color:#0880;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">'text/html'</span><span class="pun" style="font-family:inherit;color:#66660;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">)])</span><span class="pln" style="font-family:inherit;color:#000000;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;"> </span><span class="kwd" style="font-family:inherit;color:#0088;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">return</span><span class="pln" style="font-family:inherit;color:#000000;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;"> </span><span class="pun" style="font-family:inherit;color:#66660;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">[</span><span class="str" style="font-family:inherit;color:#0880;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">"kenshin say hi to you!"</span><span class="pun" style="font-family:inherit;color:#66660;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">]</span><span class="pln" style="font-family:inherit;color:#000000;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;"> </span><span class="kwd" style="font-family:inherit;color:#0088;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">def</span><span class="pln" style="font-family:inherit;color:#000000;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;"> say_hello</span><span class="pun" style="font-family:inherit;color:#66660;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">(</span><span class="pln" style="font-family:inherit;color:#000000;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">environ</span><span class="pun" style="font-family:inherit;color:#66660;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">,</span><span class="pln" style="font-family:inherit;color:#000000;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;"> start_response</span><span class="pun" style="font-family:inherit;color:#66660;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">):</span><span class="pln" style="font-family:inherit;color:#000000;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;"> start_response</span><span class="pun" style="font-family:inherit;color:#66660;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">(</span><span class="str" style="font-family:inherit;color:#0880;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">"200 OK"</span><span class="pun" style="font-family:inherit;color:#66660;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">,[(</span><span class="str" style="font-family:inherit;color:#0880;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">'Content-type'</span><span class="pun" style="font-family:inherit;color:#66660;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">,</span><span class="pln" style="font-family:inherit;color:#000000;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;"> </span><span class="str" style="font-family:inherit;color:#0880;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">'text/html'</span><span class="pun" style="font-family:inherit;color:#66660;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">)])</span><span class="pln" style="font-family:inherit;color:#000000;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;"> </span><span class="kwd" style="font-family:inherit;color:#0088;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">return</span><span class="pln" style="font-family:inherit;color:#000000;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;"> </span><span class="pun" style="font-family:inherit;color:#66660;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">[</span><span class="str" style="font-family:inherit;color:#0880;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">"kenshin say hello to you!"</span><span class="pun" style="font-family:inherit;color:#66660;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">]</span><span class="pln" style="font-family:inherit;color:#000000;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;"> app </span><span class="pun" style="font-family:inherit;color:#66660;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">=</span><span class="pln" style="font-family:inherit;color:#000000;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;"> </span><span class="typ" style="font-family:inherit;color:#66066;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">Dispatcher</span><span class="pun" style="font-family:inherit;color:#66660;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">()</span><span class="pln" style="font-family:inherit;color:#000000;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;"> httpd </span><span class="pun" style="font-family:inherit;color:#66660;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">=</span><span class="pln" style="font-family:inherit;color:#000000;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;"> make_server</span><span class="pun" style="font-family:inherit;color:#66660;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">(</span><span class="str" style="font-family:inherit;color:#0880;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">''</span><span class="pun" style="font-family:inherit;color:#66660;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">,</span><span class="pln" style="font-family:inherit;color:#000000;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;"> </span><span class="lit" style="font-family:inherit;color:#06666;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">8000</span><span class="pun" style="font-family:inherit;color:#66660;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">,</span><span class="pln" style="font-family:inherit;color:#000000;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;"> app</span><span class="pun" style="font-family:inherit;color:#66660;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">)</span><span class="pln" style="font-family:inherit;color:#000000;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;"> </span><span class="kwd" style="font-family:inherit;color:#0088;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">print</span><span class="pln" style="font-family:inherit;color:#000000;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;"> </span><span class="str" style="font-family:inherit;color:#0880;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">"Serving on port 8000..."</span><span class="pln" style="font-family:inherit;color:#000000;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;"> httpd</span><span class="pun" style="font-family:inherit;color:#66660;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">.</span><span class="pln" style="font-family:inherit;color:#000000;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">serve_forever</span><span class="pun" style="font-family:inherit;color:#66660;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">()</span>
上面的例子可以看出来,middleware 包装之后,一个简单wsgi app就有了URL dispatch功能。然后我还可以在这个app外面再加上其它的middleware来包装它,例如加一个权限认证的middleware:
<span class="kwd" style="font-family:inherit;color:#0088;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;"> class</span><span class="pln" style="font-family:inherit;color:#000000;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;"> </span><span class="typ" style="font-family:inherit;color:#66066;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">Auth</span><span class="pun" style="font-family:inherit;color:#66660;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">(</span><span class="kwd" style="font-family:inherit;color:#0088;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">object</span><span class="pun" style="font-family:inherit;color:#66660;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">):</span><span class="pln" style="font-family:inherit;color:#000000;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;"> </span><span class="kwd" style="font-family:inherit;color:#0088;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">def</span><span class="pln" style="font-family:inherit;color:#000000;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;"> __init__</span><span class="pun" style="font-family:inherit;color:#66660;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">(</span><span class="kwd" style="font-family:inherit;color:#0088;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">self</span><span class="pun" style="font-family:inherit;color:#66660;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">,</span><span class="pln" style="font-family:inherit;color:#000000;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">app</span><span class="pun" style="font-family:inherit;color:#66660;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">):</span><span class="pln" style="font-family:inherit;color:#000000;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;"> </span><span class="kwd" style="font-family:inherit;color:#0088;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">self</span><span class="pun" style="font-family:inherit;color:#66660;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">.</span><span class="pln" style="font-family:inherit;color:#000000;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">app </span><span class="pun" style="font-family:inherit;color:#66660;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">=</span><span class="pln" style="font-family:inherit;color:#000000;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;"> app </span><span class="kwd" style="font-family:inherit;color:#0088;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">def</span><span class="pln" style="font-family:inherit;color:#000000;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;"> __call__</span><span class="pun" style="font-family:inherit;color:#66660;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">(</span><span class="kwd" style="font-family:inherit;color:#0088;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">self</span><span class="pun" style="font-family:inherit;color:#66660;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">,</span><span class="pln" style="font-family:inherit;color:#000000;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">environ</span><span class="pun" style="font-family:inherit;color:#66660;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">,</span><span class="pln" style="font-family:inherit;color:#000000;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;"> start_response</span><span class="pun" style="font-family:inherit;color:#66660;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">):</span><span class="pln" style="font-family:inherit;color:#000000;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;"> </span><span class="com" style="font-family:inherit;color:#8800;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">#TODO</span><span class="pln" style="font-family:inherit;color:#000000;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;"> </span><span class="kwd" style="font-family:inherit;color:#0088;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">return</span><span class="pln" style="font-family:inherit;color:#000000;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;"> </span><span class="kwd" style="font-family:inherit;color:#0088;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">self</span><span class="pun" style="font-family:inherit;color:#66660;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">.</span><span class="pln" style="font-family:inherit;color:#000000;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">app</span><span class="pun" style="font-family:inherit;color:#66660;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">(</span><span class="pln" style="font-family:inherit;color:#000000;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">environ</span><span class="pun" style="font-family:inherit;color:#66660;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">,</span><span class="pln" style="font-family:inherit;color:#000000;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;"> start_response</span><span class="pun" style="font-family:inherit;color:#66660;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">)</span><span class="pln" style="font-family:inherit;color:#000000;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;"> app </span><span class="pun" style="font-family:inherit;color:#66660;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">=</span><span class="pln" style="font-family:inherit;color:#000000;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;"> </span><span class="typ" style="font-family:inherit;color:#66066;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">Dispatcher</span><span class="pun" style="font-family:inherit;color:#66660;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">()</span><span class="pln" style="font-family:inherit;color:#000000;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;"> auth_app </span><span class="pun" style="font-family:inherit;color:#66660;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">=</span><span class="pln" style="font-family:inherit;color:#000000;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;"> </span><span class="typ" style="font-family:inherit;color:#66066;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">Auth</span><span class="pun" style="font-family:inherit;color:#66660;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">(</span><span class="pln" style="font-family:inherit;color:#000000;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">app</span><span class="pun" style="font-family:inherit;color:#66660;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">)</span><span class="pln" style="font-family:inherit;color:#000000;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;"> httpd </span><span class="pun" style="font-family:inherit;color:#66660;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">=</span><span class="pln" style="font-family:inherit;color:#000000;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;"> make_server</span><span class="pun" style="font-family:inherit;color:#66660;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">(</span><span class="str" style="font-family:inherit;color:#0880;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">''</span><span class="pun" style="font-family:inherit;color:#66660;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">,</span><span class="pln" style="font-family:inherit;color:#000000;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;"> </span><span class="lit" style="font-family:inherit;color:#06666;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">8000</span><span class="pun" style="font-family:inherit;color:#66660;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">,</span><span class="pln" style="font-family:inherit;color:#000000;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;"> auth_app</span><span class="pun" style="font-family:inherit;color:#66660;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">)</span><span class="pln" style="font-family:inherit;color:#000000;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;"> </span><span class="kwd" style="font-family:inherit;color:#0088;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">print</span><span class="pln" style="font-family:inherit;color:#000000;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;"> </span><span class="str" style="font-family:inherit;color:#0880;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">"Serving on port 8000..."</span><span class="pln" style="font-family:inherit;color:#000000;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;"> httpd</span><span class="pun" style="font-family:inherit;color:#66660;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">.</span><span class="pln" style="font-family:inherit;color:#000000;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">serve_forever</span><span class="pun" style="font-family:inherit;color:#66660;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">()</span>
经过这些middleware的包装,已经有点框架的感觉了。其实基于wsgi的框架,例如paste,pylons就是这样通过一层层middleware组合起来的。只是一个成熟的框架,这样的middleware会有很多,例如:
<span class="kwd" style="font-family:inherit;color:#0088;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;"> def</span><span class="pln" style="font-family:inherit;color:#000000;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;"> configure</span><span class="pun" style="font-family:inherit;color:#66660;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">(</span><span class="pln" style="font-family:inherit;color:#000000;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">app</span><span class="pun" style="font-family:inherit;color:#66660;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">):</span><span class="pln" style="font-family:inherit;color:#000000;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;"> </span><span class="kwd" style="font-family:inherit;color:#0088;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">return</span><span class="pln" style="font-family:inherit;color:#000000;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;"> </span><span class="typ" style="font-family:inherit;color:#66066;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">ErrorHandlerMiddleware</span><span class="pun" style="font-family:inherit;color:#66660;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">(</span><span class="pln" style="font-family:inherit;color:#000000;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;"> </span><span class="typ" style="font-family:inherit;color:#66066;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">SessionMiddleware</span><span class="pun" style="font-family:inherit;color:#66660;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">(</span><span class="pln" style="font-family:inherit;color:#000000;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;"> </span><span class="typ" style="font-family:inherit;color:#66066;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">IdentificationMiddleware</span><span class="pun" style="font-family:inherit;color:#66660;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">(</span><span class="pln" style="font-family:inherit;color:#000000;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;"> </span><span class="typ" style="font-family:inherit;color:#66066;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">AuthenticationMiddleware</span><span class="pun" style="font-family:inherit;color:#66660;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">(</span><span class="pln" style="font-family:inherit;color:#000000;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;"> </span><span class="typ" style="font-family:inherit;color:#66066;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">UrlParserMiddleware</span><span class="pun" style="font-family:inherit;color:#66660;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">(</span><span class="pln" style="font-family:inherit;color:#000000;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">app</span><span class="pun" style="font-family:inherit;color:#66660;border:0px; font-style:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline;">))))))</span>
只要这些Middleware符合wsgi规范,甚至还可以在各个框架之间组合重用。例如pylons的认证Middleware可以直接被TurboGears拿去使用。
好了,各个部分都写完了,以后有时间再看看pylons的代码,相信又会对wsgi有很多新的理解。