【问题标题】:Instantiating an Elasticsearch client fails only when using python3仅在使用 python3 时实例化 Elasticsearch 客户端才会失败
【发布时间】:2017-05-09 22:50:50
【问题描述】:

我一直在尝试使用 python 的 elasticsearch 库连接到我的 ElasticSearch 主机。 所以代码看起来像:

client = Elasticsearch(
        ["https://my-machine.io:9200"],
        http_auth=("user", "password")
    )

现在的问题是,这条指令只有在我使用 python 2.7 解释器时才有效,而在 python 3.6 时它会失败,产生以下错误:

File "/usr/local/lib/python3.6/site-packages/elasticsearch/client/__init__.py", line 171, in __init__
    self.transport = transport_class(_normalize_hosts(hosts), **kwargs)
  File "/usr/local/lib/python3.6/site-packages/elasticsearch/transport.py", line 108, in __init__
    self.set_connections(hosts)
  File "/usr/local/lib/python3.6/site-packages/elasticsearch/transport.py", line 163, in set_connections
    connections = list(zip(connections, hosts))
  File "/usr/local/lib/python3.6/site-packages/elasticsearch/transport.py", line 160, in _create_connection
    return self.connection_class(**kwargs)
  File "/usr/local/lib/python3.6/site-packages/elasticsearch/connection/http_urllib3.py", line 78, in __init__
    raise ImproperlyConfigured("Root certificates are missing for certificate "
elasticsearch.exceptions.ImproperlyConfigured: Root certificates are missing for certificate validation. Either pass them in using the ca_certs parameter or install certifi to use it automatically.

Python 版本:

$ python2 --version
Python 2.7.13
$ python3 --version
Python 3.6.1

在这两种情况下,我都使用“elasticsearch”包,版本 5.3.0

我找不到任何可以根据所使用的 python 版本提出不同行为的文档。 谁能解释为什么会这样?

【问题讨论】:

  • 在每种情况下您使用的是哪个版本的 elasticsearch 模块?
  • 这可能会有所帮助:access.redhat.com/articles/2039753
  • 它适用于哪个版本的 python 2? (因为 urllib 的默认机制现在是默认检查证书)。在实例化您的客户端时,您是否尝试过使用参数verify_certs=False
  • 在你的 python2 的站点包中,在 elasticsearch/connection/http_urllib3.py 中,你有没有一行 if verify_certs?因为这是我能看到的唯一会引发此异常的更改。

标签: python python-3.x elasticsearch elasticsearch-py


【解决方案1】:

看起来问题出在您的 ssl 设置中,请确保您已安装 certifi,它将提供所需的根证书。

【讨论】:

猜你喜欢
  • 2014-07-14
  • 2019-05-05
  • 1970-01-01
  • 1970-01-01
  • 2020-07-05
  • 2015-02-19
  • 2021-12-20
  • 1970-01-01
  • 2020-09-11
相关资源
最近更新 更多