python - Can't connect to Meteor with Pymongo -
i trying connect meteor mongo database through pymongo. here's code:
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 pymongo import mongoclient client = mongoclient(get_mongo_url("mysite.com"))
and error (the print statement yields correct url)
>> mongodb://client-xxxxx:yyyyy@production-db-c1.meteor.io: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
if run meteor mongo --url mysite.com
, copy result return ...
@ top of function , uncomment it, connection works. why can't connect programmatically?
the subprocess code appends line feed character \n
end of url.
you need strip .rstrip()
the right way replace return in function with
return out.rstrip()
for confirmation purposes show happens function as-is , rstrip() applied/unapplied return.
murl = get_mongo_url('').rstrip()
mongodb://client-faf1d0db:746d8f43-367b-dde2-b69a-039ff8b9f7fa@production-db-a1.meteor.io:27017/_meteor_comclient = pymongo.mongoclient(murl)
worked ok
murl = get_mongo_url('')
mongodb://client-3578a20b:d4ddeec9-6d24-713e-8ddb-c357b664948a@production-db-a1.meteor.io: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
failed without rstrip.
murl = get_mongo_url('')
mongodb://client-1a193a61:4c9c572e-22e3-4b7e-44a1-dc76bfb65e86@production-db-a1.meteor.io: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
failed again (no rsrtip)
murl = get_mongo_url('').rstrip()
mongodb://client-ce463608:d7dc6be0-499f-1808-43e1-fdfb8b6e8ebc@production-db-a1.meteor.io:27017/_meteor_comclient = pymongo.mongoclient(murl)
worked (rstrip used).
the following general info on mongodb urls. may know already.
the url pymongo wants not web url url-like specifier mongo database connection. development environment, mongodb set on port 3001, not default mongodb port production server.
meteor applications can configured use mongodb hosted anywhere. not have on same server serves meteor content. specification of done through mongodb:// url pymongo wants. pymongo doesn't depend on meteor website url, can different mongodb url.
here code using
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})
according mongodb site on github, mongo_url
format is
mongodb://[username:password@]host1[:port1][,host2[:port2],...[,hostn[:portn]]][/[database][?options]]
so mongodb url mongodb://localhost:3001/meteor
can interpreted this:
* mongodb:// means describes mongodb connection * localhost means connect locally * :3001 means use non-standard port number 3001. how "meteor run" sets mongo * /meteor means connect database called "meteor"
Comments
Post a Comment