导致“elasticsearch.exceptions.ConnectionError: ConnectionError...错误('getaddrinfo()参数2必须是整数或字符串'”的原因是什么
用这段简单的代码:
import elasticsearch
es = elasticsearch.Elasticsearch([{u'host': u'127.0.0.1', u'port': u'9200'}])
# then do anything involving a connection, eg:
es.indices.exists_alias('foo')
我遇到了这个错误:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/johnc/.virtualenvs/myproject/local/lib/python2.7/site-packages/elasticsearch/client/utils.py", line 68, in _wrapped
return func(*args, params=params, **kwargs)
File "/home/johnc/.virtualenvs/myproject/local/lib/python2.7/site-packages/elasticsearch/client/indices.py", line 348, in exists_alias
params=params)
File "/home/johnc/.virtualenvs/myproject/local/lib/python2.7/site-packages/elasticsearch/transport.py", line 276, in perform_request
status, headers, data = connection.perform_request(method, url, params, body, ignore=ignore, timeout=timeout)
File "/home/johnc/.virtualenvs/myproject/local/lib/python2.7/site-packages/elasticsearch/connection/http_urllib3.py", line 51, in perform_request
raise ConnectionError('N/A', str(e), e)
elasticsearch.exceptions.ConnectionError: ConnectionError(('Connection aborted.', error('getaddrinfo() argument 2 must be integer or string',))) caused by: ProtocolError(('Connection aborted.', error('getaddrinfo() argument 2 must be integer or string',)))
这是怎么回事呢?
1 个回答
6
看起来 urllib3(至少是 1.9 版本)不太喜欢把端口号作为 Unicode 字符串传入。如果把 port
改成字节字符串或者整数,就能解决这个问题,比如:
es = elasticsearch.Elasticsearch([{u'host': u'127.0.0.1', u'port': b'9200'}])
或者
es = elasticsearch.Elasticsearch([{u'host': u'127.0.0.1', u'port': 9200}])