๐ป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"