Kailash Sharma
Kailash Sharma

Reputation: 51

Authentication in Elasticsearch using python

I want to use Python in Elasticsearch. So I wrote an Authentication code in Python for Elasticsearch. But I'm getting the error "TypeError: 'Session' object is not callable". Here's the code:

import requests
from requests import Session
from requests.auth import HTTPBasicAuth
uname = 'elastic'
pswd = 'elastic'
session = requests.Session()
session.auth = HTTPBasicAuth(uname, pswd)
res = requests.get('http://localhost:9200',auth=session)
print(res.content)

Where am I going wrong?

Upvotes: 2

Views: 18254

Answers (7)

Bitcon
Bitcon

Reputation: 141

I would recommend using the elasticsearch library to connect.

Here is an example:

from elasticsearch import Elasticsearch
es = Elasticsearch(['hostname:port'],http_auth=('username','password'))
#check if it exists...
if es.exists(index="test",doc_type="test",id="1234"):
    es.update(index="test",doc_type="test",id="1234",body{"doc": {"hi":"data"}})
else:
    es.create(index="test",doc_type="test",id="1234",body={"hi":"data"})

Upvotes: 1

vaishali KUNJIR
vaishali KUNJIR

Reputation: 1077

Instead of sessions you can directly use following method to connect to elasticsearch server:

import requests
from requests.auth import HTTPBasicAuth 
from pprint import pprint 

username = 'elastic'
password = 'elastic'

response = requests.get('http://localhost:9200', auth = HTTPBasicAuth(username, password))

pprint(response.content)

Upvotes: 6

jeremyforan
jeremyforan

Reputation: 1437

Is there any reason why you wouldn't use the elasticsearch python client library.

from elasticsearch import Elasticsearch

# you can use RFC-1738 to specify the url
es = Elasticsearch(['https://user:secret@localhost:443'])

# ... or specify common parameters as kwargs

es = Elasticsearch(
    ['localhost', 'otherhost'],
    http_auth=('user', 'secret'),
    scheme="https",
    port=443,
)

# SSL client authentication using client_cert and client_key

from ssl import create_default_context

context = create_default_context(cafile="path/to/cert.pem")
es = Elasticsearch(
    ['localhost', 'otherhost'],
    http_auth=('user', 'secret'),
    scheme="https",
    port=443,
    ssl_context=context,
)

https://elasticsearch-py.readthedocs.io/en/master/index.html

Once you have the elasticsearch object it is easy to query your index

res = es.get(index="test-index", doc_type='tweet')
print(res['_source'])

Upvotes: 1

Jan Carlo Once
Jan Carlo Once

Reputation: 25

First create a Basic header auth token based from your username and pass using base64 module, if you dont know how to use it just create Basic Authentication Header Here:

After doing so, create a dictionary which would be passed as the authentication header. See sample code below:

  import requests

  #username = elastic password = elastic
  auth_token = 'ZWxhc3RpYzplbGFzdGlj'
  head = head = {
  'Content-Type' : 'application/json; charset=utf8',
  'Authorization' : 'Basic %s' % auth_token,
  'User-Agent': 'Mozilla/5.0 (Windows NT 6.0; WOW64; rv:24.0) Gecko/20100101 
   Firefox/24.0'
  }

  res = requests.get('http://localhost:9200', header=head)
  print(res.json())

Upvotes: 0

Ashwani Shakya
Ashwani Shakya

Reputation: 439

Try this simple way to do this.

import requests
from requests.auth import HTTPBasicAuth
res = requests.get(url="url", auth=HTTPBasicAuth("username", "password"))
print(res.json())

I hope this helps.

Upvotes: 0

Nikolay Vasiliev
Nikolay Vasiliev

Reputation: 6066

If you want to use Session object, first let's clarify what is it for:

The Session object allows you to persist certain parameters across requests.

This is how you could change your code:

session = requests.Session()
session.auth = HTTPBasicAuth(uname, pswd)
res = session.get('http://localhost:9200')

And the get will use auth defined above it implicitly.

Check the page linked above, there are plenty examples.

And if you don't intend to reuse the connection, you can drop Session and go with auth in the get, like Narenda suggests in the other answer.

Upvotes: 0

Narendra Prasath
Narendra Prasath

Reputation: 1531

As per documentation http://docs.python-requests.org/en/master/user/authentication/

remove the session object and try this out

requests.get('url', auth=HTTPBasicAuth('user', 'pass'))

Upvotes: 0

Related Questions