简体   繁体   中英

Elasticsearch cannot connect with python client, ssl error

I am hosting standalone elasticsearch on external server. Using certificate and login-password authentication. I am able to connect to it using browser or postman.

However when I try to do it, using python client it doesn't work. My connection code:

        self.es = Elasticsearch(
            address,
            ca_certs=cert_path,
            basic_auth=(user, password),
        )

Error message:

elastic_transport.TlsError: TLS error caused by: TlsError(TLS error caused by: SSLError(hostname '34.116.***.***' doesn't match either of 'localhost', '172.19.0.2', '127.0.0.1', 'bde723133f75'))

Please try creating an ssl_context object and set the verification mode on the context.

import ssl
from elasticsearch.connection import create_ssl_context

ssl_context = create_ssl_context(<use `cafile`, or `cadata` or `capath` to set your CA or CAs)
context.check_hostname = False
context.verify_mode = ssl.CERT_NONE
es = Elasticsearch('localhost', ssl_context=context, timeout=60)

or you can try with this:

from ssl import create_default_context
from elasticsearch import Elasticsearch
es = Elasticsearch(["https://username:password@yourserver:9200"], verify_certs=False)
es.cat.nodes()

Option 2 will be warning, because it disable SSL with link url using HTTPS, but it work.

Using some help from Luc answer: Just before i tried something like this:

from ssl import create_default_context
import ssl
        context = create_default_context(capath=cert_path)
        context.check_hostname = False
        context.verify_mode = ssl.CERT_NONE
        self.es = Elasticsearch(
            address,
            ssl_context=context,
            basic_auth=(user, password),
            verify_certs=False,
        )

I am getting some warnings but other then that it works

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM