<p>创建ElasticSearch客户端时,可以将HTTP basic auth传递给<code>http_auth</code>参数:</p>
<pre><code>client = Elasticsearch(
hosts=['localhost:5000'],
http_auth=('username', 'password'),
)
s = Search(using=client, index='something')
</code></pre>
<p>这假设您使用的是具有<code>http_auth</code>参数的底层<a href="https://elasticsearch-py.readthedocs.io/en/7.10.0/transports.html#urllib3httpconnection" rel="nofollow noreferrer">^{<cd2>}</a>传输类</p>
<blockquote>
<pre><code>class elasticsearch.connection.Urllib3HttpConnection(host='localhost',
http_auth=None,
...,
**kwargs)
</code></pre>
<p>Default connection class using the urllib3 library and the http
protocol.</p>
<p><strong>Parameters</strong>:</p>
<ul>
<li><strong>http_auth</strong> – optional http auth information as either ‘:’ separated string or a tuple</li>
</ul>
</blockquote>
<p>有关身份验证的SSL和其他参数,请参阅文档的<a href="https://elasticsearch-py.readthedocs.io/en/7.10.0/#ssl-and-authentication" rel="nofollow noreferrer">SSL and Authentication</a>部分:</p>
<pre><code>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,
)
</code></pre>