使用ElasticSearch或OpenSearch python客户端连接失败,但可以通过curl建立连接,如何解决?

pw136qt2  于 6个月前  发布在  ElasticSearch
关注(0)|答案(2)|浏览(107)

尝试使用Python客户端连接到ES集群。当我通过curl检查连接时,我可以连接:x1c 0d1x
但是当我尝试使用ElasticSearch包并遵循documentation时,我在ping时遇到Connection failed:

headers = {"Authorization":"Bearer "+auth_token}
es = Elasticsearch(
    'https://cluster.xyz.com:443',
    bearer_auth=auth_token,
    request_timeout=10,
    verify_certs=False
)
if not es.ping():
    raise ValueError("Connection failed")

字符串

错误:

ValueError Traceback(most recent call last)es = Elasticsearch('https:/cluster.xyz.com:443',bearer_auth=auth_token,request_timeout=10,verify_certs=False)if not es.ping():---> raise ValueError(“Connection failed”)
ValueError:连接失败
我在curl上的变量auth_token中使用了相同的令牌,所以我确定令牌是正确的。为什么我在这种情况下无法连接?当我尝试使用OpenSearch时也会发生同样的事情。
错误消息:

oknrviil

oknrviil1#

你的URL是错误的,它在方案后缺少一个正斜杠。你也可以删除headers变量,因为你指定了bearer_auth,因此你没有使用标题。
这段代码应该可以工作:

es = Elasticsearch(
    'https://cluster.xyz.com:443',           <--- fix this
    bearer_auth=auth_token,
    request_timeout=10,
    verify_certs=False
)
if not es.ping():
    raise ValueError("Connection failed")

字符串

6kkfgxo0

6kkfgxo02#

我能够使用不同的方法解决连接问题:

es = Elasticsearch(es_url, headers=headers[environment], verify_certs=False)

字符串
这似乎工作,但当使用bearer_auth,我遇到了错误。

相关问题