python - 使用 python 在 Elasticsearch 中进行身份验证

标签 python elasticsearch python-requests

我想在 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服务器,而不是 session :

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)

关于python - 使用 python 在 Elasticsearch 中进行身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50366668/

相关文章:

python - 无法使用请求从网页中抓取某个字段的值

python - 多索引——访问每天最后一次

elasticsearch - 无法将非对象映射与机器学习(测试版)模块中的对象映射错误合并

python - matplotlib 绘图速度非常慢

elasticsearch - 将 Elasticsearch 插入我的ibm cloudant应用程序

c# - 查询返回两个文档而不是一个文档

python - 关闭挥之不去的连接

python - Zendesk:使用 Python 3.2.2 'requests' 模块和 API token 进行身份验证

python - 基于 Websockets 的存储后端

python - 安装 xlwt