简体   繁体   English

在python中检查elasticsearch连接状态

[英]check elasticsearch connection status in python

I am trying to connect elasticsearch in my local and I wonder how can I know the connection is successful or failed before continue to process: I wish it is possible with the way below I used but not(it returns too many values but all useless):我正在尝试在我的本地连接 elasticsearch,我想知道在继续处理之前我怎么知道连接是成功还是失败:我希望可以使用下面我使用的方式但不是(它返回太多值但都没用) :

try:
    es = Elasticsearch(['http://localhost:9200/'], verify_certs=True)
except Exception as err:
    if "Connection refused" in err.message:
        logging.error("Connection failed")

I hope there is a way to check connection status like this:我希望有一种方法可以像这样检查连接状态:

if es == false:
    raise ValueError("Connection failed")

What you can do is call ping after creating the Elasticsearch instance, like this: 您可以做的是在创建Elasticsearch实例后调用ping ,如下所示:

es = Elasticsearch(['http://localhost:9200/'], verify_certs=True)

if not es.ping():
    raise ValueError("Connection failed")

I had same urllib3.exceptions.ProtocolError issue, so made up for myself.我有同样的urllib3.exceptions.ProtocolError问题,所以我自己弥补了。

import requests
def isRunning(self):
    try:
        res = requests.get("http://localhost:9200/_cluster/health")
        if res.status_code == 200:
            if res.json()['number_of_nodes'] > 0:
                return True
        return False
    except Exception as e:
        print(e)
        return False

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM