Skip to content

Instantly share code, notes, and snippets.

@erfelipe
Created February 28, 2019 23:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save erfelipe/3d8e35899f1866ecfa8a81ac509a6c0e to your computer and use it in GitHub Desktop.
Save erfelipe/3d8e35899f1866ecfa8a81ac509a6c0e to your computer and use it in GitHub Desktop.
Elasticsearch library Connecting to Bonsai
# https://docs.bonsai.io/article/102-python
import os, base64, re, logging
from elasticsearch import Elasticsearch
# Log transport details (optional):
logging.basicConfig(level=logging.INFO)
# Parse the auth and host from env:
bonsai = os.environ['BONSAI_URL']
auth = re.search('https\:\/\/(.*)\@', bonsai).group(1).split(':')
host = bonsai.replace('https://%s:%s@' % (auth[0], auth[1]), '')
# Connect to cluster over SSL using auth for best security:
es_header = [{
'host': host,
'port': 443,
'use_ssl': True,
'http_auth': (auth[0],auth[1])
}]
# Instantiate the new Elasticsearch connection:
es = Elasticsearch(es_header)
# Verify that Python can talk to Bonsai (optional):
es.ping()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment