明霞山资源网 Design By www.htccd.com

简单的测试用例

#!/usr/bin/python
# -*- coding: UTF-8 -*-
import time
from pymongo import MongoClient
# 连接单机
# single mongo
# c = MongoClient(host="192.168.89.151", port=27017)
# 连接集群
c = MongoClient('mongodb://192.168.89.151,192.168.89.152,192.168.89.153')

大型项目的使用方式

三个目录:

目录a为config目录,存放项目的配置文件,主要由conf.ini和__init__.py组成"htmlcode">

import configparser
 
#定义环境变量
env = 'dev'
profile = env + '.'
 
#从conf.ini中读取变量
config = configparser.ConfigParser()
config.read(os.path.join(os.path.dirname(os.path.realpath(__file__)), 'conf.ini'))
 
for section in config.sections():
  for key, val in config.items(section):
    if key.startswith(profile):
      CONFIG_DICT['{0}_{1}'.format(section, key.replace(profile, '').upper())] = val
 
#通过conf.ini定义变量
MONGO_HOST = CONFIG_DICT['MONGO_HOST']

更多关于python连接mongodb集群方法详解的文章请查看下面的相关文章

标签:
python连接mongodb集群方法,python连接mongodb

明霞山资源网 Design By www.htccd.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
明霞山资源网 Design By www.htccd.com