WSGI应用接口被实现为可调用的对象:一个函数,方法,类或者一个带有__call__方法的实例。这个可调用对象
1. 必须接受两个位置参数:
一个包含类似于CGI变量的字典;
一个回调函数,这个回调函数将被应用用来发送HTTP状态码/消息和HTTP头给服务器。
2. 必须向服务器返回以可迭代对象包含的字符串的应答体。
程序骨架:
# This is our application object . it could hava any name, except when using mod_wsgi where it must be "application"
def application( # It accepts two arguments: environ points to a dictionary containing CGI like environment variables which is filled by the server for each received request #from the client
environ,
# start_response is a callback function supplied by the server which will be used to send the HTTP status and headers to the server
start_response)
# build the response body possibility using the environ dictionary
response_body = 'The request method was %s' environ['REQUEST_METHOD']
# HTTP response code and message
status = '200 OK'
# These are HTTP headers expected by the client.
# They must be wrapped as a list of tupled pairs:
# [(Header name, Header value)].
response_headers = [('Content-Type', 'txt/plain'), ('Content-Length',str(len(response_body)))]
# send them to the server using the supplied function
start_response(status, response_headers)
# return the response body, Notice it is wrapped in a list although it could be any iterable
return [reponse_body]
由于缺少服务器实例化步骤,这个代码不能运行。