1

我已将 MongoLab 附加组件添加到 Heroku 实例,但我的应用程序无法连接到 MongoDB 主机。我看过这个帖子,但我没有看到任何有用的东西。

twitter_clone_python.py

f = Flask(__name__)

# 'TheFuture' is the name of my laptop. It signifies that we are in
# development mode.
if socket.gethostname().startswith('TheFuture'):
    f.config.from_object('config.DevelopmentConfig')
# If 'TheFuture' isn't the host, then we are in production.
else:
    f.config.from_object('config.ProductionConfig')

# MongoDB connection
connection = Connection(f.config['MONGODB_HOST'], f.config['MONGODB_PORT'])
db = connection['MONGODB_DB']

# Try authenticating. This will only work in production. In development,
# MONGODB_USER and MONGODB_PASSWORD will raise KeyErrors.
try:
    db.authenticate(f.config['MONGODB_USER'], f.config['MONGODB_PASSWORD'])
except KeyError:
    pass

配置文件

class Config(object):
    '''Default configuration object.'''
    DEBUG = False
    TESTING = False
    PORT = int(os.environ.get('PORT', 5000))

class ProductionConfig(Config):
    '''Configuration object specific to production environments.'''
    REDIS_URL = os.environ.get('REDISTOGO_URL')
    if REDIS_URL:
        url = urlparse.urlparse(REDIS_URL)
        REDIS_HOST = url.hostname
        REDIS_PORT = url.port
        REDIS_PASSWORD = url.password

    MONGOLAB_URI = os.environ.get('MONGOLAB_URI')
    if MONGOLAB_URI:
        url = urlparse.urlparse(MONGOLAB_URI)
        MONGODB_USER = url.username
        MONGODB_PASSWORD = url.password
        MONGODB_HOST = url.hostname
        MONGODB_PORT = url.port
        MONGODB_DB = url.path[1:]

class DevelopmentConfig(Config):
    '''Configuration object specific to development environments.'''
    DEBUG = True

    MONGODB_HOST = 'localhost'
    MONGODB_PORT = 27017
    MONGODB_DB = 'twitter-clone-python-development'

运行heroku logs显示错误:

2012-09-28T13:57:25+00:00 app[web.1]: pymongo.errors.AutoReconnect: could not connect to ds037997.mongolab.com:27017: timed out
4

0 回答 0