Elasticsearch / Python / Proxy Elasticsearch / Python / Proxy elasticsearch elasticsearch

Elasticsearch / Python / Proxy


I got an answer on GitHub:

https://github.com/elastic/elasticsearch-py/issues/275#issuecomment-143781969

Thanks a ton again!

from elasticsearch import RequestsHttpConnectionclass MyConnection(RequestsHttpConnection):    def __init__(self, *args, **kwargs):        proxies = kwargs.pop('proxies', {})        super(MyConnection, self).__init__(*args, **kwargs)        self.session.proxies = proxieses = Elasticsearch([es_url], connection_class=MyConnection, proxies = {'https': 'http://user:pw@proxy.org:port'})print(es.info())


Generally, we don't need to add extra code for proxy, the python low-level module shall be able to use system proxy (i.e. http_proxy) directly.

In the later release (at least 6.x) we can use requests module instead of urllib3 to solve this problem nicely, see https://elasticsearch-py.readthedocs.io/en/master/transports.html

# make sure the http_proxy is in system envfrom elasticsearch import Elasticsearch, RequestsHttpConnectiones = Elasticsearch([es_url], connection_class=RequestsHttpConnection)

Another possible problem is search using GET method as default, it is rejected by my old cache server (squid/3.19), extra parameter send_get_body_as shall be added, see https://elasticsearch-py.readthedocs.io/en/master/#environment-considerations

from elasticsearch import Elasticsearches = Elasticsearch(send_get_body_as='POST')