tornado_mongodb 连接和使用
tornado,mongodb 连接和使用,开始使用tornado3.2,mongodb2.6,pymongo-3.03遇到不少的麻烦。因为新版tornado与老版本的代码有很多变化,mongodb根本没法连接,这里借Shekhar Gulati编写的代码终于看清楚这个框架怎么连接mongodb了。非常谢谢Shekhar Gulati!
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()
tornado_mongodb 连接和使用的更多相关文章
- nodejs进阶(6)—连接MySQL数据库
1. 建库连库 连接MySQL数据库需要安装支持 npm install mysql 我们需要提前安装按mysql sever端 建一个数据库mydb1 mysql> CREATE DATABA ...
- SQL Server 无法连接到服务器。SQL Server 复制需要有实际的服务器名称才能连接到服务器。请指定实际的服务器名称。
异常处理汇总-数据库系列 http://www.cnblogs.com/dunitian/p/4522990.html SQL性能优化汇总篇:http://www.cnblogs.com/dunit ...
- Linux 开机时网络自动连接
简单版本: cd /etc/sysconfig/network-scripts/ vi ifcfg-enoXXX 输入:reboot重启 或者输入:service network restart ...
- 在ubuntu16.10 PHP测试连接MySQL中出现Call to undefined function: mysql_connect()
1.问题: 测试php7.0 链接mysql数据库的时候发生错误: Fatal error: Uncaught Error: Call to undefined function mysqli_con ...
- 【初学python】使用python连接mysql数据查询结果并显示
因为测试工作经常需要与后台数据库进行数据比较和统计,所以采用python编写连接数据库脚本方便测试,提高工作效率,脚本如下(python连接mysql需要引入第三方库MySQLdb,百度下载安装) # ...
- 一起学微软Power BI系列-使用技巧(1)连接Oracle与Mysql数据库
说起Oracle数据库,以前没用过Oracle不知道,但是这1年用Oracle后,发现真的是想狂吐槽,特别是那个.NET驱动和链接字符串,特别奇葩.总归是和其他数据库不一样,标新立异,不知道为何.另外 ...
- 使用SecureCRT连接虚拟机(ubuntu)配置记录
这种配置方法,可以非常方便的操作虚拟机里的Linux系统,且让VMware在后台运行,因为有时候我直接在虚拟机里操作会稍微卡顿,或者切换速度不理想,使用该方法亲测本机效果确实ok,特此记录. Secu ...
- c# 字符串连接使用“+”和string.format格式化两种方式
参考文章:http://www.liangshunet.com/ca/201303/218815742.htm 字符串之间的连接常用的两种是:“+”连接.string.format格式化连接.Stri ...
- 如何使用本地账户"完整"安装 SharePoint Server 2010+解决“New-SPConfigurationDatabase : 无法连接到 SharePoint_Config 的 SQL Server 的数据 库 master。此数据库可能不存在,或当前用户没有连接权限。”
注:目前看到的解决本地账户完整安装SharePoint Server 2010的解决方案如下,但是,有但是的哦: 当我们选择了"完整"模式安装SharePointServer201 ...
随机推荐
- C#搜索指定文件夹内的符合要求的文件
下面的列子是文件的模糊查找, 具体功能是:选定文件夹,搜索所有文件命中包含“_bui”字样的shp图层(后缀为.shp)并将信息显示在ListView中.实际应用中可随便修改. 这里采用递归方法进行深 ...
- 一般处理程序如何获取session值
1.要在一般处理程序中获取其他页面的session值,需要引用名空间: using System.Web.SessionState; 2.然后继承一个接口:IRequiresSessionState ...
- finally类
finally叫做最后的执行快,什么是最后的执行快?他的意思是这样的 他是写在try catch 的后面但是只能写一个,他设计这个finally的意思就是,如果try里面出错肯定会往陷阱里 面跑.没有 ...
- 展望 2017年商业智能BI 发展的趋势
在展望2017年商业智能 BI 发展趋势前,我们先来了解一下商业智能 BI 发展的几个重要阶段. 传统 BI 和新型 BI 的分水岭(2013年) 大背景 在2013年以前相当长的一个周期(2005年 ...
- 关于mouse_event和sendinput无效的原因
关于mouse_event和sendinput无效的原因 SetCursorPos 有用, 于mouse_event和sendinput 无用, 导致问题不清晰, 原来是我换了杀毒软件, 360 ...
- js滚动条滚动到某个元素位置
scrollTo(0,document.getElementById('xxx').offset().top);
- IDEA +maven+ ContextLoaderListener not find
tomcat 启动失败:SEVERE: Context [] startup failed due to previous errors 查看pox.xml 有spring-web依赖 查看tomca ...
- HTTPS 进阶
参考 一.中间人攻击 概念:攻击者插入到原本直接通信的双方,让双方以为还在直接跟对方通讯,但是实际上双方的通信对方已经变成中间人,信息已经被中间人获取或篡改. HTTPS 的攻击分为两类:SSL 连接 ...
- maven---install报错
若maven项目在install或者run的时候出现莫名奇妙的问题,应该考虑是否在pom.xml中引入的包是否为最新的包: 因为在本地maven仓库中已经存在的包就不会再下载,所以可以从这方面排查问题 ...
- HTML5web存储之localStorage
localStorage与cookie的作用类似,只能存储字符串,以键值对的方式进行存储:与cookie不同的是,可以存储更多的数据. localStorage用于持久化的本地存储. var skey ...