1

我正在尝试 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,通过删除sURL 和正则表达式中的 并将其切换use_ssl为 False 但我收到以下错误:

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

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

4

2 回答 2

1

问题是客户端找不到根证书(这些证书位于您运行代码的计算机上)。正如例外所提到的,您应该能够在脚本中安装certifipip然后import certifi在脚本中进行安装,并且它应该可以正常运行,如此处所述

于 2017-10-17T19:25:35.877 回答
1

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

快速修复可能是:

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

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

于 2017-10-18T09:18:28.707 回答