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:
A WSGI equivalent of tornado.web.Application.
WSGIApplication is very similar to tornado.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.
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) server.serve_forever()
See the appengine demo for an example of using this module to run a Tornado app on Google App Engine.
WSGI applications use the same RequestHandler class, but not @asynchronous methods or flush(). This means that it is not possible to use AsyncHTTPClient, or the tornado.auth or tornado.websocket modules.
Mimics tornado.httpserver.HTTPRequest for WSGI applications.
Parses the given WSGI environment 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.
Makes a WSGI-compatible function runnable on Tornado’s HTTP server.
WSGI is a synchronous interface, while Tornado’s concurrency model is based on single-threaded asynchronous execution. This means that running a WSGI app with Tornado’s WSGIContainer is less scalable than running the same app in a multi-threaded WSGI server like gunicorn or uwsgi. Use WSGIContainer only when there are benefits to combining Tornado and WSGI in the same process that outweigh the reduced scalability.
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.instance().start()
This class is intended to let other frameworks (Django, web.py, etc) run on the Tornado HTTP server and I/O loop.