Launch elasticsearch server using python code

1.4k Views Asked by At

I would like to write a script in python which creates an elasticsearch server at localhost 9200. All of the examples that I find online regard connecting to an existing elasticsearch instance running at localhost 9200. My motivation is that I don't want to have to use the command line to run or shutoff the server.

Basically replace this line

 bin/elasticsearch

with something in python.

UPDATE: I tried the following

subprocess.popen('elasticsearch-1.4.0/bin/elasticsearch')

However, I am getting the error "AttributeError: 'module' object has no attribute 'popen'"

My application.py file will run the elasticsearch service and then create another service which I can use to make calls to the elasticsearch server.

Could anyone provide a code snippet of how I would go about creating the elasticsearch instance programmatically? Are there any existing projects that do this? Thanks in advance for any help.

3

There are 3 best solutions below

0
matt hoover On BEST ANSWER
from os import popen
import subprocess

subprocess.Popen('elasticsearch-1.4.0/bin/elasticsearch')
1
tzzz On

something like this?

from os import popen
popen('bin/elasticsearch')
0
kundan saha On

Elasticsearch takes some time to start. That's why you may not be able to see the connection and run the following commands immediately. Try running the script again after a few seconds.