WEB开发网
开发学院软件开发Python 用 Python WSGI 混和并匹配 Web 组件 阅读

用 Python WSGI 混和并匹配 Web 组件

 2010-09-22 11:13:19 来源:WEB开发网   
核心提示: 清单 2(wsgireftest.py)是测试中间件的服务器代码,它使用包含非常简单的 WSGI 服务器的 wsgiref,用 Python WSGI 混和并匹配 Web 组件(6),这个模块将包括在 Python 2.5 标准库中, 清单 2(wsgireftest.py). 测试清单

清单 2(wsgireftest.py)是测试中间件的服务器代码。它使用包含非常简单的 WSGI 服务器的 wsgiref。这个模块将包括在 Python 2.5 标准库中。

清单 2(wsgireftest.py). 测试清单 1 的服务器代码

import sys
from wsgiref.simple_server import make_server
from safexhtml import safexhtml
XHTML = open('test.xhtml').read()
XHTML_IMT = "application/xhtml+xml"
HTML_IMT = "text/html"
PORT = 8000
def app(environ, start_response):
  print "using IMT", app.current_imt
  start_response('200 OK', [('Content-Type', app.current_imt)])
  #Swap the IMT used for response (alternate between XHTML_IMT and HTML_IMT)
  app.current_imt, app.other_imt = app.other_imt, app.current_imt
  return [XHTML]
app.current_imt=XHTML_IMT
app.other_imt=HTML_IMT
httpd = make_server('', PORT, safexhtml(app))
print 'Starting up HTTP server on port %i...'%PORT
# Respond to requests until process is killed
httpd.serve_forever()

清单 2 读取 清单 3(test.xhtml)给出的简单 XHTML 文件,并向它提供替代的媒体类型。对于第一次请求使用标准的 XHTML 媒体类型,对于第二次使用 HTML 媒体类型,第三次又使用 XHTML,如此循环。如果响应没有标成 XHTML,就保持响应不变,这个中间件功能留给读者作为练习。

清单 3(test.xhtml). 由清单 2 中的示例服务器使用的简单 XHTML 文件

上一页  1 2 3 4 5 6 7  下一页

Tags:Python WSGI 匹配

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接