Last active
December 15, 2016 17:49
-
-
Save bootandy/1676440 to your computer and use it in GitHub Desktop.
Very simple sample code of tornado and mongodb
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
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() | |
nice.. working great
Hello
Thank you sso much
can you help me plase ?
ImportError: No module named connection
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
this is not working