# Connecting ElasticSearch by python code on remote node

**URL:** <https://discuss.elastic.co/t/connecting-elasticsearch-by-python-code-on-remote-node/107147>\
**Category:** Elasticsearch\
**Created:** [November 10, 2017, 7:49am UTC](https://discuss.elastic.co/t/connecting-elasticsearch-by-python-code-on-remote-node/107147 "2017-11-10T07:49:21Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![pathakhcst](https://avatars.discourse-cdn.com/v4/letter/p/82dd89/32.png) [@pathakhcst](https://discuss.elastic.co/u/pathakhcst)\
**Post date:** [November 10, 2017, 7:49am UTC](https://discuss.elastic.co/t/connecting-elasticsearch-by-python-code-on-remote-node/107147/1 "2017-11-10T07:49:21Z")

</div>

I am hosting the ES server on one computer at my home and wants to push data from other computer remotely. In this case the remote server may or may not be available all the time so my remote python code crashes when ever the server is not reachable. Then I have restart the code.

es = Elasticsearch('[http://elastic:changeme@192.168.43.2:9200/](http://elastic:changeme@192.168.43.2:9200/)')  
es.index(index="test", doc\_type="string", body={"topic" : msg.topic, "dataString" : msg.payload, "timestamp": datetime.utcnow()})

How can I make it continuously running?

---

<div class="post-metadata">

**Author:** ![danielmitterdorfer](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/danielmitterdorfer/32/110510_2.png) [@danielmitterdorfer](https://discuss.elastic.co/u/danielmitterdorfer)\
**Post date:** [November 17, 2017, 8:08am UTC](https://discuss.elastic.co/t/connecting-elasticsearch-by-python-code-on-remote-node/107147/2 "2017-11-17T08:08:01Z")

</div>

Hi,

you can just wrap this in a `try` - `catch`:

```auto
es = Elasticsearch('http://elastic:changeme@192.168.43.2:9200/')
try:
    es.index(index="test", doc_type="string", body={"topic" : msg.topic, "dataString" : msg.payload, "timestamp": datetime.utcnow()})
catch elasticsearch.exceptions.ConnectionError:
    # TODO: Handle the error appropriately for your situation
    pass

```

This will not terminate your process but it just drops the document on the floor if it could not connect to Elasticsearch and this is probably _not_ what you want. So you'd need some kind of local buffer. This could be as simple as just adding the document to a Python list (in memory). When the document has been indexed successfully you can remove it from the list.

But obviously this is not persistent, i.e. if your process crashes or you terminate it before it got a chance to drain the local buffer, the documents are lost. If that is not ok for you then you need to make this persistent somehow, e.g. by writing to a file.

Daniel

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [December 15, 2017, 8:08am UTC](https://discuss.elastic.co/t/connecting-elasticsearch-by-python-code-on-remote-node/107147/3 "2017-12-15T08:08:19Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
