### Navigation
- [index](# "General Index")
- [modules](# "Python Module Index") |
- [next](# "tornado.platform.asyncio — Bridge between asyncio and Tornado") |
- [previous](# "tornado.auth — Third-party login with OpenID and OAuth") |
- [Tornado 4.4.dev1 documentation](#) »
- [Integration with other services](#) »
# `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 interoperabilitybetween Tornado and other Python web frameworks and servers. This moduleprovides WSGI support in two ways:
- [`WSGIAdapter`](# "tornado.wsgi.WSGIAdapter") converts a [`tornado.web.Application`](# "tornado.web.Application") to the WSGI applicationinterface. This is useful for running a Tornado app on anotherHTTP server, such as Google App Engine. See the [`WSGIAdapter`](# "tornado.wsgi.WSGIAdapter") classdocumentation for limitations that apply.
- [`WSGIContainer`](# "tornado.wsgi.WSGIContainer") lets you run other WSGI applications and frameworks on theTornado HTTP server. For example, with this class you can mix Djangoand Tornado handlers in a single server.
### Running Tornado apps on WSGI servers
*class *`tornado.wsgi.``WSGIAdapter`(*application*)[[source]](#)
Converts a [`tornado.web.Application`](# "tornado.web.Application") instance into a WSGI application.
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.web.Application([
(r"/", MainHandler),
])
wsgi_app = tornado.wsgi.WSGIAdapter(application)
server = wsgiref.simple_server.make_server('', 8888, wsgi_app)
server.serve_forever()
~~~
See the [appengine demo](https://github.com/tornadoweb/tornado/tree/stable/demos/appengine) [https://github.com/tornadoweb/tornado/tree/stable/demos/appengine]for an example of using this module to run a Tornado app on GoogleApp Engine.
In WSGI mode asynchronous methods are not supported. This meansthat it is not possible to use [`AsyncHTTPClient`](# "tornado.httpclient.AsyncHTTPClient"), or the[`tornado.auth`](# "tornado.auth") or [`tornado.websocket`](# "tornado.websocket") modules.
New in version 4.0.
*class *`tornado.wsgi.``WSGIApplication`(*handlers=None*, *default_host=''*, *transforms=None*, ***settings*)[[source]](#)
A WSGI equivalent of [`tornado.web.Application`](# "tornado.web.Application").
Deprecated since version 4.0: Use a regular [`Application`](# "tornado.web.Application") and wrap it in [`WSGIAdapter`](# "tornado.wsgi.WSGIAdapter") instead.
### Running WSGI apps on Tornado servers
*class *`tornado.wsgi.``WSGIContainer`(*wsgi_application*)[[source]](#)
Makes a WSGI-compatible function runnable on Tornado's HTTP server.
Warning
WSGI is a *synchronous* interface, while Tornado's concurrency modelis based on single-threaded asynchronous execution. This means thatrunning a WSGI app with Tornado's [`WSGIContainer`](# "tornado.wsgi.WSGIContainer") is *less scalable*than running the same app in a multi-threaded WSGI server like`gunicorn` or `uwsgi`. Use [`WSGIContainer`](# "tornado.wsgi.WSGIContainer") only when there arebenefits to combining Tornado and WSGI in the same process thatoutweigh the reduced scalability.
Wrap a WSGI function in a [`WSGIContainer`](# "tornado.wsgi.WSGIContainer") and pass it to [`HTTPServer`](# "tornado.httpserver.HTTPServer") torun 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)
http_server.listen(8888)
tornado.ioloop.IOLoop.current().start()
~~~
This class is intended to let other frameworks (Django, web.py, etc)run on the Tornado HTTP server and I/O loop.
The [`tornado.web.FallbackHandler`](# "tornado.web.FallbackHandler") class is often useful for mixingTornado and WSGI apps in the same server. See[https://github.com/bdarnell/django-tornado-demo](https://github.com/bdarnell/django-tornado-demo) for a complete example.
*static *`environ`(*request*)[[source]](#)
Converts a [`tornado.httputil.HTTPServerRequest`](# "tornado.httputil.HTTPServerRequest") to a WSGI environment.
© Copyright 2009-2016, The Tornado Authors. Created using [Sphinx](http://sphinx-doc.org/) 1.3.5.
- User's guide
- Introduction
- Asynchronous and non-Blocking I/O
- Coroutines
- Queue example - a concurrent web spider
- Structure of a Tornado web application
- Templates and UI
- Authentication and security
- Running and deploying
- Web framework
- tornado.web — RequestHandler and Application classes
- tornado.template — Flexible output generation
- tornado.escape — Escaping and string manipulation
- tornado.locale — Internationalization support
- tornado.websocket — Bidirectional communication to the browser
- HTTP servers and clients
- tornado.httpserver — Non-blocking HTTP server
- tornado.httpclient — Asynchronous HTTP client
- tornado.httputil — Manipulate HTTP headers and URLs
- tornado.http1connection – HTTP/1.x client/server implementation
- Asynchronous networking
- tornado.ioloop — Main event loop
- tornado.iostream — Convenient wrappers for non-blocking sockets
- tornado.netutil — Miscellaneous network utilities
- tornado.tcpclient — IOStream connection factory
- tornado.tcpserver — Basic IOStream-based TCP server
- Coroutines and concurrency
- tornado.gen — Simplify asynchronous code
- tornado.concurrent — Work with threads and futures
- tornado.locks – Synchronization primitives
- tornado.queues – Queues for coroutines
- tornado.process — Utilities for multiple processes
- Integration with other services
- tornado.auth — Third-party login with OpenID and OAuth
- tornado.wsgi — Interoperability with other Python frameworks and servers
- tornado.platform.asyncio — Bridge between asyncio and Tornado
- tornado.platform.caresresolver — Asynchronous DNS Resolver using C-Ares
- tornado.platform.twisted — Bridges between Twisted and Tornado
- Utilities
- tornado.autoreload — Automatically detect code changes in development
- tornado.log — Logging support
- tornado.options — Command-line parsing
- tornado.stack_context — Exception handling across asynchronous callbacks
- tornado.testing — Unit testing support for asynchronous code
- tornado.util — General-purpose utilities
- Frequently Asked Questions
- Release notes
- What's new in Tornado 4.3
- What's new in Tornado 4.2.1
- What's new in Tornado 4.2
- What's new in Tornado 4.1
- What's new in Tornado 4.0.2
- What's new in Tornado 4.0.1
- What's new in Tornado 4.0
- What's new in Tornado 3.2.2
- What's new in Tornado 3.2.1
- What's new in Tornado 3.2
- What's new in Tornado 3.1.1
- What's new in Tornado 3.1
- What's new in Tornado 3.0.2
- What's new in Tornado 3.0.1
- What's new in Tornado 3.0
- What's new in Tornado 2.4.1
- What's new in Tornado 2.4
- What's new in Tornado 2.3
- What's new in Tornado 2.2.1
- What's new in Tornado 2.2
- What's new in Tornado 2.1.1
- What's new in Tornado 2.1
- What's new in Tornado 2.0
- What's new in Tornado 1.2.1
- What's new in Tornado 1.2
- What's new in Tornado 1.1.1
- What's new in Tornado 1.1
- What's new in Tornado 1.0.1
- What's new in Tornado 1.0