# !/usr/bin/env python
# -*- coding: utf-8 -*-
import urllib2
import urllib
import cookielib
import json
import httplib
import re
import requests
from lxml import etree
import StringIO
import time
import sys
import json
import re
s = requests.session()
print s.headers
username='015208'
password='65c1978850b7dca18503d8e22073bab0'
login_url = 'http://1.1.1.51/api/logincheck?password=%s&username=%s' %(password,username)
print login_url
data = {'username':username,'password':password}
print data
#data = urllib.urlencode(data)
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:54.0) Gecko/20100101 Firefox/54.0'
}
response = s.get(login_url, data=data, headers=headers,timeout=10)
print response
print '--------------------'
print response.status_code
a= response.content
print a
print type(a)
p=re.compile('.*?<token>(.*?)</token>*')
m=p.match(a)
token= m.group(1)
getstatisurl='http://1.1.1.51/api/ALERT-SERVICE/v1.0/alertExcludes?series_id=1&userId=%s&user_id=%s' %(username,username)
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:54.0) Gecko/20100101 Firefox/54.0',
'token':token
}
c= s.get(getstatisurl,headers=headers).content
print type(c)
print c
d=json.loads(c)
print d
print type(d)
print '-------------------------------------'
for i in range(len(d['data'])):
e=d['data'][i]
print e
disable_url='http://1.1.1.51/api/ALERT-SERVICE/v1.0/batchAlertExcludes?series_id=1&user_id=%s' %(username)
print disable_url
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:54.0) Gecko/20100101 Firefox/54.0',
'token':token,
'Content-Type':'application/json;charset=utf-8'
}
data = [{"excludeId":251,"systemId":9103,"systemName":"AAAA","systemFullName":"AAAA","classId":2,"className"
:"业务监控","excludeMode":1,"excludeTime":'yyyy-mm-dd-00-00-00~yyyy-mm-dd-23-59-59',"excludeReason":'null'
,"lcd":1553233034000,"userId":"006727","set":'true',"diytext":"006727业务监控AAAAAAAA未屏蔽"},{"excludeId":378
,"systemId":9103,"systemName":"AAAA","systemFullName":"AAAA","classId":3,"className":"可用性监控","excludeMode"
:1,"excludeTime":"yyyy-mm-dd-00-00-00~yyyy-mm-dd-23-59-59","excludeReason":'null',"lcd":1553233248000,"userId"
:"015208","set":'true',"diytext":"015208可用性监控AAAAAAAA未屏蔽"},{"excludeId":1013,"systemId":9103,"systemName"
:"AAAA","systemFullName":"AAAA","classId":8,"className":"主机","excludeMode":1,"excludeTime":'yyyy-mm-dd-00-00-00~yyyy-mm-dd-23-59-59',"excludeReason":'null',"lcd":1553233248000,"userId":"006727","set":'true',"diytext"
:"006727主机AAAAAAAA未屏蔽"},{"excludeId":1021,"systemId":6747,"systemName":"ESB","systemFullName":"ESB","classId"
:9,"className":"进程","excludeMode":1,"excludeTime":'yyyy-mm-dd-00-00-00~yyyy-mm-dd-23-59-59',"excludeReason"
:'null',"lcd":1537425047000,"userId":"AUTO-KJL","set":'true',"diytext":"AUTO-KJL进程ESBESB未屏蔽"}]
#data = urllib.urlencode(data)
print data
response = s.post(disable_url, data=json.dumps(data), headers=headers,timeout=10)
print response.content
getstatisurl='http://1.1.1.51/api/ALERT-SERVICE/v1.0/alertExcludes?series_id=1&userId=%s&user_id=%s' %(username,username)
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:54.0) Gecko/20100101 Firefox/54.0',
'token':token
}
c= s.get(getstatisurl,headers=headers).content
print type(c)
print c
d=json.loads(c)
print d
print type(d)
print '-------------------------------------'
for i in range(len(d['data'])):
e=d['data'][i]
print str(e).decode('unicode-escape')
# for x in d['data'][i]:
# print x,d['data'][i][x]