๐Ÿ’ปTech/๐Ÿpython

[Python] REST API ํ˜ธ์ถœ ์ƒ˜ํ”Œ ์ฝ”๋“œ

_viper_ 2019. 6. 13. 17:52
๋ฐ˜์‘ํ˜•

Python REST API ํ˜ธ์ถœ ์ƒ˜ํ”Œ ์ฝ”๋“œ์ž…๋‹ˆ๋‹ค.

  • ์ง€์ •์‹œ๊ฐ„ ์ดํ›„๋กœ ์ˆ˜ํ–‰๋˜๋Š” api killํ•˜๋Š” ์ฝ”๋“œ
import httplib, json, datetime

print datetime.datetime.now()          #๋‚ ์งœ ์ถœ๋ ฅ

INGEST_LIST="/druid/indexer/v1/tasks"  #API ์ฃผ์†Œ
HOST="aaa-bb-001"                      #API ํ˜ธ์ŠคํŠธ
PORT="8090"                            #API ํฌํŠธ

hour = (datetime.datetime.now() + datetime.timedelta(hours=-9)).hour

conn = httplib.HTTPConnection(HOST, PORT)  #connection ์„ค์ •
conn.request("GET", INGEST_LIST)           #request 
res = conn.getresponse()                   #response ๊ฒฐ๊ณผ ๋ณ€์ˆ˜์— ์ €์žฅ
taskList = json.loads(res.read())          #json๋ฌธ์ž์—ด dictionary๋กœ ๋ณ€ํ™˜

for task in taskList :                     #list for๋ฌธ
        if 'RUNNING' == task['statusCode'] and 'index_hadoop' == task['type']:
                print "id : " + task['id']
                if 'T' in task :
                        taskStartTime = task.split('T')[1][0:2]
                        if (int(hour)-int(taskStartTime)) > 2 :
                                print "KILL ID : " + task
                                conn.request("POST", "/druid/indexer/v1/task/" + task + "/shutdown")
                                res = conn.getresponse()
                                print "SUCCESS KILL"