PYMONGOD使用集群且读写分离

easy_install install pymongo
pip install pymongo

#!/usr/bin/python env
from pymongo import MongoClient
from pymongo import ReadPreference
from time import sleep
#c = MongoClient('mongodb://dbadmin:123@172.17.13.6:27017,172.17.13.8:27017/?localThresholdMS=30')
c = MongoClient("mongodb://dbadmin:123@172.17.13.6:27017,172.17.13.8:27017,replicaSet=rs1,readPreference='secondaryPreferred'")
db = c.test
db.read_preference = ReadPreference.SECONDARY_PREFERRED
db.test.insert_one({"x": 1}).inserted_id
print db.client.address
def select():
    while 1==1:
        s = db.test.find_one()
        print s
        sleep(1)
        print db.client.address
        print db.read_preference
try:
    select()
except Exception:
    select()