[英]check elasticsearch connection status in python
我正在嘗試在我的本地連接 elasticsearch,我想知道在繼續處理之前我怎么知道連接是成功還是失敗:我希望可以使用下面我使用的方式但不是(它返回太多值但都沒用) :
try:
es = Elasticsearch(['http://localhost:9200/'], verify_certs=True)
except Exception as err:
if "Connection refused" in err.message:
logging.error("Connection failed")
我希望有一種方法可以像這樣檢查連接狀態:
if es == false:
raise ValueError("Connection failed")
您可以做的是在創建Elasticsearch
實例后調用ping
,如下所示:
es = Elasticsearch(['http://localhost:9200/'], verify_certs=True)
if not es.ping():
raise ValueError("Connection failed")
我有同樣的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.