python连接elasticsearch

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)

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容