Question

I can issue CURL commands to an instance of my elasticsearch engine from the command line as shown below. But how can I issue these commands in python monitoring script? I can only use the default python 2.7 built-in std python packages.

Examples of commands I want to run in a python script:

delete an index:

$ curl -XDELETE 'http://localhost:9200/twitter/'

check if index exists:

$ curl -XHEAD 'http://localhost:9200/twitter'


$ curl -XPUT 'http://localhost:9200/twitter/tweet/_mapping' -d '
{
    "tweet" : {
        "properties" : {
            "message" : {"type" : "string", "store" : "yes"}
        }
    }
}
'

Thank you in advance for any help you can provide.

Was it helpful?

Solution

If you can generate a list of arguments as strings, you can use the subprocess module easily. For example:

def curl_delete(url):
    cmd = ['curl', '-XDELETE', url]
    subprocess.check_call(cmd)

If you want to use the retcode as a value instead of just raising on non-zero:

    return subprocess.call(cmd)

Or if you want to get the stdout as a value, or anything else... See the subprocess docs section on replacing shell commands. Usually what you want is something like:

    p = subprocess.Popen(cmd, stdout=subprocess.PIPE)
    out, _ = p.communicate()
    return out

However, it's worth noting that the urllib2 module can do everything in your examples (although it will be painful for some of them), and httplib can do almost anything curl can do. For example:

def http_delete(url):
    bits = urlparse.urlparse(bits)
    conn = httplib.HTTPConnection(bits.host, bits.port)
    req = conn.request('delete', url)
    return req.getresponse()

This will return an object that can give you the response code and message, headers, and body. See the examples for more details.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top