Skip to content

Instantly share code, notes, and snippets.

@ricleal
Forked from bootandy/tornado_mongo.py
Created July 15, 2016 14:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ricleal/d7e6110f6dc2813b92ec8cecc4b452a6 to your computer and use it in GitHub Desktop.
Save ricleal/d7e6110f6dc2813b92ec8cecc4b452a6 to your computer and use it in GitHub Desktop.
Very simple sample code of tornado and mongodb
from datetime import datetime
from pymongo.connection import Connection
import tornado.httpserver
import tornado.ioloop
import tornado.options
import tornado.web
from tornado.options import define, options
define("port", default=8888, help="run on the given port", type=int)
class Application(tornado.web.Application):
def __init__(self):
handlers = [
(r"/", MainHandler),
]
settings = dict(
autoescape=None,
)
tornado.web.Application.__init__(self, handlers, **settings)
self.con = Connection('localhost', 27017)
self.database = self.con["mongosample"]
class MainHandler(tornado.web.RequestHandler):
def get(self):
db=self.application.database
new_comment = {
"comment" : "what a nice page",
"author" : "bobby",
"time" : datetime.utcnow(),
}
db.comments.insert(new_comment)
comments = db["comments"].find()
self.write("Hello, world. with mongo")
for c in comments:
self.write("<br/>")
self.write(c["comment"])
self.write(' - ' + c["author"])
self.write(' at time: ' + str(c["time"]))
def main():
tornado.options.parse_command_line()
http_server = tornado.httpserver.HTTPServer(Application())
http_server.listen(options.port)
tornado.ioloop.IOLoop.instance().start()
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment