最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

【已解决】根据参数自动生成MongoDB的MongoClient的Uri参数

参数 crifan 1442浏览 0评论
折腾:
【未解决】PySpider中把结果保存到MongoDB数据库中
期间,涉及到本地开发测试和在线生产等不同环境,传递不同参数给pymongo的MongoClient,去打开和连接MongoDB。
而关于mongodb的uri,此处希望可以根据参数不同自动生成。
参考之前用过的代码,另外再去整合
先参考:
mongo_client – Tools for connecting to MongoDB — PyMongo 3.7.2 documentation
Tutorial — PyMongo 3.7.2 documentation
mongo_client – Tools for connecting to MongoDB — PyMongo 3.7.2 documentation
mongo数据库基本操作–python篇 – 2BiTT – 博客园
经过调试,目前用代码:
from urllib.parse import quote_plus

def generateMongoUri(host=None,
                port=None,
                isUseAuth=False,
                username=None,
                password=None,
                authSource=None,
                authMechanism=None):
    """"generate mongodb uri"""
    mongodbUri = ""

    if not host:
        # host = "127.0.0.0"
        host = "localhost"

    if not port:
        port = 27017

    mongodbUri = "mongodb://%s:%s" % (
        host, \
        port
    )
    # '
mongodb://localhost:27017
'
    # '
mongodb://xxx:27017
'

    if isUseAuth:
        mongodbUri = "mongodb://%s:%s@%s:%s" % (
            quote_plus(username), \
            quote_plus(password), \
            host, \
            port \
        )
        print(mongodbUri)

        if authSource:
            mongodbUri = mongodbUri + ("/%s" % authSource)
            print("mongodbUri=%s" % mongodbUri)

        if authMechanism:
            mongodbUri = mongodbUri + ("?authMechanism=%s" % authMechanism)
            print("mongodbUri=%s" % mongodbUri)

    print("return mongodbUri=%s" % mongodbUri)
    #
mongodb://username:quoted_password@host:port/authSource?authMechanism=authMechanism
    #
mongodb://localhost:27017

    return mongodbUri

if __name__ == "__main__":
    # test default
    defaultMongoUri = generateMongoUri()
    print("defaultMongoUri=%s" % defaultMongoUri)

    # test normal host port
    normalMongoUri = generateMongoUri(host="1.2.3.4", port=34567)
    print("normalMongoUri=%s" % normalMongoUri)

    # test use auth
    useAuthMongoUri = generateMongoUri(host="1.2.3.4",
                port=34567,
                isUseAuth=True,
                username="normal_user",
                password="complexP@D",
                authSource="admin",
                authMechanism="SCRAM-SHA-1")
    print("useAuthMongoUri=%s" % useAuthMongoUri)
输出:
return mongodbUri=
mongodb://localhost:27017
defaultMongoUri=
mongodb://localhost:27017
return mongodbUri=
mongodb://1.2.3.4:34567
normalMongoUri=
mongodb://1.2.3.4:34567
mongodb://normal_user:complexP%[email protected]:34567
mongodbUri=
mongodb://normal_user:complexP%[email protected]:34567/admin
mongodbUri=
mongodb://normal_user:complexP%[email protected]:34567/admin?authMechanism=SCRAM-SHA-1
return mongodbUri=
mongodb://normal_user:complexP%[email protected]:34567/admin?authMechanism=SCRAM-SHA-1
useAuthMongoUri=
mongodb://normal_user:complexP%[email protected]:34567/admin?authMechanism=SCRAM-SHA-1
[1]    4243 terminated  env "PYTHONIOENCODING=UTF-8" "PYTHONUNBUFFERED=1" /usr/local/bin/python3
貌似满足需求了。

转载请注明:在路上 » 【已解决】根据参数自动生成MongoDB的MongoClient的Uri参数

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
80 queries in 0.189 seconds, using 22.09MB memory