I'm trying to query elasticsearch from a python script, and an exception is raised.
I followed the official guides such as this one . But when I'm trying to query elasticsearch, no success. here is the exception:
File "C:\...\connection\http_urllib3.py", line 250, in perform_request
raise ConnectionError("N/A", str(e), e)
elasticsearch.exceptions.ConnectionError:
ConnectionError(check_hostname requires server_hostname) caused by: ValueError(check_hostname requires server_hostname)
Here is my code:
from elasticsearch import Elasticsearch
from elasticsearch import RequestsHttpConnection
from ssl import create_default_context
import ssl
context = create_default_context(cafile="certificate.pem")
es = Elasticsearch("https://localhost", ssl_context=context, http_auth=('username','password'))
res = es.search(index="dr_*", body = {
'size' : 10,
'query': {
'match_all' : {}
}
})
Why does it happen?
Unfortunately, I also met such issue.
By default, the value of check_hostname in context is True, so you have to specify the server_hostname. Here is a simple solution, just add the following line after you create context
context.check_hostname = False
It should work without any issue
You don't mention the port in your example. Please verify the tutorial you mentioned and following snippet:
es = Elasticsearch(
['localhost', 'otherhost'],
http_auth=('user', 'secret'),
scheme="https",
port=443,
)
By doing context.check_hostname = False
you make the connection insecure.
Use this constructor instead:
Elasticsearch(
hosts=['ip_address'],,
http_auth=('elastic', 'pass'),
use_ssl=True,
verify_certs=True,
port=9200, #make sure of the port elasticsearch is using
ca_certs='/Users/raphaeldelio/ca.crt'
)
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.