从我的计算机上访问Heroku Bonsai的弹性搜索

时间:2017-10-17 17:54:38

标签: python elasticsearch heroku https bonsai-elasticsearch

我正在尝试ping我的Elasticsearch实例(通过Bonsai部署Heroku添加)。我遵循了他们的指导方针并尝试在我的计算机上执行以下代码:

from elasticsearch import Elasticsearch
from settings import BONSAI_URL
import re, logging

# Log transport details (optional):
logging.basicConfig(level=logging.INFO)

# Parse the auth and host from env:
bonsai = BONSAI_URL
print(bonsai)
auth = re.search('https\:\/\/(.*)\@', bonsai).group(1).split(':')
host = bonsai.replace('https://%s:%s@' % (auth[0], auth[1]), '')

# Connect to cluster over SSL using auth for best security:
es_header = [{
  'host': host,
  'port': 443,
  'use_ssl': True,
  'http_auth': (auth[0],auth[1])
}]

# Instantiate the new Elasticsearch connection:
es = Elasticsearch(es_header)

# Verify that Python can talk to Bonsai (optional):
es.ping()

我收到以下错误消息:

elasticsearch.exceptions.ImproperlyConfigured: Root certificates are missing for certificate validation. Either pass them in using the ca_certs parameter or install certifi to use it automatically.

我认为这个错误来自于我没有https证书,因此我使用了HTTP,删除了URL中的s和正则表达式并将use_ssl切换为False但是我收到了以下错误:

urllib3.exceptions.ProtocolError: ('Connection aborted.', ConnectionResetError(54, 'Connection reset by peer'))

如何将计算机中的数据插入Heroku上的elasticsearch?

2 个答案:

答案 0 :(得分:1)

问题是客户端无法找到根证书(这些证书存在于运行代码的计算机上)。如例外情况所述,您应该可以在{1}}中安装certifi,然后在脚本中安装pip,并且应该无问题as described here运行。

答案 1 :(得分:1)

你可能正在使用Python3。 问题在于你的python版本和urlib的行为方式。

快速修复可能是:

es_header = [{
'host': host,
'port': 443,
'use_ssl': True,
'http_auth': (auth[0],auth[1]),
'verify_certs': False
}]

但这种方式并不安全。更明确的解决方法可能是写下你的requirements.txt:

certifi

输入您的终端:

pip install -r requirements.txt

在您正在进行弹性搜索的文件中:

import certifi

然后启动您之前启动的完全相同的代码,它应该可以正常工作并且是安全的。