python处理xml文件参考:https://zhuanlan.zhihu.com/p/67735717
#!/usr/bin/env python
import requests
import json
import xml.etree.ElementTree as ET
PATH = "/"
URL = "http://EUREKA_IP:PORT/eureka/apps"
r=requests.get(URL)
with open('./temp.xml','w+') as F:
F.write(r.text)
def robot(appName,value):
headers = {"Content-Type": "application/json"}
url = "https://oapi.dingtalk.com/robot/send?access_token=XXXXXX"
data = {
"msgtype": "text",
"text": {
"content": "check: " + appName + "failed." + value
},
"isAtAll": True
}
value = json.dumps(data)
response = requests.post(url,data=value,headers=headers)
tree = ET.parse('temp.xml')
root = tree.getroot()
nodes = root.findall("application")
S = {}
for node in nodes:
appName = node.findall("name")[0].text
appInstances = node.findall("instance")
L = []
for instance in appInstances:
ip = instance.find("ipAddr").text
port = instance.find("port").text
value = ip + ':' + port
L.append(value)
S[appName] = L
#print S
for key, values in S.iteritems():
print "checking appname: %s..." % (key)
for value in values:
reqUrl = "http://" + value + PATH
req = requests.get(reqUrl)
if req.status_code != 200:
robot(key,value)
# break