[Python] Querying ElasticSearch with Python Requests

Discussão em 'Python' iniciado por Stack, Fevereiro 8, 2025.

  1. Stack

    Stack Membro Participativo

    ,I am not quite able to retrieve the results when I try to query ElasticSearch using python requests. Here is my code:

    json_data = updateJson(sys.argv[1])

    headers={'Accept': 'application/json', 'Content-type': 'application/json'}

    elastic_url ='https://localhost:9200/logstash-kafka-wga-blueid-\*/_search'

    query = json.dumps(json_data)

    response = requests.get(elastic_url, data = query, auth=('xxx','xxx'), verify=False, headers = headers)

    print response.text


    I always get the following output:

    {"took":1,"timed_out":false,"_shards":{"total":0,"successful":0,"failed":0},"hits":{"total":0,"max_score":0.0,"hits":[]}}


    But if I try to use the following CURL command, I get the right results. In the above code json_data, reads the json from abc.json file. Is there something incorrect in the above code?

    curl -X GET -k -u xxx:xxx https://localhost:9200/logstash-kafka-wga-blueid-\*/_search -d @temporaryRundeckReport.json


    Here is my updateJson() method:

    def updateJson(fileName):
    with open(fileName, 'r') as file:
    json_data = file.read()
    json_data = json_data.replace('%X-FORWARDED-HOST%', sys.argv[2]);
    json_data = json_data.replace('%TIME%', sys.argv[3]);
    json_data = json_data.replace('%INTERVAL%', sys.argv[4]);

    with open('temporaryRundeckReport.json', 'w+') as file:
    os.chmod('temporaryRundeckReport.json',0o777)
    file.write(json_data)
    return json_data

    Continue reading...

Compartilhe esta Página