Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Minimal Tornado Web Server & RethinkDB Python Client
# A test tornado HTTP server integrates with RethinkDB
# Requirements: Python 3.4, Tornado 4.3, RethinkDB 2.3.0
# JOS 2016-04-08
import socket
import logging
import os
import tornado.ioloop
import tornado.web
import rethinkdb as rdb
# Some constants - hack these to change the port, or the home page
HOST=socket.gethostname( )
HomePage="<html><body><p>RethinkDB TestHTTPServer http://%s:%d</p></body></html>" % ( HOST, PORT)
class RootHandler( tornado.web.RequestHandler):
def get( self):
self.write( HomePage)
def make_app( ):
return tornado.web.Application([
(r'/*', RootHandler),
def print_cfeed_data( connection_future, table):
connection = yield connection_future
feed = yield rdb.table( table).changes( ).run( connection)
while ( yield feed.fetch_next( )):
item = yield )
print( item)
if __name__ == "__main__":
# tornado logs to stdout by default - we want it in a file in the %TEMP% dir
logf = '%s\\tornado_%d.log' % ( os.environ.get('TEMP'), os.getpid( ))
logfmt = '%(asctime)s %(levelname)s %(thread)s %(message)s'
logging.basicConfig( filename=logf, format=logfmt, level=logging.INFO)
# init RethinkDB
rdb.set_loop_type( 'tornado')
conn = rdb.connect( host='localhost', port=28015)
app = make_app( )
app.listen( PORT)
loop = tornado.ioloop.IOLoop.current( )
loop.add_callback( print_cfeed_data, conn, 'tv_shows')
loop.start( )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment