美文网首页
python连接elasticsearch

python连接elasticsearch

作者: 安静的码农 | 来源:发表于2021-07-05 21:43 被阅读0次

    import requests

    import datetime

    import time

    url_search = """http://xxx:9200/cdblog-stat-error/_search"""

    fmb_time = ((datetime.datetime.now()-datetime.timedelta(minutes=20)).strftime("%Y-%m-%dT%H:%M:%S+08:00"))

    now_time = (datetime.datetime.now()).strftime("%Y-%m-%dT%H:%M:%S+08:00")

    query = json.dumps({"query": {"bool": {"filter": [{"term": {"cdblog.alarm_words": "terribly wrong"}},{"range": {"@timestamp": {"gte": fmb_time,"lt": now_time}}}]}},"_source": {"includes": ["instinfo.instid","instinfo.ip","instinfo.port","instinfo.insttype","cdblog.timestamp"]}})

    try:

        headers = {"Content-Type": 'application/json'}

        user="elastic"

        password="xxx"

        response = requests.get(url_search, data=query,headers=headers,auth=(user,password))

        results = json.loads(response.text)

    except exceptions.Timeout as e:

        print('请求超时:'+str(e.message) + url_search)

        except exceptions.HTTPError as e:

        print('http请求错误:'+str(e.message) + url_search)

    相关文章

      网友评论

          本文标题:python连接elasticsearch

          本文链接:https://www.haomeiwen.com/subject/owaiultx.html