我正在尝试通过 pymongo 连接到 Meteor Mongo 数据库。代码如下:
def get_mongo_url(site):
# return "mongodb://client-xxxxx:<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="acd5d5d5d5d5ecdcdec3c8d9cfd8c5c3c281c8ce81cf9d82c1c9d8c9c3de82c5c3" rel="noreferrer noopener nofollow">[email protected]</a>:27017/site"
import subprocess
p = subprocess.Popen(['meteor', 'mongo', '--url', site], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err = p.communicate()
print out
return out
from pymongo import MongoClient
client = MongoClient(get_mongo_url("mysite.com"))
以及错误(打印语句产生正确的 url)
>> mongodb://client-xxxxx:<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="69101010101029191b060d1c0a1d000607440d0b440a5847040c1d0c061b470006" rel="noreferrer noopener nofollow">[email protected]</a>:27017/site
Traceback (most recent call last):
File "private/test.py", line 46, in <module>
client = pymongo.MongoClient(get_mongo_url(METEOR_SITE))
File "/Library/Python/2.7/site-packages/pymongo/mongo_client.py", line 369, in __init__
raise ConfigurationError(str(exc))
pymongo.errors.ConfigurationError: command SON([('authenticate', 1), ('user', u'client-xxxxx'), ('nonce', u'zzzzz'), ('key', u'ttttt')]) failed: auth fails
如果我运行 meteor mongo --url mysite.com
,将结果复制到函数顶部的 return ...
并取消注释,连接作品。为什么我无法以编程方式连接?
最佳答案
子进程代码附加换行符 \n
到网址末尾。
您需要用 .rstrip()
删除它
正确的方法是将函数中的 return 替换为
return out.rstrip()
出于确认目的,我将按原样展示该函数会发生什么情况,并且 rstrip() 应用/未应用到返回值。
block 引用> block 引用>murl = get_mongo_url('').rstrip()
mongodb://client-faf1d0db:[email protected]:27017/_meteor_comclient = pymongo.MongoClient(murl)
工作正常
block 引用> block 引用>murl = get_mongo_url('')
mongodb://client-3578a20b:[email protected]:27017/_meteor_comclient = pymongo.MongoClient(murl)
Traceback (most recent call last):
File "", line 1, in
File "/home/action/.local/lib/python2.7/site-packages/pymongo/mongo_client.py", line 383, in init
raise ConfigurationError(str(exc))
pymongo.errors.ConfigurationError: command SON([('authenticate', 1), ('user', u'client-3578a20b'), ('nonce', u'e14e2bdb3d8484b9'), ('key', u'9 c101b78ff1a617a9c5f0def36c7e3d9')]) failed: auth fails没有 rstrip 失败。
block 引用> block 引用>murl = get_mongo_url('')
mongodb://client-1a193a61:[email protected]:27017/_meteor_comclient = pymongo.MongoClient(murl)
Traceback (most recent call last):
File "", line 1, in
File "/home/action/.local/lib/python2.7/site-packages/pymongo/mongo_client.py", line 383, in init
raise ConfigurationError(str(exc))
pymongo.errors.ConfigurationError: command SON([('authenticate', 1), ('user', u'client-1a193a61'), ('nonce', u'a2576142b1a33d8b'), ('key', u'4 419c490bcdcc65b20f2950c3b106d59')]) failed: auth fails再次失败(无 rsrtip)
block 引用> block 引用>murl = get_mongo_url('').rstrip()
mongodb://client-ce463608:[email protected]:27017/_meteor_comclient = pymongo.MongoClient(murl)
有效(使用了 rstrip)。
以下是有关 mongodb URL 的一般信息。您可能已经知道这一点。
pymongo 想要的 URL 不是 Web URL,而是用于 mongo 数据库连接的类似 URL 的说明符。 对于开发环境,mongodb 通常设置在端口 3001,这不是生产服务器的默认 mongodb 端口。
Meteor 应用程序可以配置为使用托管在任何地方的 mongodb。它不必位于提供 meteor 内容的同一服务器上。这个规范是通过 mongodb://URL 完成的,这正是 pymongo 想要的。 pymongo 不依赖于 Meteor 网站 url,它可能与 mongodb url 有很大不同。
这是我正在使用的一些代码
import pymongo MONGO_URL = r'mongodb://localhost:3001/meteor' ### def connect(): client = pymongo.MongoClient(MONGO_URL) return client def findUser(c, email): users = c.meteor.users return users.find_one({"emails.address": email})
根据 Github 上的 mongodb 站点,
MONGO_URL
format是
mongodb://[username:password@]host1[:port1][,host2[:port2],...[,hostN[:portN]]][/[database][?options]]
所以 mongodb url
mongodb://localhost:3001/meteor
可以这样解释:* mongodb:// means this describes a mongodb connection * localhost means connect locally * :3001 means use non-standard port number 3001. this is how "meteor run" sets up mongo * /meteor means connect to the database called "meteor"
关于python - 无法使用 Pymongo 连接到 Meteor,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23300295/