Tornado is a Python web framework and asynchronous networking library, originally developed at FriendFeed. Tornado Web Server — Tornado 4.2.1 documentation

Non-blocking ORM for Tornado?

Is there any asynchronous Python ORM other than Twistar?

I'm looking for lightweight ORM for non-blocking API, built on top of tornado. Of course, I can write raw SQL queries using momoko, but I'd like to work with objects.

Source: (StackOverflow)

Tornado login Examples/Tutorials [closed]

I was wondering if anyone know of any example code or tutorials on implementing a login/signup page in Tornado? Ive seen the examples that come with it, but they seem very facebook/oauth centric.

Source: (StackOverflow)

Using Tornado, how do I serve static files and serve a favicon.ico from a different directory than the static path?

I am trying this:

favicon_path = '/path/to/favicon.ico'

settings = {'debug': True, 
            'static_path': os.path.join(PATH, 'static')}

handlers = [(r'/', WebHandler),
            (r'/favicon.ico', tornado.web.StaticFileHandler, {'path': favicon_path})]

application = tornado.web.Application(handlers, **settings)

But it keeps serving the favicon.ico that I have in my static_path (I have two different favicon.ico's in two separate paths, as indicated above, but I want to be able to override the one in the static_path).

Source: (StackOverflow)

what's the tornado ioloop, and tornado's workflow?

i want to know tornado's internal workflow, and have seen this article, it's great, but something i just can't figure out

within the ioloop.py, there is such a function

def add_handler(self, fd, handler, events):
    """Registers the given handler to receive the given events for fd."""
    self._handlers[fd] = handler
    self._impl.register(fd, events | self.ERROR)

so what's this mean? every request will trigger add_handler or it's just triggered once when init?

every socket connect will generate a file descriptor , or it's just generated once?

what's the relationship between ioloop and iostream ?

how does httpserver work with ioloop and iostream ?

is there any workflow chart, so i can see it clearly ?

sorry for these questiones, i just confused

any link, suggestion, tip helps. many thanks :)

Source: (StackOverflow)

Tornado is "a relatively simple, non-blocking web server framework written in Python"--can somewhat explain what that means?

This is probably a stupid question, but what exactly is a "non-blocking web server"? All web servers are technically non-blocking, arent they? otherwise how could they handle simultaneous connections? Apache2 achieves this using a combination of fork() and pthreads. How exactly are Tornado (and Twisted also) different? Do they just set a bunch of sockets to non-bocking mode, build an FD list (or equivalent), and then loop over that with one big select() sys call?

Where would you use a framework like these, and what advantages can they give you over Apache2 (or other popular servers)? Thanks

Source: (StackOverflow)

What are the possible ways to authenticate user when websocket connection is used?

Example scenario: Web based multi-user chat application through websocket connection. How can I ensure (or guarantee) that each connection in this application belongs to certain authenticated user and "can't be" exploited by false user impersonation or intervene during the connection.

by the way I am using tornado websocket on server side to implement basic chat room and already doing authentication for the non-websocket part of my app.

ps : suppose authenticated user posts what he wants and when other user open the listing page of item and automatically other user is added to list of websocket listeners what I want each user able to chat with buyer of the item individually not in a chatroom way but with one to one chat

Source: (StackOverflow)

cross-origin resource sharing (CORS) with jQuery and Tornado

Let's say, I have a Tornado web server (localhost) and a web page (othermachine.com), and the latter contains javascript that needs to make cross-domain ajax calls to the Tornado server.

So I set up my Tornado as such:

class BaseHandler(tornado.web.RequestHandler):
    def set_default_headers(self):
        self.set_header("Access-Control-Allow-Origin", "http://www.othermachine.com")
        self.set_header("Access-Control-Allow-Credentials", "true")
        self.set_header("Access-Control-Allow-Methods", "GET,PUT,POST,DELETE,OPTIONS")
            "Content-Type, Depth, User-Agent, X-File-Size, X-Requested-With, X-Requested-By, If-Modified-Since, X-File-Name, Cache-Control")

And my javascript makes a jQuery call:

    type: 'GET',
    url: "http://localhost:8899/load/space",
    data: { src: "dH8b" },
    success: function(resp){
        console.log("ajax response: "+resp);
    dataType: 'json',
    beforeSend: function ( xhr ) {
        xhr.setRequestHeader('Content-Type', 'text/plain');
        xhr.setRequestHeader('Access-Control-Request-Method', 'GET');
        xhr.setRequestHeader('Access-Control-Request-Headers', 'X-Requested-With');
        xhr.withCredentials = true;

But I get the lovely XMLHttpRequest cannot load http://localhost:8899/load/space?src=dH8b. Origin http://www.othermachine.com is not allowed by Access-Control-Allow-Origin error. I can't tell which side of jQuery / Tornado (or both?) am I not setting up correctly.

According to dev tools, these are the headers the jQuery request is sending:

Request Headers

User-Agent:Mozilla/5.0 ...

If I simply make a request from my browser's url field I get a '200 OK' with this:

Response Headers

Access-Control-Allow-Headers:Content-Type, User-Agent, X-Requested-With, X-Requested-By, Cache-Control
Content-Type:text/html; charset=UTF-8

Does that mean Tornado is doing its job? I tried to follow the advice of all the stackoverflow CORS+jQuery posts (e.g. this), to no avail. CORS in concept seems simple enough, but maybe I am fundamentally misunderstanding what is supposed to happen in a CORS transaction... please help! Thanks in advance.

Source: (StackOverflow)

Using a simple python generator as a co-routine in a Tornado async handler?

I have a python generator function which yields chunks of text. I would like to write a get method for a tornado.web.RequestHandler subclass that will iterate over the generator, writing the chunks out to the response as it goes.

Since this is Tornado, and since the generator may take over a second to process, I thought it would be nice to make the handler asynchronous, using this generator as a co-routine and passing off control to the IOLoop after every chunk. However, I can't make heads or tails of how to do this.

Here's my example (blocking) code:

class TextHandler(web.RequestHandler):
    def get(self, n):
        generator = self.generate_text(100000)
        # Clearly, this will block. How to make it asynchronous?
        for text in generator:

    def generate_text(n):
        for x in xrange(n):
            if not x % 15:
                yield "FizzBuzz\n"
            elif not x % 5:
                yield "Buzz\n"
            elif not x % 3:
                yield "Fizz\n"
                yield "%s\n" % x

How can I make this handler work asynchronously?

Source: (StackOverflow)

tornado 403 GET warning when opening websocket

I found this python script which should allow me to open a websocket. However, I receive the warning [W 1402720 14:44:35 web:1811] 403 GET / ( 11.02 ms in my linux terminal when trying to open the actual websocket (using Old WebSocket Terminal Chrome plugin). The messages "connection opened", "connection closed" and "message received" are never printed in the terminal window.

import tornado.httpserver
import tornado.ioloop
import tornado.options
import tornado.web
import tornado.websocket

class MyHandler(tornado.websocket.WebSocketHandler):
        def open(self):
                print "connection opened"
                self.write_message("connection opened")

        def on_close(self):
                print "connection closed"

        def on_message(self,message):
                print "Message received: {}".format(message)
                self.write_message("message received")

if __name__ == "__main__":
        app = tornado.web.Application(handlers=[(r"/",MyHandler)])
        server = tornado.httpserver.HTTPServer(app)

Source: (StackOverflow)

What is Facebook's new Tornado framework?

Facebook just open-sourced a framework called Tornado.

What is it? What does it help a site do?

I believe Facebook uses a LAMP structure. Is it useful for smaller sites which are written under the LAMP stack?

Source: (StackOverflow)

Tornado on Raspberry Pi to use websockets as well as monitor serial port Arduino communication

Essentially, what I'm hoping to achieve is a canvas based web interface to control an Arduino, via a Raspberry Pi. The use case is that a user navigates to raspberrypi:8080 which displays a canvas. Then upon moving a slider, a websocket message is sent to the Tornado server on the Raspberry Pi. Tornado then sends a serial message to the Arduino which changes the RGB value of an LED. So far so good, I've been able to do this with the help of the documentation by a developer, Raspberry Pi Android HTML5 Realtime Servo Control.

However, the communication is only one-way from Raspberry Pi to Arduino. I'd like Tornado to also monitor the serial port to get any sensor data back to the front-end. Here's where I'm unsure about how to proceed. I was able to accomplish something like this using Node.js, which monitors for both websocket messages as well as serial messages asynchronously.

Should an asynchronous process be spawned which constantly monitors the port? I've seen a couple of options for this sort of solution.

  1. Some people suggest tornado.gen.Task, but for single HTTP requests, not for constant serial data.
  2. tornado.ioloop.PeriodicCallback which I could set up to check for serial data every millisecond, but that sounds like a lot of overhead.
  3. I've also seen separate tools such as Swirl. (Swirl is outdated according to it's Github repo)

Or should I set up a separate Python application which monitors serial and then communicates to the Tornado application on something it can understand like the following?

  1. websocket messages using a websocket client
  2. ZeroMQ (working example: pyzmq / examples / eventloop / web.py)

So there are lots of options... What are some recommendations and some reasons to try out or avoid any of the above options?

Here's what I have and need to add serial monitoring to:

import tornado.httpserver
import tornado.ioloop
import tornado.options
import tornado.web
import tornado.websocket

from tornado.options import define, options
define("port", default=8080, help="run on the given port", type=int)

class IndexHandler(tornado.web.RequestHandler):
    def get(self):

class WebSocketHandler(tornado.websocket.WebSocketHandler):
    def open(self):
        print 'new connection'

    def on_message(self, message):
        print 'message received %s' % message
        self.write_message('message received %s' % message)

    def on_close(self):
        print 'connection closed'

if __name__ == "__main__":
    app = tornado.web.Application(
            (r"/", IndexHandler),
            (r"/ws", WebSocketHandler)
    httpServer = tornado.httpserver.HTTPServer(app)
    print "Listening on port:", options.port

Source: (StackOverflow)

How use Django with Tornado web server?

How do I use Django with the Tornado web server?

Source: (StackOverflow)

In tornado is it possible to return a HTTP error code without the default template?

I am currently using the following to raise a HTTP bad request:

raise tornado.web.HTTPError(400)

which returns a html output:

<html><title>400: Bad Request</title><body>400: Bad Request</body></html>

Is it possible to return just the HTTP response code with a custom body?

Source: (StackOverflow)

How to run functions outside websocket loop in python (tornado)

I'm trying to set up a small example of a public Twitter stream over websockets. This is my websocket.py, and it's working.

What I'm wondering is: how can I interact with the websocket from 'outside' the class WSHandler (ie. not only answer when receiving a message from websocket.js)? Say I want to run some other function within this same script that would post "hello!" every five seconds and send that to the websocket (browser) without any interaction from client-side. How could I do that?

So it's kind of a fundamental beginner's question, I suppose, about how to deal with classes as those below. Any pointers in any direction would be greatly appreciated!

import os.path
import tornado.httpserver
import tornado.websocket
import tornado.ioloop
import tornado.web

# websocket
class FaviconHandler(tornado.web.RequestHandler):
    def get(self):

class WebHandler(tornado.web.RequestHandler):
    def get(self):

class WSHandler(tornado.websocket.WebSocketHandler):
    def open(self):
        print 'new connection'
        self.write_message("Hi, client: connection is made ...")

    def on_message(self, message):
        print 'message received: \"%s\"' % message
        self.write_message("Echo: \"" + message + "\"")
        if (message == "green"):

    def on_close(self):
        print 'connection closed'

handlers = [
    (r"/favicon.ico", FaviconHandler),
    (r'/static/(.*)', tornado.web.StaticFileHandler, {'path': 'static'}),
    (r'/', WebHandler),
    (r'/ws', WSHandler),

settings = dict(
    template_path=os.path.join(os.path.dirname(__file__), "static"),

application = tornado.web.Application(handlers, **settings)

if __name__ == "__main__":
    http_server = tornado.httpserver.HTTPServer(application)

Source: (StackOverflow)

Tornado URL query parameters

I've been playing around with Tornado, and I've written some code that doesn't seem very nice.

I'm writing an app to store recipes as an example. These are my handlers:

handlers = [
    (r"/recipes/", RecipeHandler),
    (r"/recipes", RecipeSearchHandler), #so query params can be used to search

This lead me to writing this:

class RecipeHandler(RequestHandler):      
    def get(self):

class RecipeSearchHandler(RequestHandler):    
    def get(self):
            name = self.get_argument('name', True)
        # will do some searching
        except AssertionError:
            self.write("no params")
            # will probably redirect to /recipes/

Is there a better way to approach these URLs without a try/except? I'd like /recipes and /recipes/ to show the same thing, whereas /recipes?name=something would do a search, and ideally be a different handler.

Source: (StackOverflow)