Python Can';不要用Pymongo连接流星
我正试图通过pymongo连接到Meteor Mongo数据库。代码如下:Python Can';不要用Pymongo连接流星,python,mongodb,meteor,pymongo,Python,Mongodb,Meteor,Pymongo,我正试图通过pymongo连接到Meteor Mongo数据库。代码如下: def get_mongo_url(site): # return "mongodb://client-xxxxx:yyyyy@production-db-c1.meteor.io:27017/site" import subprocess p = subprocess.Popen(['meteor', 'mongo', '--url', site], stdout=subprocess.PIPE, stde
def get_mongo_url(site):
# return "mongodb://client-xxxxx:yyyyy@production-db-c1.meteor.io: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"))
以及错误(print语句生成正确的url)
>mongodb://client-xxxxx:yyyyy@production-db-c1.meteor.io:27017/现场
回溯(最近一次呼叫最后一次):
文件“private/test.py”,第46行,在
client=pymongo.MongoClient(获取mongo的url(METEOR站点))
文件“/Library/Python/2.7/site packages/pymongo/mongo_client.py”,第369行,在__
raise配置错误(str(exc))
pymongo.errors.ConfigurationError:命令子([('authenticate',1),('user',u'client-xxxxx'),('nonce',u'zzzzz'),('key',u'ttttttt'))失败:身份验证失败
如果我运行meteor mongo--url mysite.com,将结果复制到函数顶部的
return…
,并取消注释,连接就会工作。为什么不能以编程方式连接?子流程代码在url的末尾追加换行符\n
您需要使用.rstrip()
正确的方法是将函数中的返回替换为
return out.rstrip()
为了确认起见,我将显示函数的原样和
rstrip()已应用/未应用于退货
murl=get_mongo_url(“”).rstrip()mongodb://client-faf1d0db:746d8f43-367b-dde2-b69a-039ff8b9f7fa@production-db-a1.meteor.io:27017/\u meteor\u com client=pymongo.MongoClient(murl) 工作正常 murl=get_mongo_url(“”)
mongodb://client-3578a20b:d4ddeec9-6d24-713e-8ddb-c357b664948a@production-db-a1.meteor.io:27017/\u meteor\u com client=pymongo.MongoClient(murl)
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
文件“/home/action/.local/lib/python2.7/site packages/pymongo/mongo_client.py”,第383行,在init
引发配置错误(str(exc))
pymongo.errors.ConfigurationError:command-SON([('authenticate',1),('user',u'client-3578a20b'),('nonce',u'e14e2bdb3d84b9'),('key',u'9 c101b78ff1a617a9c5f0def36c7e3d9'))失败:身份验证失败 在没有rstrip的情况下失败 murl=get_mongo_url(“”)
mongodb://client-1a193a61:4c9c572e-22e3-4b7e-44a1-dc76bfb65e86@production-db-a1.meteor.io:27017/\u meteor\u com client=pymongo.MongoClient(murl)
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
文件“/home/action/.local/lib/python2.7/site packages/pymongo/mongo_client.py”,第383行,在init
引发配置错误(str(exc))
pymongo.errors.ConfigurationError:命令子([('authenticate',1),('user',u'client-1a193a61'),('nonce',u'a2576142b1a33d8b'),('key',u'4 419c490bcdcc65b20f2950c3b106d59'))失败:身份验证失败 再次失败(无rsrtip) murl=get_mongo_url(“”).rstrip()
mongodb://client-ce463608:d7dc6be0-499f-1808-43e1-fdfb8b6e8ebc@production-db-a1.meteor.io:27017/\u meteor\u com client=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站点
mongodb://[username:password@]host1[:port1][,host2[:port2],…[,hostN[:portN]][/[database][?options]]
所以mongodb urlmongodb://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"
不过,我正在连接到生产Meteor部署。请解决编辑问题——这是因为该命令生成的Meteor用户名和密码仅持续60秒。我仍然应该能够连接到它。可能没关系,但是在使用该URL字符串清除换行符之前,请尝试将其通过
.rstrip()
。我不确定有效的URL与除换行符之外不可用的URL有什么区别。这里是换行符,如果我小心地使用您的get\u mongo\u url().rstrip()
中的url,它将立即起作用。但是如果没有rstrip(),它就无法工作。
* 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"