gpt4 book ai didi

python - 从我的电脑访问 Heroku Bonsai 上的 elasticsearch

转载 作者:行者123 更新时间:2023-12-01 02:31:19 25 4
gpt4 key购买 nike

我正在尝试 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?

最佳答案

您可能正在使用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

在实例化 elasticsearch 的文件中:

import certifi

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

关于python - 从我的电脑访问 Heroku Bonsai 上的 elasticsearch,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46796287/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com