Как аутентифицироваться в elasticsearch через python requests?

Практикуюсь в elasticsearch. Через python клиент функции создания и чтения индексов работают. Я написал request к моей локальной базе данных на чтение индексов через curl но он просит меня аутентифицироваться.

Вот моя функция для создания индекса в базе через python-client

import requests
from elasticsearch import Elasticsearch


host = "localhost"
host_port = "9200"
user_name = "elastic"
password = "elasticpass"
host_uri = f"http://{host}:{host_port}"

es = Elasticsearch(
    host_uri, basic_auth = (user_name, password)
    )

print (dir(es.cluster.stats))

document_json1 = {
    "autor": "elasic",
    "text":"some text example",
    "created at" : datetime.now(),
}

res = es.index(index = "first_index", id = 1, body = document_json1)
print (res["result"])

Индекс создался и добавился в базу. Я получил его данные по curl запросу

http://localhost:9200/_cat/indices?v

введите сюда описание изображения

Теперь хочу получить данные по тому же запросу только через python requests. Для этого написал вспомогательную функцию:

def elasticsearch_curl(uri=host_uri, json_body='', verb='get'):
    # pass header option for content type if request has a
    # body to avoid Content-Type error in Elasticsearch v6.0
    headers = {
    # need to add auth keys
        'Content-Type': 'application/json',
    }

    try:
        # make HTTP verb parameter case-insensitive by converting to lower()
        if verb.lower() == "get":
            resp = requests.get(uri, headers=headers, data=json_body)
        elif verb.lower() == "post":
            resp = requests.post(uri, headers=headers, data=json_body)
        elif verb.lower() == "put":
            resp = requests.put(uri, headers=headers, data=json_body)

        # read the text object string
        try:
            resp_text = json_body.loads(resp.text)
        except:
            resp_text = resp.text

        # catch exceptions and print errors to terminal
    except Exception as error:
        print ('\nelasticsearch_curl() error:', error)
        resp_text = error

    # return the Python dict of the request
    print ("resp_text:", resp_text)
    return resp_text

И вызвал ее.


request_body = '''  
curl -X GET "localhost:9200/first-index?pretty"
'''
elasticsearch_curl(host_uri, request_body, "get")

Получаю в терминал ошибку с просьбой аутентефикации. Как аутентифицироваться без python клиента? Вот ошибка:

"error":{"root_cause":[{"type":"security_exception","reason":"missing authentication credentials for REST request [/]","header":{"WWW-Authenticate":["Basic realm=\"security\" charset=\"UTF-8\"","ApiKey"]}}],"type":"security_exception","reason":"missing authentication credentials for REST request [/]","header":{"WWW-Authenticate":["Basic realm=\"security\" charset=\"UTF-8\"","ApiKey"]}},"status":401}

Ответы (0 шт):