修改lb权重,通知钉钉

前提需要安装阿里的核心库

#!/usr/local/python-3.6.4/bin/python3
#coding=utf-8
from aliyunsdkcore.client import AcsClient
from aliyunsdkcore.acs_exception.exceptions import ClientException
from aliyunsdkcore.acs_exception.exceptions import ServerException
from aliyunsdkslb.request.v20140515.SetBackendServersRequest import SetBackendServersRequest
from urllib import request as ddrequest
import json
from sys import argv

client = AcsClient('LTAI42e74K2hrtmu', 'f4S7rx9KEFv9Js3AR5KmU6V7g9D4xd', 'cn-beijing')
access_token = "dingding"

def set_lb(request,ecs,weight,loadslb):
    serverid = ecs.get("serverid")
    request.set_accept_format('json')
    slb_list = '[{"ServerId":"%s","Weight":"%s"},]' % (str(serverid),str(weight))
    print(slb_list)
    request.set_BackendServers(slb_list)
    request.set_LoadBalancerId(loadslb)
    try:
        response = client.do_action_with_exception(request)
    except Exception as e:
        response = {"hostname":ecs.get("hostname"),"ip":ecs.get("ip"),"message":"在修改权重为100的时候,发生了意外"}
        send_msg(response)
    else:
        response = {"hostname":ecs.get("hostname"),"ip":ecs.get("ip"),"message":"修改权重为100成功"}
        send_msg(response)

def send_msg(response):
    url = "https://oapi.dingtalk.com/robot/send?access_token=" + access_token
    data = {
        "msgtype": "text",
        "text": {
            "content": response
        },
        "at": {
            "isAtAll": "false"
        }
    }
    json_data= json.dumps(data).encode(encoding='utf-8')
    print(json_data)
    header_encoding = {"Content-Type": "application/json"}
    req = ddrequest.Request(url=url, data=json_data, headers=header_encoding)
    res = ddrequest.urlopen(req)
    res = res.read()
    print(res.decode(encoding='utf-8'))

if __name__ == "__main__":
    request = SetBackendServersRequest()
    ecs = {"hostname":"pc-prod-1","ip":"ip","serverid":"id"}
    weight = "100"
    loadslb = "lb-id"
    set_lb(request,ecs,weight,loadslb)

  

set_lb的更多相关文章

随机推荐

  1. time、datetime

    目录 time() datetime() time() python的时间模块 时间戳: 给电脑看的.1970-01-01 00:00:00到当前时间,按秒计算 格式化时间(Format String ...

  2. 洛谷 P1801 黑匣子_NOI导刊2010提高(06) 题解

    昨晚恶补了一下二叉堆的内容 然后就找了几个二叉堆的题来做awa 然后发现用二叉堆做这题复杂度是O(nlogn) 但是有O(n)的解法 (某大佬这么说) 思路大概就是: 利用一个大根堆一个小根堆来维护第 ...

  3. 【贪心】【P5521】[yLOI2019] 梅深不见冬

    B [yLOI2019] 梅深不见冬 Background 风,吹起梅岭的深冬:霜,如惊涛一样汹涌:雪,飘落后把所有烧成空, 像这场,捕捉不到的梦. 醒来时已是多年之久,宫门铜环才长了铁锈, 也开始生 ...

  4. sparksql基础知识二

    目标 掌握sparksql操作jdbc数据源 掌握sparksql保存数据操作 掌握sparksql整合hive 要点 1. jdbc数据源 spark sql可以通过 JDBC 从关系型数据库中读取 ...

  5. 【BigData】Java基础_构造方法的使用

    需求描述 实现上图需求,根据输入的三个人的信息,分别计算出这个三个客户的平均年龄和最大年龄 涉及知识点: ①构造方法 ②字符串切割 ③对象数组 代码实现 代码结构图: package cn.test. ...

  6. Glimma 包

    http://master.bioconductor.org/packages/3.9/bioc/html/Glimma.html 安装 if (!requireNamespace("Bio ...

  7. python 之类的继承

    #类的继承 class As1(): def As2(self): print("he11...") class As2(As1): def As2(self): print(&q ...

  8. Tomcat对取消post长度限制

    1.Tomcat 默认的post参数的最大大小为2M, 当超过时将会出错,可以配置maxPostSize参数来改变大小. 从 apache-tomcat-7.0.63 开始,参数 maxPostSiz ...

  9. just the check 买单

    今日焦点 Just the check 买单吧 split it 平分 leave a tip 留小费 词汇实践 hey. Can I get you anything else today? 您好. ...

  10. 64位Winows2008下连接Access数据库 Jet4.0不支持解决代替方案

    如何在windows2008 64位的机子上连接Access数据库 用以前的连接Access数据库的方式Provider=Microsoft.Jet.OLEDB.4.0在32位机子上可以使用,在64位 ...