# the application object. 可以使用其他名字,
# 但是在使用mod_wsgi 时必须为 "application"
def application( environ, start_response):
# 函数接受两个参数:
# environ :包含有CGI 式环境变量的字典,由server负责提供内容
# start_response:由server提供的回调函数,其作用是将状态码和响应头返回给server
# 构造响应体,以可迭代字符串形式封装
response_body = 'The request method was %s' % environ['REQUEST_METHOD']
# HTTP 响应码及消息
status = '200 OK'
# 提供给客户端的响应头.
# 封装成list of tuple pairs 的形式:
# 格式要求:[(Header name, Header value)].
response_headers = [('Content-Type', 'text/plain'),
('Content-Length', str(len(response_body)))]
# 将响应码/消息及响应头通过传入的start_reponse回调函数返回给server
start_response(status, response_headers)
# 响应体作为返回值返回
# 注意这里被封装到了list中.
return [response_body]
from wsgiref.simple_server import make_server
srv = make_server('localhost', 8080, hello_world)
srv.serve_forever()
import re
# 这是一个标准的application object
def index(environ, start_response):
start_response('200 OK', [('Content-Type', 'text/html')])
return ['index page']
# 这是一个标准的application object
def hello(environ, start_response):
start_response('200 OK', [('Content-Type', 'text/html')])
return ['hello page']
# 这是一个标准的application object
def not_found(environ, start_response):
start_response('404 NOT FOUND', [('Content-Type', 'text/plain')])
return ['Not Found Page']
# map urls to functions
urls = [
(r'^$', index),
(r'hello/?$', hello)
]
# 这是一个middleware
# 根据不同的route返回不同的application object
def application(environ, start_response):
path = environ.get('PATH_INFO', '').lstrip('/')
for regex, callback in urls:
match = re.search(regex, path)
if match is not None:
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有