1. 程式人生 > >第一個python腳本

第一個python腳本

quest reat strftime creat http sta datetime port PE

#腳本用於創建es索引及別名

#!/usr/bin/python
import json
import requests
import datetime
import time

#get tomorrow date
now = datetime.datetime.now()
delta = datetime.timedelta(days=1)
n_days = now + delta


tomorrowDate2 = int(n_days.strftime(‘%M‘))//10
str = ‘%d‘%tomorrowDate2
tomorrowDate = n_days.strftime(‘%Y-%m-%d-%H‘)+"-"+str

#create_indices_function
def create_index(type):
index_name = type+"-njyh-"+tomorrowDate
req = "http://ip:9600/" + index_name + "/"
stats = requests.put(req).json()
time.sleep(1)
return

#create_alias_function_function
def create_alias(type,alias_name):
index_name = type+"-njyh-"+tomorrowDate
req="http://ip:9600/"+index_name+"/_alias/"+alias_name+"-"+tom0rrowDate
requests.put(req)
time.sleep(1)
return

list1 = [‘index_name1‘,‘index_name2‘]
for index in range(len(list1)):
create_index(list1[index])
create_alias(list1[index],list1[index])

第一個python腳本