Last active
March 16, 2020 13:28
-
-
Save osullivj/e0faf2348528285a388b74e398307bae to your computer and use it in GitHub Desktop.
Minimal Tornado Web Server & RethinkDB Python Client
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 | |
PORT=9090 | |
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), | |
]) | |
@tornado.gen.coroutine | |
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 feed.next( ) | |
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