修改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. dubbo-zookeeper demo解析图

  2. 待办事项App 评测

    1. 敬业签 2. Microsoft To-Do(奇妙清单) 3. Evernote 4.one note 5.Google Keep 6.to-do-ist 7.365 日历 8.Any.Do 9 ...

  3. selenium--设置浏览器的位置和高度宽度

    前戏 web自动化的时候,如果你只有一个显示器,这时如果启动了谷歌浏览器,占据了整个显示器,那你肯定是没办法干其他的事情了.当然,你也可以使用phantomjs无头浏览器,那浏览器的兼容性你又不能不测 ...

  4. Qt :编译警告 LNK4042对象被多次指定,已忽略多余的指定 ....segmentLayout.obj

    解决办法: 步骤: 1.将segmentLayout.h .segmentLayout.cpp 从工程中删除(no  permanently delete),并清理工程 2.在代码文件目录重命名seg ...

  5. MPI linux Ubuntu cluster 集群

    在局域网内安装mpi,并进行并行计算.MPICH3. 下载源码: wget http://www.mpich.org/static/downloads/3.3.1/mpich-3.3.1.tar.gz ...

  6. Intellij IDEA运行前不检查其他类的错误

    解决方法 第一步 第二步 在工具栏选择 , Run Configurations  设置在运行前不检查错误 

  7. 如何配置maven的环境变量

    安装maven后,这是maven的安装路径 打开:我的电脑——右键——属性——高级系统设置——环境变量 第一步:点击“系统变量(S)”下面的“新建(W)...”按钮,在“新建系统变量”中输入变量名MA ...

  8. @AspectJ注解的value属性

    @Component @Scope("prototype") @Aspect(value="perthis(execution(* com.helius.service. ...

  9. 前端与算法 leetcode 36. 有效的数独

    目录 # 前端与算法 leetcode 36. 有效的数独 题目描述 概要 提示 解析 算法 传入[['5', '3', '.', '.', '7', '.', '.', '.', '.'],['6' ...

  10. 第十六节:Asp.Net Core中Session的使用、扩展、进程外Session

    一. 简介 关于Session的原理可参照Asp.Net版本Session的文章,去查阅. 1. 普通用法 (1).通过Nuget引入[Microsoft.AspNetCore.Http]程序集,Co ...