tornado.wsgi — Interoperability with other Python frameworks and servers

WSGI support for the Tornado web framework.

WSGI is the Python standard for web servers, and allows for interoperability between Tornado and other Python web frameworks and servers. This module provides WSGI support in two ways:

  • WSGIApplication is a version of tornado.web.Application that can run inside a WSGI server. This is useful for running a Tornado app on another HTTP server, such as Google App Engine. See the WSGIApplication class documentation for limitations that apply.
  • WSGIContainer lets you run other WSGI applications and frameworks on the Tornado HTTP server. For example, with this class you can mix Django and Tornado handlers in a single server.


class tornado.wsgi.WSGIApplication(handlers=None, default_host='', **settings)[source]

A WSGI equivalent of tornado.web.Application.

WSGIApplication is very similar to web.Application, except no asynchronous methods are supported (since WSGI does not support non-blocking requests properly). If you call self.flush() or other asynchronous methods in your request handlers running in a WSGIApplication, we throw an exception.

Example usage:

import tornado.web
import tornado.wsgi
import wsgiref.simple_server

class MainHandler(tornado.web.RequestHandler):
    def get(self):
        self.write("Hello, world")

if __name__ == "__main__":
    application = tornado.wsgi.WSGIApplication([
        (r"/", MainHandler),
    server = wsgiref.simple_server.make_server('', 8888, application)

See the ‘appengine’ demo for an example of using this module to run a Tornado app on Google AppEngine.

Since no asynchronous methods are available for WSGI applications, the httpclient and auth modules are both not available for WSGI applications. We support the same interface, but handlers running in a WSGIApplication do not support flush() or asynchronous methods.

class tornado.wsgi.HTTPRequest(environ)[source]

Mimics tornado.httpserver.HTTPRequest for WSGI applications.

Parses the given WSGI environ to construct the request.


Returns True if this request supports HTTP/1.1 semantics


A dictionary of Cookie.Morsel objects.


Reconstructs the full URL for this request.


Returns the amount of time it took for this request to execute.


class tornado.wsgi.WSGIContainer(wsgi_application)[source]

Makes a WSGI-compatible function runnable on Tornado’s HTTP server.

Wrap a WSGI function in a WSGIContainer and pass it to HTTPServer to run it. For example:

def simple_app(environ, start_response):
    status = "200 OK"
    response_headers = [("Content-type", "text/plain")]
    start_response(status, response_headers)
    return ["Hello world!\n"]

container = tornado.wsgi.WSGIContainer(simple_app)
http_server = tornado.httpserver.HTTPServer(container)

This class is intended to let other frameworks (Django,, etc) run on the Tornado HTTP server and I/O loop.

The tornado.web.FallbackHandler class is often useful for mixing Tornado and WSGI apps in the same server. See for a complete example.

static environ(request)[source]

Converts a tornado.httpserver.HTTPRequest to a WSGI environment.

Table Of Contents

Previous topic

tornado.websocket — Bidirectional communication to the browser

Next topic


This Page