Elasticsearch / Python / Proxy

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 env
from elasticsearch import Elasticsearch, RequestsHttpConnection
es = 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 Elasticsearch
es = Elasticsearch(send_get_body_as='POST')

I got an answer on GitHub:

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

Thanks a ton again!

from elasticsearch import RequestsHttpConnection

class MyConnection(RequestsHttpConnection):
    def __init__(self, *args, **kwargs):
        proxies = kwargs.pop('proxies', {})
        super(MyConnection, self).__init__(*args, **kwargs)
        self.session.proxies = proxies

es = Elasticsearch([es_url], connection_class=MyConnection, proxies = {'https': 'http://user:[email protected]:port'})


print(es.info())