James Gardner: Home > Work > Code > WSGIPack > 0.1.0 > Manual

WSGIPack v0.1.0 documentation


If you are calling a WSGI application you will need to provide environ and start_response() objects as well as pass the result back to the HTTP service. The WSGIService provides objects which handle this for you:

The WSGI environ object containing all the request information
The WSGI start_response() callable which sets the status and headers
A list of strings or an iterator which when iterated over, produces the response you wish to send. This must be set exactly once for the result to actually be passed on to Flows. If you set it more than once the previous data will be lost, if you don’t set it at all, any data written to the object returned from start_response() will not be passed on to Flows.

The WSGI service then lets you call WSGI applications like this:

flow.wsgi.result = application(

The WSGI application will be called and the correct data will be set up for flow.http.response.status, flow.http.response.headers and flow.http.response.body.

The flow.wsgi.environ object has a flows.flow key representing the current flow so that the flow can be accessed from within the WSGI application.

Data written to the writable object returned from start_response() is buffered, not streamed, but the result returned from the WSGI application is streamed.

Here’s an example demonstrating all the features:

import time

def hello(environ, start_response):
    write = start_response('200 OK', [('Content-type', 'text/html')])
    def result():
        yield ' World! Environ keys: '
        for name in environ['flows.flow'].keys():
            yield name + ', '
    return result()

def on_action_wsgi(flow):
    flow.wsgi.result = hello(flow.wsgi.environ, flow.wsgi.start_response)
James Gardner: Home > Work > Code > WSGIPack > 0.1.0 > Manual