Skip to content

Instantly share code, notes, and snippets.

@Mithrilwoodrat
Last active March 26, 2018 09:22
Show Gist options
  • Save Mithrilwoodrat/a3ba4ed29af8e3a7937f0e8908855cbb to your computer and use it in GitHub Desktop.
Save Mithrilwoodrat/a3ba4ed29af8e3a7937f0e8908855cbb to your computer and use it in GitHub Desktop.
python script to make curl cmd for request elasticsearch
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
## usage: chmod +x es.py & mv es.py /usr/bin/es
## edit file to custom server
import os
from optparse import OptionParser
from urllib2 import urlparse
SERVER = "http://127.0.0.1:9200/"
METHODS = ["GET", "PUT", "POST", "DELETE"]
def main():
parser = OptionParser()
parser.add_option('-X', dest="method", help="METHOD", default="GET")
parser.add_option('-p', dest="hpath", help="urlpath", default="")
parser.add_option('-d', dest="data", help="data")
(options, args) = parser.parse_args()
method, hpath, data = options.method, options.hpath, options.data
#print options
if not method in METHODS:
print 'invalid method', method
return
url = urlparse.urljoin(SERVER, hpath)
if not '?' in hpath:
url += '?pretty'
else:
url += '&pretty'
if data is None:
cmdline = 'curl -X \'{0}\' -i \'{1}\''.format(method, url)
else:
cmdline = 'curl -X \'{0}\' -i \'{1}\' -d \'{2}\' '.format(method, url, data)
print cmdline
os.system(cmdline)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment