English 中文(简体)
我怎么能用 p子在Heroku增加的 m子?
原标题:How can I use the mongolab add-on to Heroku from python?

文件只谈如何从废墟中去做。

问题回答

这是来自MongoLab的。 我们有一个通用的例子,说明如何利用官方假日司机(pymongo)在沙里连接起来。 这个例子并不是要从Heroku说来连接,而是应该这样。 不同之处在于,你需要把你的司机从你的Heroku ENV环境中conf,向司机供应。

https://github.com/mongolab/mongodb-db-river-examples/blobmaster/python/pymongo_simple_example.py

如果仍然有麻烦,你可以自由地直接在[电子邮箱:受保护]

- 亲爱

Im 采用以下方法:

import os
from urlparse import urlsplit
from pymongo import Connection

url = os.getenv( MONGOLAB_URI ,  mongodb://localhost:27017/testdb )
parsed = urlsplit(url)
db_name = parsed.path[1:]

# Get your DB
db = Connection(url)[db_name]

# Authenticate
if  @  in url:
    user, password = parsed.netloc.split( @ )[0].split( : )
    db.authenticate(user, password)

在安装了信oku后,通过在指挥线上运行<代码>heroku config进入连接线。

即将进入非政府组织会议部。 URI以这种形式:

<>code>MONGOLAB_URI => mongodb http://user:[电子邮箱::27707/db

简言之,通过从里面铺设一条连接线,从古丁的里叶中穿透。

我认为,应该做些什么:

import os
import sys
import pymongo


mongo_url = os.getenv( MONGOLAB_URI ,  mongodb://localhost:27017 )
db_name =  mongotest 

if __name__ ==  __main__ :
  try:
   connection = pymongo.Connection(mongo_url)
   if  localhost  in self.mongo_url:
     db_name =  my_local_db_name 
   else:
     db_name = self.mongo_url.rsplit( / ,1)[1]
   database = connection[db_name]
  except:
   print( Error: Unable to Connect )
   connection = None

if connection is not None:
  database.test.insert({ name :  foo })

PyMongo now provides a get_default_database() method that makes this entire exercise trivial:

from pymongo import MongoClient

client = MongoClient(os.environ[ MONGOLAB_URI ])
db = client.get_default_database()




相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签