[英]Authentication in Elasticsearch using python
我想在Elasticsearch中使用Python。 所以我用Python為Elasticsearch編寫了一個身份驗證代碼。 但是我收到錯誤消息“ TypeError:'Session'對象不可調用”。 這是代碼:
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)
我要去哪里錯了?
除了會話,您可以直接使用以下方法連接elasticsearch服務器:
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)
根據文檔http://docs.python-requests.org/en/master/user/authentication/
刪除會話對象並嘗試一下
requests.get('url', auth=HTTPBasicAuth('user', 'pass'))
如果要使用Session
對象,首先讓我們弄清楚它的用途 :
通過Session對象,您可以在請求中保留某些參數。
這是更改代碼的方式:
session = requests.Session()
session.auth = HTTPBasicAuth(uname, pswd)
res = session.get('http://localhost:9200')
並且get
將隱式使用上面定義的auth
。
檢查上面鏈接的頁面,這里有很多示例。
而且,如果您不打算重用連接,則可以刪除Session
並在get
使用auth
,就像Narenda在另一個答案中建議的那樣 。
試試這種簡單的方法來做到這一點。
import requests
from requests.auth import HTTPBasicAuth
res = requests.get(url="url", auth=HTTPBasicAuth("username", "password"))
print(res.json())
我希望這有幫助。
首先根據您的用戶名創建一個基本標頭身份驗證令牌,然后使用base64模塊傳遞它,如果您不知道如何使用它,只需在此處創建基本身份驗證標頭:
這樣做之后,創建將作為身份驗證頭傳遞的字典。 請參見下面的示例代碼:
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())
有什么原因為什么您不使用elasticsearch python客戶端庫。
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
一旦有了elasticsearch對象,就可以輕松查詢索引
res = es.get(index="test-index", doc_type='tweet')
print(res['_source'])
我建議使用elasticsearch庫進行連接。
這是一個例子:
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"})
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.