tornado_mongodb 连接和使用
2015-11-17 16:33
435 查看
tornado,mongodb 连接和使用,开始使用tornado3.2,mongodb2.6,pymongo-3.03遇到不少的麻烦。因为新版tornado与老版本的代码有很多变化,mongodb根本没法连接,这里借Shekhar Gulati编写的代码终于看清楚这个框架怎么连接mongodb了。非常谢谢Shekhar Gulati!
tornadoapp.py文件
app.py文件
tornadoapp.py文件
import os from tornado import ioloop,web from tornado.escape import json_encode from pymongo import MongoClient import json from bson import json_util from bson.objectid import ObjectId MONGODB_DB_URL = os.environ.get('OPENSHIFT_MONGODB_DB_URL') if os.environ.get('OPENSHIFT_MONGODB_DB_URL') else 'mongodb://localhost:27017/' MONGODB_DB_NAME = os.environ.get('OPENSHIFT_APP_NAME') if os.environ.get('OPENSHIFT_APP_NAME') else 'getbookmarks' client = MongoClient(MONGODB_DB_URL) db = client[MONGODB_DB_NAME] class IndexHandler(web.RequestHandler): def get(self): self.render("index.html") class StoriesHandler(web.RequestHandler): def get(self): stories = db.stories.find() self.set_header("Content-Type", "application/json") self.write(json.dumps(list(stories),default=json_util.default)) def post(self): story_data = json.loads(self.request.body) story_id = db.stories.insert(story_data) print('story created with id ' + str(story_id)) self.set_header("Content-Type", "application/json") self.set_status(201) class StoryHandler(web.RequestHandler): def get(self , story_id): story = db.stories.find_one({"_id":ObjectId(str(story_id))}) self.set_header("Content-Type", "application/json") self.write(json.dumps((story),default=json_util.default)) settings = { "template_path": os.path.join(os.path.dirname(__file__), "templates"), "static_path": os.path.join(os.path.dirname(__file__), "static"), "debug" : True } application = web.Application([ (r'/', IndexHandler), (r'/index', IndexHandler), (r'/api/v1/stories',StoriesHandler), (r'/api/v1/stories/(.*)', StoryHandler) ],**settings) if __name__ == "__main__": application.listen(8081) ioloop.IOLoop.instance().start()
app.py文件
#!/usr/bin/env python # # This file may be used instead of Apache mod_wsgi to run your python # web application in a different framework. A few examples are # provided (cherrypi, gevent), but this file may be altered to run # whatever framework is desired - or a completely customized service. # import imp import os try: zvirtenv = os.path.join(os.environ['OPENSHIFT_PYTHON_DIR'], 'virtenv', 'bin', 'activate_this.py') execfile(zvirtenv, dict(__file__ = zvirtenv) ) except IOError: pass # # IMPORTANT: Put any additional includes below this line. If placed above this # line, it's possible required libraries won't be in your searchable path # # # main(): # if __name__ == '__main__': ip = os.environ['OPENSHIFT_PYTHON_IP'] port = int(os.environ['OPENSHIFT_PYTHON_PORT']) app = imp.load_source('application', 'tornadoapp.py') app.application.listen(port , ip) app.ioloop.IOLoop.instance().start()
相关文章推荐
- MongoDB小结15 - find【查询条件$ne】
- MongoDB小结14 - find【查询条件$lt $lte $gt $gte】
- MongoDB小结13 - find【2个参数】
- MongoDB小结12 - update【多文档更新】
- MongoDB小结11 - update【save】
- MongoDB小结10 - update【upsert】
- java下如何执行mongodb的db.currentOp
- nginx+gridfs+mongodb分布式图片存储系统
- mongodb 持久化
- nodejs、express、mongoDB的安装
- Windows7下安装MongoDB
- MongoDB 复制集模式Replica Sets
- python-mongodb存储文件
- MongoDB介绍以及在linux平台的安装使用
- mongodb里的集合数据迁移到mysql库
- Spider based on scrapy
- MongoDB小结09 - update【定位修改器】
- MongoDB小结07 - update【$pop】
- MongoDB小结08 - update【$pull】
- MongoDB小结06 - update【$push】